-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathfake-clock-integration-test.js
128 lines (100 loc) · 3.74 KB
/
fake-clock-integration-test.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict";
var jsdom;
if (typeof require === "function" && typeof module === "object") {
try {
jsdom = require("jsdom");
} catch (e) {
// ignored
}
}
if (!jsdom) {
// eslint-disable-next-line no-console
console.warn("JSDOM is not supported in the current environment.");
return;
}
var assert = require("@sinonjs/referee-sinon").assert;
var FakeTimers = require("../src/fake-timers-src");
var sinon = require("@sinonjs/referee-sinon").sinon;
describe("withGlobal", function () {
var jsdomGlobal, withGlobal, timers;
beforeEach(function () {
var dom = new jsdom.JSDOM("", { runScripts: "dangerously" });
jsdomGlobal = dom.window;
withGlobal = FakeTimers.withGlobal(jsdomGlobal);
timers = Object.keys(withGlobal.timers);
});
it("matches the normal FakeTimers API", function () {
assert.equals(Object.keys(withGlobal), Object.keys(FakeTimers));
});
it("should support basic setTimeout", function () {
var clock = withGlobal.install({ toFake: timers });
var stub = sinon.stub();
jsdomGlobal.setTimeout(stub, 5);
clock.tick(5);
assert(stub.calledOnce);
clock.uninstall();
});
it("Date is instanceof itself", function () {
assert(new jsdomGlobal.Date() instanceof jsdomGlobal.Date);
var clock = withGlobal.install({ toFake: timers });
assert(new jsdomGlobal.Date() instanceof jsdomGlobal.Date);
clock.uninstall();
});
});
describe("globally configured browser objects", function () {
var withGlobal, originalDescriptors;
// We use a set up function instead of beforeEach to avoid Mocha's check leaks detector
function setUpGlobal() {
// Configuration taken from from here https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md
var dom = new jsdom.JSDOM("<!doctype html><html><body></body></html>");
var window = dom.window;
function copyProps(src, target) {
originalDescriptors = Object.getOwnPropertyDescriptors(target);
Object.defineProperties(
target,
Object.getOwnPropertyDescriptors(src)
);
Object.defineProperties(target, originalDescriptors);
}
global.window = window;
global.document = window.document;
global.navigator = window.navigator;
global.requestAnimationFrame = function (callback) {
return setTimeout(callback, 0);
};
global.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
copyProps(window, global);
withGlobal = FakeTimers.withGlobal(global);
}
function tearDownGlobal() {
var originalDescriptorNames = Object.keys(originalDescriptors);
var windowDescriptorNames = Object.getOwnPropertyNames(global.window);
windowDescriptorNames.forEach(function (descriptorName) {
if (!originalDescriptorNames.includes(descriptorName)) {
delete global[descriptorName];
}
});
delete global.window;
delete global.document;
delete global.navigator;
delete global.requestAnimationFrame;
delete global.cancelAnimationFrame;
}
it("correctly instantiates and tears down", function () {
setUpGlobal();
try {
var mockNow = new Date("1990-1-1");
var clock = withGlobal.install({
now: mockNow,
});
assert.equals(new Date(Date.now()), mockNow);
assert.equals(new Date(), mockNow);
clock.uninstall();
assert(new Date().valueOf() !== mockNow.valueOf());
} finally {
tearDownGlobal();
}
});
});