-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclear-interval-async.spec.ts
96 lines (88 loc) · 3.13 KB
/
clear-interval-async.spec.ts
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
import { install, InstalledClock } from "@sinonjs/fake-timers";
import sinon from "sinon";
import { strict as assert } from "assert";
import {
setIntervalAsync as setIntervalAsyncDynamic,
clearIntervalAsync as clearIntervalAsyncDynamic,
SetIntervalAsyncTimer,
} from "set-interval-async/dynamic";
import {
setIntervalAsync as setIntervalAsyncFixed,
clearIntervalAsync as clearIntervalAsyncFixed,
} from "set-interval-async/fixed";
for (const [strategy, setIntervalAsync, clearIntervalAsync] of [
["Dynamic", setIntervalAsyncDynamic, clearIntervalAsyncDynamic],
["Fixed", setIntervalAsyncFixed, clearIntervalAsyncFixed],
] as const) {
describe(`[${strategy}] clearIntervalAsync`, () => {
let clock: InstalledClock;
beforeEach(() => {
clock = install();
});
afterEach(() => {
clock.uninstall();
});
it("should fail if timer is not an instance of SetIntervalAsyncTimer", async () => {
const invalidTimers = [null, undefined, 0, "str", {}, []];
for (const invalidTimer of invalidTimers) {
try {
await clearIntervalAsync(invalidTimer as SetIntervalAsyncTimer<[]>);
assert.fail("Did not throw");
} catch (err: unknown) {
assert.ok(err instanceof TypeError);
}
}
});
it(`should stop running successfully before the first iteration`, async () => {
const intervalMs = 100;
const handler = sinon.fake(async () => {
/* empty */
});
const timer = setIntervalAsync(handler, intervalMs);
await clearIntervalAsync(timer);
assert.equal(handler.callCount, 0);
});
it(`should stop running successfully after the first iteration`, async () => {
const iterationCount = 10;
const intervalMs = 100;
const handler = sinon.fake(async () => {
/* empty */
});
const timer = setIntervalAsync(handler, intervalMs);
for (let iteration = 1; iteration <= iterationCount; ++iteration) {
await clock.nextAsync();
}
await clearIntervalAsync(timer);
assert.equal(handler.callCount, iterationCount);
await clock.nextAsync();
assert.equal(handler.callCount, iterationCount);
});
it(`should stop running successfully in the middle of an iteration`, async () => {
const intervalMs = 1000;
const handler = sinon.fake(async () => {
await new Promise((resolve) => clock.setTimeout(resolve, 100));
await new Promise((resolve) => clock.setTimeout(resolve, 100));
});
const timer = setIntervalAsync(handler, intervalMs);
await clock.nextAsync();
await clock.nextAsync();
const cleared = clearIntervalAsync(timer);
await clock.nextAsync();
await cleared;
});
it(`should throw if the last iteration throws`, async () => {
const intervalMs = 100;
const handler = sinon.fake(async () => {
throw new Error("Some Error");
});
const timer = setIntervalAsync(handler, intervalMs);
await clock.nextAsync();
try {
await clearIntervalAsync(timer);
assert.fail("Did not throw.");
} catch (_) {
/* empty */
}
});
});
}