jasmine-core 2.99.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jasmine-core/boot.js +7 -6
- data/lib/jasmine-core/boot/boot.js +7 -6
- data/lib/jasmine-core/boot/node_boot.js +0 -3
- data/lib/jasmine-core/jasmine-html.js +228 -137
- data/lib/jasmine-core/jasmine.css +11 -5
- data/lib/jasmine-core/jasmine.js +1046 -608
- data/lib/jasmine-core/node_boot.js +0 -3
- data/lib/jasmine-core/spec/core/CallTrackerSpec.js +130 -0
- data/lib/jasmine-core/spec/core/ClearStackSpec.js +137 -0
- data/lib/jasmine-core/spec/core/ClockSpec.js +710 -0
- data/lib/jasmine-core/spec/core/DelayedFunctionSchedulerSpec.js +286 -0
- data/lib/jasmine-core/spec/core/EnvSpec.js +200 -0
- data/lib/jasmine-core/spec/core/ExceptionFormatterSpec.js +120 -0
- data/lib/jasmine-core/spec/core/ExceptionsSpec.js +46 -0
- data/lib/jasmine-core/spec/core/ExpectationResultSpec.js +61 -0
- data/lib/jasmine-core/spec/core/ExpectationSpec.js +434 -0
- data/lib/jasmine-core/spec/core/GlobalErrorsSpec.js +110 -0
- data/lib/jasmine-core/spec/core/JsApiReporterSpec.js +259 -0
- data/lib/jasmine-core/spec/core/MockDateSpec.js +200 -0
- data/lib/jasmine-core/spec/core/PrettyPrintSpec.js +332 -0
- data/lib/jasmine-core/spec/core/QueueRunnerSpec.js +670 -0
- data/lib/jasmine-core/spec/core/ReportDispatcherSpec.js +140 -0
- data/lib/jasmine-core/spec/core/SpecSpec.js +407 -0
- data/lib/jasmine-core/spec/core/SpyRegistrySpec.js +364 -0
- data/lib/jasmine-core/spec/core/SpySpec.js +177 -0
- data/lib/jasmine-core/spec/core/SpyStrategySpec.js +202 -0
- data/lib/jasmine-core/spec/core/StackTraceSpec.js +166 -0
- data/lib/jasmine-core/spec/core/SuiteSpec.js +123 -0
- data/lib/jasmine-core/spec/core/TimerSpec.js +31 -0
- data/lib/jasmine-core/spec/core/TreeProcessorSpec.js +794 -0
- data/lib/jasmine-core/spec/core/UserContextSpec.js +54 -0
- data/lib/jasmine-core/spec/core/UtilSpec.js +105 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/AnySpec.js +91 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/AnythingSpec.js +76 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/ArrayContainingSpec.js +52 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/ArrayWithExactContentsSpec.js +47 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/ObjectContainingSpec.js +99 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/StringMatchingSpec.js +27 -0
- data/lib/jasmine-core/spec/core/formatErrorMsgSpec.js +13 -0
- data/lib/jasmine-core/spec/core/integration/CustomMatchersSpec.js +200 -0
- data/lib/jasmine-core/spec/core/integration/CustomSpyStrategiesSpec.js +138 -0
- data/lib/jasmine-core/spec/core/integration/EnvSpec.js +2344 -0
- data/lib/jasmine-core/spec/core/integration/SpecRunningSpec.js +976 -0
- data/lib/jasmine-core/spec/core/matchers/DiffBuilderSpec.js +47 -0
- data/lib/jasmine-core/spec/core/matchers/NullDiffBuilderSpec.js +13 -0
- data/lib/jasmine-core/spec/core/matchers/ObjectPathSpec.js +43 -0
- data/lib/jasmine-core/spec/core/matchers/matchersUtilSpec.js +645 -0
- data/lib/jasmine-core/spec/core/matchers/nothingSpec.js +8 -0
- data/lib/jasmine-core/spec/core/matchers/toBeCloseToSpec.js +93 -0
- data/lib/jasmine-core/spec/core/matchers/toBeDefinedSpec.js +18 -0
- data/lib/jasmine-core/spec/core/matchers/toBeFalsySpec.js +38 -0
- data/lib/jasmine-core/spec/core/matchers/toBeGreaterThanOrEqualSpec.js +29 -0
- data/lib/jasmine-core/spec/core/matchers/toBeGreaterThanSpec.js +20 -0
- data/lib/jasmine-core/spec/core/matchers/toBeLessThanOrEqualSpec.js +29 -0
- data/lib/jasmine-core/spec/core/matchers/toBeLessThanSpec.js +20 -0
- data/lib/jasmine-core/spec/core/matchers/toBeNaNSpec.js +37 -0
- data/lib/jasmine-core/spec/core/matchers/toBeNegativeInfinitySpec.js +31 -0
- data/lib/jasmine-core/spec/core/matchers/toBeNullSpec.js +17 -0
- data/lib/jasmine-core/spec/core/matchers/toBePositiveInfinitySpec.js +31 -0
- data/lib/jasmine-core/spec/core/matchers/toBeSpec.js +17 -0
- data/lib/jasmine-core/spec/core/matchers/toBeTruthySpec.js +38 -0
- data/lib/jasmine-core/spec/core/matchers/toBeUndefinedSpec.js +18 -0
- data/lib/jasmine-core/spec/core/matchers/toContainSpec.js +26 -0
- data/lib/jasmine-core/spec/core/matchers/toEqualSpec.js +785 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledBeforeSpec.js +99 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledSpec.js +47 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledTimesSpec.js +86 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledWithSpec.js +67 -0
- data/lib/jasmine-core/spec/core/matchers/toMatchSpec.js +43 -0
- data/lib/jasmine-core/spec/core/matchers/toThrowErrorSpec.js +315 -0
- data/lib/jasmine-core/spec/core/matchers/toThrowMatchingSpec.js +73 -0
- data/lib/jasmine-core/spec/core/matchers/toThrowSpec.js +100 -0
- data/lib/jasmine-core/spec/helpers/BrowserFlags.js +15 -0
- data/lib/jasmine-core/spec/helpers/asyncAwait.js +27 -0
- data/lib/jasmine-core/spec/helpers/checkForMap.js +37 -0
- data/lib/jasmine-core/spec/helpers/checkForSet.js +41 -0
- data/lib/jasmine-core/spec/helpers/checkForSymbol.js +28 -0
- data/lib/jasmine-core/spec/helpers/checkForTypedArrays.js +20 -0
- data/lib/jasmine-core/spec/helpers/defineJasmineUnderTest.js +6 -0
- data/lib/jasmine-core/spec/helpers/integrationMatchers.js +43 -0
- data/lib/jasmine-core/spec/helpers/nodeDefineJasmineUnderTest.js +30 -0
- data/lib/jasmine-core/spec/html/HtmlReporterSpec.js +1261 -0
- data/lib/jasmine-core/spec/html/HtmlSpecFilterSpec.js +18 -0
- data/lib/jasmine-core/spec/html/MatchersHtmlSpec.js +37 -0
- data/lib/jasmine-core/spec/html/PrettyPrintHtmlSpec.js +27 -0
- data/lib/jasmine-core/spec/html/QueryStringSpec.js +72 -0
- data/lib/jasmine-core/spec/html/ResultsNodeSpec.js +62 -0
- data/lib/jasmine-core/spec/html/SpyRegistryHtmlSpec.js +39 -0
- data/lib/jasmine-core/spec/html/matchers/toHaveClassSpec.js +48 -0
- data/lib/jasmine-core/spec/npmPackage/npmPackageSpec.js +101 -0
- data/lib/jasmine-core/spec/performance/large_object_test.js +36 -0
- data/lib/jasmine-core/spec/performance/performance_test.js +10 -0
- data/lib/jasmine-core/version.rb +1 -1
- metadata +88 -4
- data/lib/console/console.js +0 -190
@@ -23,9 +23,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
23
|
module.exports = function(jasmineRequire) {
|
24
24
|
var jasmine = jasmineRequire.core(jasmineRequire);
|
25
25
|
|
26
|
-
var consoleFns = require('../console/console.js');
|
27
|
-
consoleFns.console(consoleFns, jasmine);
|
28
|
-
|
29
26
|
var env = jasmine.getEnv();
|
30
27
|
|
31
28
|
var jasmineInterface = jasmineRequire.interface(jasmine, env);
|
@@ -0,0 +1,130 @@
|
|
1
|
+
describe("CallTracker", function() {
|
2
|
+
it("tracks that it was called when executed", function() {
|
3
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
4
|
+
|
5
|
+
expect(callTracker.any()).toBe(false);
|
6
|
+
|
7
|
+
callTracker.track();
|
8
|
+
|
9
|
+
expect(callTracker.any()).toBe(true);
|
10
|
+
});
|
11
|
+
|
12
|
+
it("tracks that number of times that it is executed", function() {
|
13
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
14
|
+
|
15
|
+
expect(callTracker.count()).toEqual(0);
|
16
|
+
|
17
|
+
callTracker.track();
|
18
|
+
|
19
|
+
expect(callTracker.count()).toEqual(1);
|
20
|
+
});
|
21
|
+
|
22
|
+
it("tracks the params from each execution", function() {
|
23
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
24
|
+
|
25
|
+
callTracker.track({object: void 0, args: []});
|
26
|
+
callTracker.track({object: {}, args: [0, "foo"]});
|
27
|
+
|
28
|
+
expect(callTracker.argsFor(0)).toEqual([]);
|
29
|
+
|
30
|
+
expect(callTracker.argsFor(1)).toEqual([0, "foo"]);
|
31
|
+
});
|
32
|
+
|
33
|
+
it("returns any empty array when there was no call", function() {
|
34
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
35
|
+
|
36
|
+
expect(callTracker.argsFor(0)).toEqual([]);
|
37
|
+
});
|
38
|
+
|
39
|
+
it("allows access for the arguments for all calls", function() {
|
40
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
41
|
+
|
42
|
+
callTracker.track({object: {}, args: []});
|
43
|
+
callTracker.track({object: {}, args: [0, "foo"]});
|
44
|
+
|
45
|
+
expect(callTracker.allArgs()).toEqual([[], [0, "foo"]]);
|
46
|
+
});
|
47
|
+
|
48
|
+
it("tracks the context and arguments for each call", function() {
|
49
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
50
|
+
|
51
|
+
callTracker.track({object: {}, args: []});
|
52
|
+
callTracker.track({object: {}, args: [0, "foo"]});
|
53
|
+
|
54
|
+
expect(callTracker.all()[0]).toEqual({object: {}, args: []});
|
55
|
+
|
56
|
+
expect(callTracker.all()[1]).toEqual({object: {}, args: [0, "foo"]});
|
57
|
+
});
|
58
|
+
|
59
|
+
it("simplifies access to the arguments for the last (most recent) call", function() {
|
60
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
61
|
+
|
62
|
+
callTracker.track();
|
63
|
+
callTracker.track({object: {}, args: [0, "foo"]});
|
64
|
+
|
65
|
+
expect(callTracker.mostRecent()).toEqual({
|
66
|
+
object: {},
|
67
|
+
args: [0, "foo"]
|
68
|
+
});
|
69
|
+
});
|
70
|
+
|
71
|
+
it("returns a useful falsy value when there isn't a last (most recent) call", function() {
|
72
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
73
|
+
|
74
|
+
expect(callTracker.mostRecent()).toBeFalsy();
|
75
|
+
});
|
76
|
+
|
77
|
+
it("simplifies access to the arguments for the first (oldest) call", function() {
|
78
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
79
|
+
|
80
|
+
callTracker.track({object: {}, args: [0, "foo"]});
|
81
|
+
|
82
|
+
expect(callTracker.first()).toEqual({object: {}, args: [0, "foo"]})
|
83
|
+
});
|
84
|
+
|
85
|
+
it("returns a useful falsy value when there isn't a first (oldest) call", function() {
|
86
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
87
|
+
|
88
|
+
expect(callTracker.first()).toBeFalsy();
|
89
|
+
});
|
90
|
+
|
91
|
+
|
92
|
+
it("allows the tracking to be reset", function() {
|
93
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
94
|
+
|
95
|
+
callTracker.track();
|
96
|
+
callTracker.track({object: {}, args: [0, "foo"]});
|
97
|
+
callTracker.reset();
|
98
|
+
|
99
|
+
expect(callTracker.any()).toBe(false);
|
100
|
+
expect(callTracker.count()).toEqual(0);
|
101
|
+
expect(callTracker.argsFor(0)).toEqual([]);
|
102
|
+
expect(callTracker.all()).toEqual([]);
|
103
|
+
expect(callTracker.mostRecent()).toBeFalsy();
|
104
|
+
});
|
105
|
+
|
106
|
+
it("allows object arguments to be shallow cloned", function() {
|
107
|
+
var callTracker = new jasmineUnderTest.CallTracker();
|
108
|
+
callTracker.saveArgumentsByValue();
|
109
|
+
|
110
|
+
var objectArg = {"foo": "bar"},
|
111
|
+
arrayArg = ["foo", "bar"];
|
112
|
+
|
113
|
+
callTracker.track({object: {}, args: [objectArg, arrayArg, false, undefined, null, NaN, "", 0, 1.0]});
|
114
|
+
|
115
|
+
expect(callTracker.mostRecent().args[0]).not.toBe(objectArg);
|
116
|
+
expect(callTracker.mostRecent().args[0]).toEqual(objectArg);
|
117
|
+
expect(callTracker.mostRecent().args[1]).not.toBe(arrayArg);
|
118
|
+
expect(callTracker.mostRecent().args[1]).toEqual(arrayArg);
|
119
|
+
});
|
120
|
+
|
121
|
+
it('saves primitive arguments by value', function() {
|
122
|
+
var callTracker = new jasmineUnderTest.CallTracker(),
|
123
|
+
args = [undefined, null, false, '', /\s/, 0, 1.2, NaN];
|
124
|
+
|
125
|
+
callTracker.saveArgumentsByValue();
|
126
|
+
callTracker.track({ object: {}, args: args });
|
127
|
+
|
128
|
+
expect(callTracker.mostRecent().args).toEqual(args);
|
129
|
+
});
|
130
|
+
});
|
@@ -0,0 +1,137 @@
|
|
1
|
+
describe("ClearStack", function() {
|
2
|
+
it("works in an integrationy way", function(done) {
|
3
|
+
var clearStack = jasmineUnderTest.getClearStack(jasmineUnderTest.getGlobal());
|
4
|
+
|
5
|
+
clearStack(function() {
|
6
|
+
done();
|
7
|
+
});
|
8
|
+
});
|
9
|
+
|
10
|
+
it("uses setImmediate when available", function() {
|
11
|
+
var setImmediate = jasmine.createSpy('setImmediate').and.callFake(function(fn) { fn() }),
|
12
|
+
global = { setImmediate: setImmediate },
|
13
|
+
clearStack = jasmineUnderTest.getClearStack(global),
|
14
|
+
called = false;
|
15
|
+
|
16
|
+
clearStack(function() {
|
17
|
+
called = true;
|
18
|
+
});
|
19
|
+
|
20
|
+
expect(called).toBe(true);
|
21
|
+
expect(setImmediate).toHaveBeenCalled();
|
22
|
+
});
|
23
|
+
|
24
|
+
it("uses setTimeout instead of setImmediate every 10 calls to make sure we release the CPU", function() {
|
25
|
+
var setImmediate = jasmine.createSpy('setImmediate'),
|
26
|
+
setTimeout = jasmine.createSpy('setTimeout'),
|
27
|
+
global = { setImmediate: setImmediate, setTimeout: setTimeout },
|
28
|
+
clearStack = jasmineUnderTest.getClearStack(global);
|
29
|
+
|
30
|
+
clearStack(function() { });
|
31
|
+
clearStack(function() { });
|
32
|
+
clearStack(function() { });
|
33
|
+
clearStack(function() { });
|
34
|
+
clearStack(function() { });
|
35
|
+
clearStack(function() { });
|
36
|
+
clearStack(function() { });
|
37
|
+
clearStack(function() { });
|
38
|
+
clearStack(function() { });
|
39
|
+
|
40
|
+
expect(setImmediate).toHaveBeenCalled();
|
41
|
+
expect(setTimeout).not.toHaveBeenCalled();
|
42
|
+
|
43
|
+
clearStack(function() { });
|
44
|
+
expect(setImmediate.calls.count()).toEqual(9);
|
45
|
+
expect(setTimeout.calls.count()).toEqual(1);
|
46
|
+
|
47
|
+
clearStack(function() { });
|
48
|
+
expect(setImmediate.calls.count()).toEqual(10);
|
49
|
+
expect(setTimeout.calls.count()).toEqual(1);
|
50
|
+
});
|
51
|
+
|
52
|
+
it("uses MessageChannels when available", function() {
|
53
|
+
var fakeChannel = {
|
54
|
+
port1: {},
|
55
|
+
port2: { postMessage: function() { fakeChannel.port1.onmessage(); } }
|
56
|
+
},
|
57
|
+
global = { MessageChannel: function() { return fakeChannel; } },
|
58
|
+
clearStack = jasmineUnderTest.getClearStack(global),
|
59
|
+
called = false;
|
60
|
+
|
61
|
+
clearStack(function() {
|
62
|
+
called = true;
|
63
|
+
});
|
64
|
+
|
65
|
+
expect(called).toBe(true);
|
66
|
+
});
|
67
|
+
|
68
|
+
it("uses setTimeout instead of MessageChannel every 10 calls to make sure we release the CPU", function() {
|
69
|
+
var fakeChannel = {
|
70
|
+
port1: {},
|
71
|
+
port2: {
|
72
|
+
postMessage: jasmine.createSpy('postMessage').and.callFake(function() {
|
73
|
+
fakeChannel.port1.onmessage();
|
74
|
+
})
|
75
|
+
}
|
76
|
+
},
|
77
|
+
setTimeout = jasmine.createSpy('setTimeout'),
|
78
|
+
global = { MessageChannel: function() { return fakeChannel; }, setTimeout: setTimeout },
|
79
|
+
clearStack = jasmineUnderTest.getClearStack(global);
|
80
|
+
|
81
|
+
clearStack(function() { });
|
82
|
+
clearStack(function() { });
|
83
|
+
clearStack(function() { });
|
84
|
+
clearStack(function() { });
|
85
|
+
clearStack(function() { });
|
86
|
+
clearStack(function() { });
|
87
|
+
clearStack(function() { });
|
88
|
+
clearStack(function() { });
|
89
|
+
clearStack(function() { });
|
90
|
+
|
91
|
+
expect(fakeChannel.port2.postMessage).toHaveBeenCalled();
|
92
|
+
expect(setTimeout).not.toHaveBeenCalled();
|
93
|
+
|
94
|
+
clearStack(function() { });
|
95
|
+
expect(fakeChannel.port2.postMessage.calls.count()).toEqual(9);
|
96
|
+
expect(setTimeout.calls.count()).toEqual(1);
|
97
|
+
|
98
|
+
clearStack(function() { });
|
99
|
+
expect(fakeChannel.port2.postMessage.calls.count()).toEqual(10);
|
100
|
+
expect(setTimeout.calls.count()).toEqual(1);
|
101
|
+
});
|
102
|
+
|
103
|
+
it("calls setTimeout when onmessage is called recursively", function() {
|
104
|
+
var fakeChannel = {
|
105
|
+
port1: {},
|
106
|
+
port2: { postMessage: function() { fakeChannel.port1.onmessage(); } }
|
107
|
+
},
|
108
|
+
setTimeout = jasmine.createSpy('setTimeout'),
|
109
|
+
global = {
|
110
|
+
MessageChannel: function() { return fakeChannel; },
|
111
|
+
setTimeout: setTimeout,
|
112
|
+
},
|
113
|
+
clearStack = jasmineUnderTest.getClearStack(global),
|
114
|
+
fn = jasmine.createSpy("second clearStack function");
|
115
|
+
|
116
|
+
clearStack(function() {
|
117
|
+
clearStack(fn);
|
118
|
+
});
|
119
|
+
|
120
|
+
expect(fn).not.toHaveBeenCalled();
|
121
|
+
expect(setTimeout).toHaveBeenCalledWith(fn, 0);
|
122
|
+
});
|
123
|
+
|
124
|
+
it("falls back to setTimeout", function() {
|
125
|
+
var setTimeout = jasmine.createSpy('setTimeout').and.callFake(function(fn) { fn() }),
|
126
|
+
global = { setTimeout: setTimeout },
|
127
|
+
clearStack = jasmineUnderTest.getClearStack(global),
|
128
|
+
called = false;
|
129
|
+
|
130
|
+
clearStack(function() {
|
131
|
+
called = true;
|
132
|
+
});
|
133
|
+
|
134
|
+
expect(called).toBe(true);
|
135
|
+
expect(setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 0);
|
136
|
+
});
|
137
|
+
});
|
@@ -0,0 +1,710 @@
|
|
1
|
+
describe("Clock", function() {
|
2
|
+
|
3
|
+
var NODE_JS = typeof process !== 'undefined' && process.versions && typeof process.versions.node === 'string';
|
4
|
+
|
5
|
+
it("does not replace setTimeout until it is installed", function() {
|
6
|
+
var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
|
7
|
+
fakeGlobal = { setTimeout: fakeSetTimeout },
|
8
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["scheduleFunction"]),
|
9
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
10
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
11
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
12
|
+
|
13
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
14
|
+
|
15
|
+
expect(fakeSetTimeout).toHaveBeenCalledWith(delayedFn, 0);
|
16
|
+
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
17
|
+
|
18
|
+
fakeSetTimeout.calls.reset();
|
19
|
+
|
20
|
+
clock.install();
|
21
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
22
|
+
|
23
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalled();
|
24
|
+
expect(fakeSetTimeout).not.toHaveBeenCalled();
|
25
|
+
});
|
26
|
+
|
27
|
+
it("does not replace clearTimeout until it is installed", function() {
|
28
|
+
var fakeClearTimeout = jasmine.createSpy("global cleartimeout"),
|
29
|
+
fakeGlobal = { clearTimeout: fakeClearTimeout },
|
30
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["removeFunctionWithId"]),
|
31
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
32
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
33
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
34
|
+
|
35
|
+
fakeGlobal.clearTimeout("foo");
|
36
|
+
|
37
|
+
expect(fakeClearTimeout).toHaveBeenCalledWith("foo");
|
38
|
+
expect(delayedFunctionScheduler.removeFunctionWithId).not.toHaveBeenCalled();
|
39
|
+
|
40
|
+
fakeClearTimeout.calls.reset();
|
41
|
+
|
42
|
+
clock.install();
|
43
|
+
fakeGlobal.clearTimeout("foo");
|
44
|
+
|
45
|
+
expect(delayedFunctionScheduler.removeFunctionWithId).toHaveBeenCalled();
|
46
|
+
expect(fakeClearTimeout).not.toHaveBeenCalled();
|
47
|
+
});
|
48
|
+
|
49
|
+
it("does not replace setInterval until it is installed", function() {
|
50
|
+
var fakeSetInterval = jasmine.createSpy("global setInterval"),
|
51
|
+
fakeGlobal = { setInterval: fakeSetInterval },
|
52
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["scheduleFunction"]),
|
53
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
54
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
55
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
56
|
+
|
57
|
+
fakeGlobal.setInterval(delayedFn, 0);
|
58
|
+
|
59
|
+
expect(fakeSetInterval).toHaveBeenCalledWith(delayedFn, 0);
|
60
|
+
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
61
|
+
|
62
|
+
fakeSetInterval.calls.reset();
|
63
|
+
|
64
|
+
clock.install();
|
65
|
+
fakeGlobal.setInterval(delayedFn, 0);
|
66
|
+
|
67
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalled();
|
68
|
+
expect(fakeSetInterval).not.toHaveBeenCalled();
|
69
|
+
});
|
70
|
+
|
71
|
+
it("does not replace clearInterval until it is installed", function() {
|
72
|
+
var fakeClearInterval = jasmine.createSpy("global clearinterval"),
|
73
|
+
fakeGlobal = { clearInterval: fakeClearInterval },
|
74
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["removeFunctionWithId"]),
|
75
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
76
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
77
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
78
|
+
|
79
|
+
fakeGlobal.clearInterval("foo");
|
80
|
+
|
81
|
+
expect(fakeClearInterval).toHaveBeenCalledWith("foo");
|
82
|
+
expect(delayedFunctionScheduler.removeFunctionWithId).not.toHaveBeenCalled();
|
83
|
+
|
84
|
+
fakeClearInterval.calls.reset();
|
85
|
+
|
86
|
+
clock.install();
|
87
|
+
fakeGlobal.clearInterval("foo");
|
88
|
+
|
89
|
+
expect(delayedFunctionScheduler.removeFunctionWithId).toHaveBeenCalled();
|
90
|
+
expect(fakeClearInterval).not.toHaveBeenCalled();
|
91
|
+
});
|
92
|
+
|
93
|
+
it("does not install if the current setTimeout is not the original function on the global", function() {
|
94
|
+
var originalFakeSetTimeout = function() {},
|
95
|
+
replacedSetTimeout = function() {},
|
96
|
+
fakeGlobal = { setTimeout: originalFakeSetTimeout },
|
97
|
+
delayedFunctionSchedulerFactory = jasmine.createSpy('delayedFunctionSchedulerFactory'),
|
98
|
+
mockDate = {},
|
99
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, delayedFunctionSchedulerFactory, mockDate);
|
100
|
+
|
101
|
+
fakeGlobal.setTimeout = replacedSetTimeout;
|
102
|
+
|
103
|
+
expect(function() {
|
104
|
+
clock.install();
|
105
|
+
}).toThrowError(/unable to install/);
|
106
|
+
|
107
|
+
expect(delayedFunctionSchedulerFactory).not.toHaveBeenCalled();
|
108
|
+
expect(fakeGlobal.setTimeout).toBe(replacedSetTimeout);
|
109
|
+
});
|
110
|
+
|
111
|
+
it("does not install if the current clearTimeout is not the original function on the global", function() {
|
112
|
+
var originalFakeClearTimeout = function() {},
|
113
|
+
replacedClearTimeout = function() {},
|
114
|
+
fakeGlobal = { clearTimeout: originalFakeClearTimeout },
|
115
|
+
delayedFunctionSchedulerFactory = jasmine.createSpy('delayedFunctionSchedulerFactory'),
|
116
|
+
mockDate = {},
|
117
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, delayedFunctionSchedulerFactory, mockDate);
|
118
|
+
|
119
|
+
fakeGlobal.clearTimeout = replacedClearTimeout;
|
120
|
+
|
121
|
+
expect(function() {
|
122
|
+
clock.install();
|
123
|
+
}).toThrowError(/unable to install/);
|
124
|
+
|
125
|
+
expect(delayedFunctionSchedulerFactory).not.toHaveBeenCalled();
|
126
|
+
expect(fakeGlobal.clearTimeout).toBe(replacedClearTimeout);
|
127
|
+
});
|
128
|
+
|
129
|
+
it("does not install if the current setInterval is not the original function on the global", function() {
|
130
|
+
var originalFakeSetInterval = function() {},
|
131
|
+
replacedSetInterval = function() {},
|
132
|
+
fakeGlobal = { setInterval: originalFakeSetInterval },
|
133
|
+
delayedFunctionSchedulerFactory = jasmine.createSpy('delayedFunctionSchedulerFactory'),
|
134
|
+
mockDate = {},
|
135
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, delayedFunctionSchedulerFactory, mockDate);
|
136
|
+
|
137
|
+
fakeGlobal.setInterval = replacedSetInterval;
|
138
|
+
|
139
|
+
expect(function() {
|
140
|
+
clock.install();
|
141
|
+
}).toThrowError(/unable to install/);
|
142
|
+
|
143
|
+
expect(delayedFunctionSchedulerFactory).not.toHaveBeenCalled();
|
144
|
+
expect(fakeGlobal.setInterval).toBe(replacedSetInterval);
|
145
|
+
});
|
146
|
+
|
147
|
+
it("does not install if the current clearInterval is not the original function on the global", function() {
|
148
|
+
var originalFakeClearInterval = function() {},
|
149
|
+
replacedClearInterval = function() {},
|
150
|
+
fakeGlobal = { clearInterval: originalFakeClearInterval },
|
151
|
+
delayedFunctionSchedulerFactory = jasmine.createSpy('delayedFunctionSchedulerFactory'),
|
152
|
+
mockDate = {},
|
153
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, delayedFunctionSchedulerFactory, mockDate);
|
154
|
+
|
155
|
+
fakeGlobal.clearInterval = replacedClearInterval;
|
156
|
+
|
157
|
+
expect(function() {
|
158
|
+
clock.install();
|
159
|
+
}).toThrowError(/unable to install/);
|
160
|
+
|
161
|
+
expect(delayedFunctionSchedulerFactory).not.toHaveBeenCalled();
|
162
|
+
expect(fakeGlobal.clearInterval).toBe(replacedClearInterval);
|
163
|
+
});
|
164
|
+
|
165
|
+
it("replaces the global timer functions on uninstall", function() {
|
166
|
+
var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
|
167
|
+
fakeClearTimeout = jasmine.createSpy("global clearTimeout"),
|
168
|
+
fakeSetInterval = jasmine.createSpy("global setInterval"),
|
169
|
+
fakeClearInterval = jasmine.createSpy("global clearInterval"),
|
170
|
+
fakeGlobal = {
|
171
|
+
setTimeout: fakeSetTimeout,
|
172
|
+
clearTimeout: fakeClearTimeout,
|
173
|
+
setInterval: fakeSetInterval,
|
174
|
+
clearInterval: fakeClearInterval
|
175
|
+
},
|
176
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["scheduleFunction", "reset"]),
|
177
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
178
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
179
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
180
|
+
|
181
|
+
clock.install();
|
182
|
+
clock.uninstall();
|
183
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
184
|
+
fakeGlobal.clearTimeout("foo");
|
185
|
+
fakeGlobal.setInterval(delayedFn, 10);
|
186
|
+
fakeGlobal.clearInterval("bar");
|
187
|
+
|
188
|
+
expect(fakeSetTimeout).toHaveBeenCalledWith(delayedFn, 0);
|
189
|
+
expect(fakeClearTimeout).toHaveBeenCalledWith("foo");
|
190
|
+
expect(fakeSetInterval).toHaveBeenCalledWith(delayedFn, 10);
|
191
|
+
expect(fakeClearInterval).toHaveBeenCalledWith("bar");
|
192
|
+
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
193
|
+
});
|
194
|
+
|
195
|
+
it("can be installed for the duration of a passed in function and uninstalled when done", function() {
|
196
|
+
var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
|
197
|
+
fakeClearTimeout = jasmine.createSpy("global clearTimeout"),
|
198
|
+
fakeSetInterval = jasmine.createSpy("global setInterval"),
|
199
|
+
fakeClearInterval = jasmine.createSpy("global clearInterval"),
|
200
|
+
fakeGlobal = {
|
201
|
+
setTimeout: fakeSetTimeout,
|
202
|
+
clearTimeout: fakeClearTimeout,
|
203
|
+
setInterval: fakeSetInterval,
|
204
|
+
clearInterval: fakeClearInterval
|
205
|
+
},
|
206
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["scheduleFunction", "reset", "removeFunctionWithId"]),
|
207
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
208
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
209
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
|
210
|
+
passedFunctionCalled = false;
|
211
|
+
|
212
|
+
clock.withMock(function() {
|
213
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
214
|
+
fakeGlobal.clearTimeout("foo");
|
215
|
+
fakeGlobal.setInterval(delayedFn, 10);
|
216
|
+
fakeGlobal.clearInterval("bar");
|
217
|
+
passedFunctionCalled = true;
|
218
|
+
});
|
219
|
+
|
220
|
+
expect(passedFunctionCalled).toBe(true);
|
221
|
+
|
222
|
+
expect(fakeSetTimeout).not.toHaveBeenCalled();
|
223
|
+
expect(fakeClearTimeout).not.toHaveBeenCalled();
|
224
|
+
expect(fakeSetInterval).not.toHaveBeenCalled();
|
225
|
+
expect(fakeClearInterval).not.toHaveBeenCalled();
|
226
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalled();
|
227
|
+
|
228
|
+
delayedFunctionScheduler.scheduleFunction.calls.reset();
|
229
|
+
|
230
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
231
|
+
fakeGlobal.clearTimeout("foo");
|
232
|
+
fakeGlobal.setInterval(delayedFn, 10);
|
233
|
+
fakeGlobal.clearInterval("bar");
|
234
|
+
|
235
|
+
expect(fakeSetTimeout).toHaveBeenCalledWith(delayedFn, 0);
|
236
|
+
expect(fakeClearTimeout).toHaveBeenCalledWith("foo");
|
237
|
+
expect(fakeSetInterval).toHaveBeenCalledWith(delayedFn, 10);
|
238
|
+
expect(fakeClearInterval).toHaveBeenCalledWith("bar");
|
239
|
+
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
240
|
+
});
|
241
|
+
|
242
|
+
it("can be installed for the duration of a passed in function and uninstalled if an error is thrown", function() {
|
243
|
+
var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
|
244
|
+
fakeClearTimeout = jasmine.createSpy("global clearTimeout"),
|
245
|
+
fakeSetInterval = jasmine.createSpy("global setInterval"),
|
246
|
+
fakeClearInterval = jasmine.createSpy("global clearInterval"),
|
247
|
+
fakeGlobal = {
|
248
|
+
setTimeout: fakeSetTimeout,
|
249
|
+
clearTimeout: fakeClearTimeout,
|
250
|
+
setInterval: fakeSetInterval,
|
251
|
+
clearInterval: fakeClearInterval
|
252
|
+
},
|
253
|
+
delayedFunctionScheduler = jasmine.createSpyObj("delayedFunctionScheduler", ["scheduleFunction", "reset", "removeFunctionWithId"]),
|
254
|
+
delayedFn = jasmine.createSpy("delayedFn"),
|
255
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
256
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
|
257
|
+
passedFunctionCalled = false;
|
258
|
+
|
259
|
+
expect(function() {
|
260
|
+
clock.withMock(function() {
|
261
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
262
|
+
fakeGlobal.clearTimeout("foo");
|
263
|
+
fakeGlobal.setInterval(delayedFn, 10);
|
264
|
+
fakeGlobal.clearInterval("bar");
|
265
|
+
passedFunctionCalled = true;
|
266
|
+
throw 'oops';
|
267
|
+
});
|
268
|
+
}).toThrow('oops');
|
269
|
+
|
270
|
+
expect(passedFunctionCalled).toBe(true);
|
271
|
+
|
272
|
+
expect(fakeSetTimeout).not.toHaveBeenCalled();
|
273
|
+
expect(fakeClearTimeout).not.toHaveBeenCalled();
|
274
|
+
expect(fakeSetInterval).not.toHaveBeenCalled();
|
275
|
+
expect(fakeClearInterval).not.toHaveBeenCalled();
|
276
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalled();
|
277
|
+
|
278
|
+
delayedFunctionScheduler.scheduleFunction.calls.reset();
|
279
|
+
|
280
|
+
fakeGlobal.setTimeout(delayedFn, 0);
|
281
|
+
fakeGlobal.clearTimeout("foo");
|
282
|
+
fakeGlobal.setInterval(delayedFn, 10);
|
283
|
+
fakeGlobal.clearInterval("bar");
|
284
|
+
|
285
|
+
expect(fakeSetTimeout).toHaveBeenCalledWith(delayedFn, 0);
|
286
|
+
expect(fakeClearTimeout).toHaveBeenCalledWith("foo");
|
287
|
+
expect(fakeSetInterval).toHaveBeenCalledWith(delayedFn, 10);
|
288
|
+
expect(fakeClearInterval).toHaveBeenCalledWith("bar");
|
289
|
+
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
290
|
+
});
|
291
|
+
|
292
|
+
it("schedules the delayed function (via setTimeout) with the fake timer", function() {
|
293
|
+
var fakeSetTimeout = jasmine.createSpy('setTimeout'),
|
294
|
+
scheduleFunction = jasmine.createSpy('scheduleFunction'),
|
295
|
+
delayedFunctionScheduler = { scheduleFunction: scheduleFunction },
|
296
|
+
fakeGlobal = { setTimeout: fakeSetTimeout },
|
297
|
+
delayedFn = jasmine.createSpy('delayedFn'),
|
298
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
299
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
|
300
|
+
timeout = new clock.FakeTimeout();
|
301
|
+
|
302
|
+
clock.install();
|
303
|
+
clock.setTimeout(delayedFn, 0, 'a', 'b');
|
304
|
+
|
305
|
+
expect(fakeSetTimeout).not.toHaveBeenCalled();
|
306
|
+
|
307
|
+
if (!NODE_JS) {
|
308
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b']);
|
309
|
+
} else {
|
310
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], false, timeout);
|
311
|
+
}
|
312
|
+
});
|
313
|
+
|
314
|
+
it("returns an id for the delayed function", function() {
|
315
|
+
var fakeSetTimeout = jasmine.createSpy('setTimeout'),
|
316
|
+
scheduleId = 123,
|
317
|
+
scheduleFunction = jasmine.createSpy('scheduleFunction').and.returnValue(scheduleId),
|
318
|
+
delayedFunctionScheduler = {scheduleFunction: scheduleFunction},
|
319
|
+
fakeGlobal = { setTimeout: fakeSetTimeout },
|
320
|
+
delayedFn = jasmine.createSpy('delayedFn'),
|
321
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
322
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
|
323
|
+
timeout;
|
324
|
+
|
325
|
+
clock.install();
|
326
|
+
timeout = clock.setTimeout(delayedFn, 0);
|
327
|
+
|
328
|
+
if (!NODE_JS) {
|
329
|
+
expect(timeout).toEqual(123);
|
330
|
+
} else {
|
331
|
+
expect(timeout.constructor.name).toEqual('FakeTimeout');
|
332
|
+
}
|
333
|
+
});
|
334
|
+
|
335
|
+
it("clears the scheduled function with the scheduler", function() {
|
336
|
+
var fakeClearTimeout = jasmine.createSpy('clearTimeout'),
|
337
|
+
delayedFunctionScheduler = jasmine.createSpyObj('delayedFunctionScheduler', ['removeFunctionWithId']),
|
338
|
+
fakeGlobal = { setTimeout: fakeClearTimeout },
|
339
|
+
delayedFn = jasmine.createSpy('delayedFn'),
|
340
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
341
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
342
|
+
|
343
|
+
clock.install();
|
344
|
+
clock.clearTimeout(123);
|
345
|
+
|
346
|
+
expect(fakeClearTimeout).not.toHaveBeenCalled();
|
347
|
+
expect(delayedFunctionScheduler.removeFunctionWithId).toHaveBeenCalledWith(123);
|
348
|
+
});
|
349
|
+
|
350
|
+
it("schedules the delayed function with the fake timer", function() {
|
351
|
+
var fakeSetInterval = jasmine.createSpy('setInterval'),
|
352
|
+
scheduleFunction = jasmine.createSpy('scheduleFunction'),
|
353
|
+
delayedFunctionScheduler = {scheduleFunction: scheduleFunction},
|
354
|
+
fakeGlobal = { setInterval: fakeSetInterval },
|
355
|
+
delayedFn = jasmine.createSpy('delayedFn'),
|
356
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
357
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
|
358
|
+
timeout = new clock.FakeTimeout;
|
359
|
+
|
360
|
+
clock.install();
|
361
|
+
clock.setInterval(delayedFn, 0, 'a', 'b');
|
362
|
+
|
363
|
+
expect(fakeSetInterval).not.toHaveBeenCalled();
|
364
|
+
|
365
|
+
if (!NODE_JS) {
|
366
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true);
|
367
|
+
} else {
|
368
|
+
expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true, timeout);
|
369
|
+
}
|
370
|
+
});
|
371
|
+
|
372
|
+
it("returns an id for the delayed function", function() {
|
373
|
+
var fakeSetInterval = jasmine.createSpy('setInterval'),
|
374
|
+
scheduleId = 123,
|
375
|
+
scheduleFunction = jasmine.createSpy('scheduleFunction').and.returnValue(scheduleId),
|
376
|
+
delayedFunctionScheduler = {scheduleFunction: scheduleFunction},
|
377
|
+
fakeGlobal = { setInterval: fakeSetInterval },
|
378
|
+
delayedFn = jasmine.createSpy('delayedFn'),
|
379
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
380
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
|
381
|
+
interval;
|
382
|
+
|
383
|
+
clock.install();
|
384
|
+
interval = clock.setInterval(delayedFn, 0);
|
385
|
+
|
386
|
+
if (!NODE_JS) {
|
387
|
+
expect(interval).toEqual(123);
|
388
|
+
} else {
|
389
|
+
expect(interval.constructor.name).toEqual('FakeTimeout');
|
390
|
+
}
|
391
|
+
});
|
392
|
+
|
393
|
+
it("clears the scheduled function with the scheduler", function() {
|
394
|
+
var clearInterval = jasmine.createSpy('clearInterval'),
|
395
|
+
delayedFunctionScheduler = jasmine.createSpyObj('delayedFunctionScheduler', ['removeFunctionWithId']),
|
396
|
+
fakeGlobal = { setInterval: clearInterval },
|
397
|
+
delayedFn = jasmine.createSpy('delayedFn'),
|
398
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
399
|
+
clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
|
400
|
+
|
401
|
+
clock.install();
|
402
|
+
clock.clearInterval(123);
|
403
|
+
|
404
|
+
expect(clearInterval).not.toHaveBeenCalled();
|
405
|
+
expect(delayedFunctionScheduler.removeFunctionWithId).toHaveBeenCalledWith(123);
|
406
|
+
});
|
407
|
+
|
408
|
+
it("gives you a friendly reminder if the Clock is not installed and you tick", function() {
|
409
|
+
var clock = new jasmineUnderTest.Clock({}, jasmine.createSpyObj('delayedFunctionScheduler', ['tick']));
|
410
|
+
expect(function() {
|
411
|
+
clock.tick(50);
|
412
|
+
}).toThrow();
|
413
|
+
});
|
414
|
+
});
|
415
|
+
|
416
|
+
describe("Clock (acceptance)", function() {
|
417
|
+
it("can run setTimeouts/setIntervals synchronously", function() {
|
418
|
+
var delayedFn1 = jasmine.createSpy('delayedFn1'),
|
419
|
+
delayedFn2 = jasmine.createSpy('delayedFn2'),
|
420
|
+
delayedFn3 = jasmine.createSpy('delayedFn3'),
|
421
|
+
recurring1 = jasmine.createSpy('recurring1'),
|
422
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
423
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
424
|
+
clock = new jasmineUnderTest.Clock({setTimeout: setTimeout}, function () { return delayedFunctionScheduler; }, mockDate);
|
425
|
+
|
426
|
+
clock.install();
|
427
|
+
|
428
|
+
clock.setTimeout(delayedFn1, 0);
|
429
|
+
var intervalId = clock.setInterval(recurring1, 50);
|
430
|
+
clock.setTimeout(delayedFn2, 100);
|
431
|
+
clock.setTimeout(delayedFn3, 200);
|
432
|
+
|
433
|
+
expect(delayedFn1).not.toHaveBeenCalled();
|
434
|
+
expect(delayedFn2).not.toHaveBeenCalled();
|
435
|
+
expect(delayedFn3).not.toHaveBeenCalled();
|
436
|
+
|
437
|
+
clock.tick(0);
|
438
|
+
|
439
|
+
expect(delayedFn1).toHaveBeenCalled();
|
440
|
+
expect(delayedFn2).not.toHaveBeenCalled();
|
441
|
+
expect(delayedFn3).not.toHaveBeenCalled();
|
442
|
+
|
443
|
+
clock.tick(50);
|
444
|
+
|
445
|
+
expect(recurring1).toHaveBeenCalled();
|
446
|
+
expect(recurring1.calls.count()).toBe(1);
|
447
|
+
expect(delayedFn2).not.toHaveBeenCalled();
|
448
|
+
expect(delayedFn3).not.toHaveBeenCalled();
|
449
|
+
|
450
|
+
clock.tick(50);
|
451
|
+
|
452
|
+
expect(recurring1.calls.count()).toBe(2);
|
453
|
+
expect(delayedFn2).toHaveBeenCalled();
|
454
|
+
expect(delayedFn3).not.toHaveBeenCalled();
|
455
|
+
|
456
|
+
clock.tick(100);
|
457
|
+
|
458
|
+
expect(recurring1.calls.count()).toBe(4);
|
459
|
+
expect(delayedFn3).toHaveBeenCalled();
|
460
|
+
|
461
|
+
clock.clearInterval(intervalId);
|
462
|
+
clock.tick(50);
|
463
|
+
|
464
|
+
expect(recurring1.calls.count()).toBe(4);
|
465
|
+
});
|
466
|
+
|
467
|
+
it("can clear a previously set timeout", function() {
|
468
|
+
var clearedFn = jasmine.createSpy('clearedFn'),
|
469
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
470
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
471
|
+
clock = new jasmineUnderTest.Clock({setTimeout: function() {}}, function () { return delayedFunctionScheduler; }, mockDate),
|
472
|
+
timeoutId;
|
473
|
+
|
474
|
+
clock.install();
|
475
|
+
|
476
|
+
timeoutId = clock.setTimeout(clearedFn, 100);
|
477
|
+
expect(clearedFn).not.toHaveBeenCalled();
|
478
|
+
|
479
|
+
clock.clearTimeout(timeoutId);
|
480
|
+
clock.tick(100);
|
481
|
+
|
482
|
+
expect(clearedFn).not.toHaveBeenCalled();
|
483
|
+
});
|
484
|
+
|
485
|
+
it("can clear a previously set interval using that interval's handler", function() {
|
486
|
+
var spy = jasmine.createSpy('spy'),
|
487
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
488
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
489
|
+
clock = new jasmineUnderTest.Clock({setInterval: function() {}}, function () { return delayedFunctionScheduler; }, mockDate),
|
490
|
+
intervalId;
|
491
|
+
|
492
|
+
clock.install();
|
493
|
+
|
494
|
+
intervalId = clock.setInterval(function() {
|
495
|
+
spy();
|
496
|
+
clock.clearInterval(intervalId);
|
497
|
+
}, 100);
|
498
|
+
clock.tick(200);
|
499
|
+
|
500
|
+
expect(spy.calls.count()).toEqual(1);
|
501
|
+
});
|
502
|
+
|
503
|
+
it("correctly schedules functions after the Clock has advanced", function() {
|
504
|
+
var delayedFn1 = jasmine.createSpy('delayedFn1'),
|
505
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
506
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
507
|
+
clock = new jasmineUnderTest.Clock({setTimeout: function() {}}, function () { return delayedFunctionScheduler; }, mockDate);
|
508
|
+
|
509
|
+
clock.install();
|
510
|
+
|
511
|
+
clock.tick(100);
|
512
|
+
clock.setTimeout(delayedFn1, 10, ['some', 'arg']);
|
513
|
+
clock.tick(5);
|
514
|
+
expect(delayedFn1).not.toHaveBeenCalled();
|
515
|
+
clock.tick(5);
|
516
|
+
expect(delayedFn1).toHaveBeenCalled();
|
517
|
+
});
|
518
|
+
|
519
|
+
it("correctly schedules functions while the Clock is advancing", function() {
|
520
|
+
var delayedFn1 = jasmine.createSpy('delayedFn1'),
|
521
|
+
delayedFn2 = jasmine.createSpy('delayedFn2'),
|
522
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
523
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
524
|
+
clock = new jasmineUnderTest.Clock({setTimeout: function() {}}, function () { return delayedFunctionScheduler; }, mockDate);
|
525
|
+
|
526
|
+
delayedFn1.and.callFake(function() { clock.setTimeout(delayedFn2, 0); });
|
527
|
+
clock.install();
|
528
|
+
clock.setTimeout(delayedFn1, 5);
|
529
|
+
|
530
|
+
clock.tick(5);
|
531
|
+
expect(delayedFn1).toHaveBeenCalled();
|
532
|
+
expect(delayedFn2).not.toHaveBeenCalled();
|
533
|
+
|
534
|
+
clock.tick();
|
535
|
+
expect(delayedFn2).toHaveBeenCalled();
|
536
|
+
});
|
537
|
+
|
538
|
+
it("correctly calls functions scheduled while the Clock is advancing", function() {
|
539
|
+
var delayedFn1 = jasmine.createSpy('delayedFn1'),
|
540
|
+
delayedFn2 = jasmine.createSpy('delayedFn2'),
|
541
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
542
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
543
|
+
clock = new jasmineUnderTest.Clock({setTimeout: function() {}}, function () { return delayedFunctionScheduler; }, mockDate);
|
544
|
+
|
545
|
+
delayedFn1.and.callFake(function() { clock.setTimeout(delayedFn2, 1); });
|
546
|
+
clock.install();
|
547
|
+
clock.setTimeout(delayedFn1, 5);
|
548
|
+
|
549
|
+
clock.tick(6);
|
550
|
+
expect(delayedFn1).toHaveBeenCalled();
|
551
|
+
expect(delayedFn2).toHaveBeenCalled();
|
552
|
+
});
|
553
|
+
|
554
|
+
it("correctly schedules functions scheduled while the Clock is advancing but after the Clock is uninstalled", function() {
|
555
|
+
var delayedFn1 = jasmine.createSpy('delayedFn1'),
|
556
|
+
delayedFn2 = jasmine.createSpy('delayedFn2'),
|
557
|
+
delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
558
|
+
mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
|
559
|
+
clock = new jasmineUnderTest.Clock({setTimeout: function() {}}, function () { return delayedFunctionScheduler; }, mockDate);
|
560
|
+
|
561
|
+
delayedFn1.and.callFake(function() {
|
562
|
+
clock.uninstall();
|
563
|
+
clock.install();
|
564
|
+
clock.setTimeout(delayedFn2, 0);
|
565
|
+
});
|
566
|
+
|
567
|
+
clock.install();
|
568
|
+
clock.setTimeout(delayedFn1, 1);
|
569
|
+
|
570
|
+
clock.tick(1);
|
571
|
+
expect(delayedFn1).toHaveBeenCalled();
|
572
|
+
expect(delayedFn2).not.toHaveBeenCalled();
|
573
|
+
|
574
|
+
clock.tick(1);
|
575
|
+
expect(delayedFn2).toHaveBeenCalled();
|
576
|
+
});
|
577
|
+
|
578
|
+
it("does not mock the Date object by default", function() {
|
579
|
+
var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
580
|
+
global = {Date: Date},
|
581
|
+
mockDate = new jasmineUnderTest.MockDate(global),
|
582
|
+
clock = new jasmineUnderTest.Clock({setTimeout: setTimeout}, function () { return delayedFunctionScheduler; }, mockDate);
|
583
|
+
|
584
|
+
clock.install();
|
585
|
+
|
586
|
+
expect(global.Date).toEqual(Date);
|
587
|
+
|
588
|
+
var now = new global.Date().getTime();
|
589
|
+
|
590
|
+
clock.tick(50);
|
591
|
+
|
592
|
+
expect(new global.Date().getTime() - now).not.toEqual(50);
|
593
|
+
});
|
594
|
+
|
595
|
+
it("mocks the Date object and sets it to current time", function() {
|
596
|
+
var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
597
|
+
global = {Date: Date},
|
598
|
+
mockDate = new jasmineUnderTest.MockDate(global),
|
599
|
+
clock = new jasmineUnderTest.Clock({setTimeout: setTimeout}, function () { return delayedFunctionScheduler; }, mockDate);
|
600
|
+
|
601
|
+
clock.install().mockDate();
|
602
|
+
|
603
|
+
var now = new global.Date().getTime();
|
604
|
+
|
605
|
+
clock.tick(50);
|
606
|
+
|
607
|
+
expect(new global.Date().getTime() - now).toEqual(50);
|
608
|
+
|
609
|
+
var timeoutDate = 0;
|
610
|
+
clock.setTimeout(function() {
|
611
|
+
timeoutDate = new global.Date().getTime();
|
612
|
+
}, 100);
|
613
|
+
|
614
|
+
clock.tick(100);
|
615
|
+
|
616
|
+
expect(timeoutDate - now).toEqual(150);
|
617
|
+
});
|
618
|
+
|
619
|
+
it("mocks the Date object and sets it to a given time", function() {
|
620
|
+
var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
621
|
+
global = {Date: Date},
|
622
|
+
mockDate = new jasmineUnderTest.MockDate(global),
|
623
|
+
clock = new jasmineUnderTest.Clock({setTimeout: setTimeout}, function () { return delayedFunctionScheduler; }, mockDate),
|
624
|
+
baseTime = new Date(2013, 9, 23);
|
625
|
+
|
626
|
+
|
627
|
+
clock.install().mockDate(baseTime);
|
628
|
+
|
629
|
+
var now = new global.Date().getTime();
|
630
|
+
|
631
|
+
expect(now).toEqual(baseTime.getTime());
|
632
|
+
|
633
|
+
clock.tick(50);
|
634
|
+
|
635
|
+
expect(new global.Date().getTime()).toEqual(baseTime.getTime() + 50);
|
636
|
+
|
637
|
+
var timeoutDate = 0;
|
638
|
+
clock.setTimeout(function() {
|
639
|
+
timeoutDate = new global.Date().getTime();
|
640
|
+
}, 100);
|
641
|
+
|
642
|
+
clock.tick(100);
|
643
|
+
|
644
|
+
expect(timeoutDate).toEqual(baseTime.getTime() + 150);
|
645
|
+
});
|
646
|
+
|
647
|
+
it("mocks the Date object and updates the date per delayed function", function () {
|
648
|
+
var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
649
|
+
global = {Date: Date},
|
650
|
+
mockDate = new jasmineUnderTest.MockDate(global),
|
651
|
+
clock = new jasmineUnderTest.Clock({setTimeout: setTimeout}, function () { return delayedFunctionScheduler; }, mockDate),
|
652
|
+
baseTime = new Date();
|
653
|
+
|
654
|
+
clock.install().mockDate(baseTime);
|
655
|
+
|
656
|
+
var actualTimes = [];
|
657
|
+
var pushCurrentTime = function() { actualTimes.push(global.Date().getTime()); };
|
658
|
+
delayedFunctionScheduler.scheduleFunction(pushCurrentTime);
|
659
|
+
delayedFunctionScheduler.scheduleFunction(pushCurrentTime, 1);
|
660
|
+
delayedFunctionScheduler.scheduleFunction(pushCurrentTime, 3);
|
661
|
+
|
662
|
+
clock.tick(1);
|
663
|
+
expect(global.Date().getTime()).toEqual(baseTime.getTime() + 1);
|
664
|
+
|
665
|
+
clock.tick(3);
|
666
|
+
expect(global.Date().getTime()).toEqual(baseTime.getTime() + 4);
|
667
|
+
|
668
|
+
clock.tick(1);
|
669
|
+
expect(global.Date().getTime()).toEqual(baseTime.getTime() + 5);
|
670
|
+
|
671
|
+
expect(actualTimes).toEqual([baseTime.getTime(), baseTime.getTime() + 1, baseTime.getTime() + 3]);
|
672
|
+
})
|
673
|
+
|
674
|
+
it('correctly clears a scheduled timeout while the Clock is advancing', function () {
|
675
|
+
var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
676
|
+
global = {Date: Date, setTimeout: undefined},
|
677
|
+
mockDate = new jasmineUnderTest.MockDate(global),
|
678
|
+
clock = new jasmineUnderTest.Clock(global, function () { return delayedFunctionScheduler; }, mockDate);
|
679
|
+
|
680
|
+
clock.install();
|
681
|
+
|
682
|
+
var timerId2;
|
683
|
+
|
684
|
+
global.setTimeout(function () {
|
685
|
+
global.clearTimeout(timerId2);
|
686
|
+
}, 100);
|
687
|
+
|
688
|
+
timerId2 = global.setTimeout(fail, 100);
|
689
|
+
|
690
|
+
clock.tick(100);
|
691
|
+
});
|
692
|
+
|
693
|
+
it('correctly clears a scheduled interval while the Clock is advancing', function () {
|
694
|
+
var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
|
695
|
+
global = {Date: Date, setTimeout: undefined},
|
696
|
+
mockDate = new jasmineUnderTest.MockDate(global),
|
697
|
+
clock = new jasmineUnderTest.Clock(global, function () { return delayedFunctionScheduler; }, mockDate);
|
698
|
+
|
699
|
+
clock.install();
|
700
|
+
|
701
|
+
var timerId2;
|
702
|
+
var timerId1 = global.setInterval(function () {
|
703
|
+
global.clearInterval(timerId2);
|
704
|
+
}, 100);
|
705
|
+
|
706
|
+
timerId2 = global.setInterval(fail, 100);
|
707
|
+
|
708
|
+
clock.tick(400);
|
709
|
+
});
|
710
|
+
});
|