2010-06-05 14:32:41 +00:00
|
|
|
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);
|
2010-08-20 06:55:21 +00:00
|
|
|
expect(onComplete).not.toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
block.execute(onComplete);
|
2010-06-24 17:34:03 +00:00
|
|
|
expect(onComplete).toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2010-06-24 17:34:03 +00:00
|
|
|
expect(spec.fail).toHaveBeenCalledWith('some error');
|
|
|
|
expect(onComplete).toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2010-08-20 06:55:21 +00:00
|
|
|
it('latchFunction should be retried after 10 ms', function () {
|
2010-06-05 14:32:41 +00:00
|
|
|
var block = new jasmine.WaitsForBlock(env, timeout, latchFunction, message, spec);
|
2010-08-20 06:55:21 +00:00
|
|
|
expect(latchFunction).not.toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
block.execute(onComplete);
|
|
|
|
expect(latchFunction.callCount).toEqual(1);
|
2010-08-20 06:55:21 +00:00
|
|
|
fakeTimer.tick(5);
|
2010-06-05 14:32:41 +00:00
|
|
|
expect(latchFunction.callCount).toEqual(1);
|
2010-08-20 06:55:21 +00:00
|
|
|
fakeTimer.tick(5);
|
2010-06-05 14:32:41 +00:00
|
|
|
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);
|
2010-08-20 06:55:21 +00:00
|
|
|
expect(onComplete).not.toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
block.execute(onComplete);
|
2010-08-20 06:55:21 +00:00
|
|
|
expect(onComplete).not.toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
latchFunction.andReturn(true);
|
|
|
|
fakeTimer.tick(100);
|
2010-06-24 17:34:03 +00:00
|
|
|
expect(onComplete).toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2010-08-20 06:55:21 +00:00
|
|
|
expect(spec.fail).not.toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
fakeTimer.tick(timeout);
|
2010-06-24 17:34:03 +00:00
|
|
|
expect(spec.fail).toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
var failMessage = spec.fail.mostRecentCall.args[0].message;
|
|
|
|
expect(failMessage).toMatch(message);
|
2010-08-26 00:16:19 +00:00
|
|
|
expect(onComplete).toHaveBeenCalled();
|
2010-06-05 14:32:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|