-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
69 lines (58 loc) · 2.28 KB
/
index.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
const { Timeout, Interval } = require(".");
require("jest");
beforeEach(() => {
jest.useFakeTimers();
});
describe("state tests", () => {
test("state is set to 0 after creation of timer", () => {
const to = new Timeout(() => { }, 0);
expect(to.state).toBe(0);
});
test("state is set to 1 when timer has been started", () => {
const waitTime = 1000;
const callback = jest.fn();
const to = new Timeout(callback, waitTime);
to.start();
expect(to.state).toBe(1);
});
test("state is set to 3 and callback is called after timer is completed", () => {
const waitTime = 1000;
const callback = jest.fn();
const to = new Timeout(callback, waitTime);
to.start();
jest.runAllTimers();
expect(to.state).toBe(3);
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalledTimes(1);
});
});
describe("time start tests", () => {
test("timeout is not started when autostart is false", () => {
const to = new Timeout(() => { }, 0, false);
expect(setTimeout).toHaveBeenCalledTimes(0);
});
test("timeout is not started when autostart is not specified", () => {
const to = new Timeout(() => { }, 0);
expect(setTimeout).toHaveBeenCalledTimes(0);
});
test("timeout is started when autostart is true", () => {
const waitTime = 1000;
const to = new Timeout(() => { }, waitTime, true);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), waitTime);
});
test("interval is not started when autostart is false", () => {
const to = new Interval(() => to.stop(), 1, false);
expect(setInterval).toHaveBeenCalledTimes(0);
});
test("interval is not started when autostart is not specified", () => {
const to = new Interval(() => to.stop(), 1);
expect(setInterval).toHaveBeenCalledTimes(0);
});
test("interval is started when autostart is true", () => {
const waitTime = 1000;
const to = new Interval(() => to.stop(), waitTime, true);
expect(setInterval).toHaveBeenCalledTimes(1);
expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), waitTime);
});
});