-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosure.spec.js
63 lines (48 loc) · 1.84 KB
/
closure.spec.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
const {
printValues,
printValuesIIFE,
printValuesWithLet
} = require('./closure');
describe('Print the numbers from 1 to n with a delay of 1 second each', () => {
jest.useFakeTimers();
it('waits 5 calls before ending the function', () => {
printValues();
expect(setTimeout).toHaveBeenCalledTimes(5);
});
it('should print undefined', () => {
const callback = jest.fn(x => x);
printValues(callback);
// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
// Fast-forward until all timers have been executed
jest.runAllTimers();
// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalledTimes(5);
expect(typeof callback.mock.results[0].value).toBe('undefined');
});
it('should print numbers from 1 to 5', () => {
const callback = jest.fn(x => x);
printValuesIIFE(callback);
// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
// Fast-forward until all timers have been executed
jest.runAllTimers();
// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalledTimes(5);
expect(typeof callback.mock.results[0].value).toBe('number');
});
it('should print numbers from 1 to 5', () => {
const callback = jest.fn(x => x);
printValuesWithLet(callback);
// At this point in time, the callback should not have been called yet
expect(callback).not.toBeCalled();
// Fast-forward until all timers have been executed
jest.runAllTimers();
// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalledTimes(5);
expect(typeof callback.mock.results[0].value).toBe('number');
});
});