Blame view

framework/thirdparty/jasmine/spec/suites/WaitsForBlockSpec.js 3.34 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  describe('WaitsForBlock', function () {
    var env, suite, timeout, spec, message, onComplete, fakeTimer;
    beforeEach(function() {
      env = new jasmine.Env();
      env.updateInterval = 0;
      suite = new jasmine.Suite(env, 'suite 1');
      timeout = 1000;
      spec = new jasmine.Spec(env, suite);
      message = "some error message";
      onComplete = jasmine.createSpy("onComplete");
    });
  
    it('onComplete should be called if the latchFunction returns true', function () {
      var latchFunction = function() {
        return true;
      };
      var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
      expect(onComplete).not.toHaveBeenCalled();
      block.execute(onComplete);
      expect(onComplete).toHaveBeenCalled();
    });
  
    it('latchFunction should run in same scope as spec', function () {
      var result;
      var latchFunction = function() {
        result = this.scopedValue;
      };
      spec.scopedValue = 'foo';
      var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
      block.execute(onComplete);
      expect(result).toEqual('foo');
    });
  
    it('should fail spec and call onComplete if there is an error in the latchFunction', function() {
      var latchFunction = jasmine.createSpy('latchFunction').andThrow('some error');
      spyOn(spec, 'fail');
      var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
      block.execute(onComplete);
      expect(spec.fail).toHaveBeenCalledWith('some error');
      expect(onComplete).toHaveBeenCalled();
    });
  
    describe("if latchFunction returns false", function() {
      var latchFunction, fakeTimer;
      beforeEach(function() {
        latchFunction = jasmine.createSpy('latchFunction').andReturn(false);
        fakeTimer = new jasmine.FakeTimer();
        env.setTimeout = fakeTimer.setTimeout;
        env.clearTimeout = fakeTimer.clearTimeout;
        env.setInterval = fakeTimer.setInterval;
        env.clearInterval = fakeTimer.clearInterval;
      });
  
      it('latchFunction should be retried after 10 ms', function () {
        var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
        expect(latchFunction).not.toHaveBeenCalled();
        block.execute(onComplete);
        expect(latchFunction.callCount).toEqual(1);
        fakeTimer.tick(5);
        expect(latchFunction.callCount).toEqual(1);
        fakeTimer.tick(5);
        expect(latchFunction.callCount).toEqual(2);
      });
  
      it('onComplete should be called if latchFunction returns true before timeout', function () {
        var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
        expect(onComplete).not.toHaveBeenCalled();
        block.execute(onComplete);
        expect(onComplete).not.toHaveBeenCalled();
        latchFunction.andReturn(true);
        fakeTimer.tick(100);
        expect(onComplete).toHaveBeenCalled();
      });
  
      it('spec should fail with the passed message if the timeout is reached (and not call onComplete)', function () {
        spyOn(spec, 'fail');
        var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
        block.execute(onComplete);
        expect(spec.fail).not.toHaveBeenCalled();
        fakeTimer.tick(timeout);
        expect(spec.fail).toHaveBeenCalled();
        var failMessage = spec.fail.mostRecentCall.args[0].message;
        expect(failMessage).toMatch(message);
        expect(onComplete).toHaveBeenCalled();
      });
    });
  });