-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
108 lines (101 loc) · 2.76 KB
/
Copy pathindex.test.js
File metadata and controls
108 lines (101 loc) · 2.76 KB
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
'use strict';
import createCountdown from ".";
describe('Create Timer', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.clearAllTimers();
});
it('counts down to time set by set method', () => {
const listenCallback = jest.fn();
const doneCallback = jest.fn();
const timer = createCountdown({ h: 1, m: 30, s: 0 }, {
listen: listenCallback,
done: doneCallback
});
timer.set({ h: 0, m: 0, s: 30});
timer.start();
jest.runAllTimers();
expect(listenCallback).toHaveBeenCalledTimes(31);
});
it('calls listen and done after count down is complete', () => {
const listenCallback = jest.fn();
const doneCallback = jest.fn();
const timer = createCountdown({ h: 1, m: 30, s: 0 }, {
listen: listenCallback,
done: doneCallback
});
timer.start();
jest.runAllTimers();
expect(listenCallback).toHaveBeenCalledWith({
h: 0,
hh: '00',
m: 0,
mm: '00',
s: 0,
ss: '00'
});
expect(doneCallback).toHaveBeenCalled();
});
it('calls listen after one second countdown', () => {
const listenCallback = jest.fn();
const doneCallback = jest.fn();
const timer = createCountdown({ h: 1, m: 30, s: 0 }, {
listen: listenCallback,
done: doneCallback
});
timer.start();
jest.advanceTimersByTime(1000);
expect(listenCallback).toHaveBeenCalledTimes(2);
expect(listenCallback.mock.calls[1][0]).toMatchInlineSnapshot(`
Object {
"h": 1,
"hh": "01",
"m": 29,
"mm": "29",
"s": 59,
"ss": "59",
}
`);
expect(doneCallback).not.toHaveBeenCalled();
});
it('calls clearInterval after initiating stop', () => {
const listenCallback = jest.fn();
const doneCallback = jest.fn();
clearInterval = jest.fn();
const timer = createCountdown({ h: 1, m: 30, s: 0 }, {
listen: listenCallback,
done: doneCallback
});
timer.start();
jest.advanceTimersByTime(1000);
timer.stop();
expect(clearInterval).toHaveBeenCalled();
expect(listenCallback).toHaveBeenCalledTimes(2);
});
it('calls clearInterval and listen after initiating reset', () => {
const listenCallback = jest.fn();
const doneCallback = jest.fn();
clearInterval = jest.fn();
const timer = createCountdown({ h: 1, m: 30, s: 0 }, {
listen: listenCallback,
done: doneCallback
});
timer.start();
jest.advanceTimersByTime(1000);
timer.reset();
expect(clearInterval).toHaveBeenCalled();
expect(listenCallback).toHaveBeenCalledTimes(3);
expect(listenCallback.mock.calls[2][0]).toMatchInlineSnapshot(`
Object {
"h": 1,
"hh": "01",
"m": 30,
"mm": "30",
"s": 0,
"ss": "00",
}
`);
});
});