forked from CodingTrain/TestingTestTest
-
Notifications
You must be signed in to change notification settings - Fork 12
/
clock.test.js
executable file
·44 lines (31 loc) · 1.02 KB
/
clock.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
/* Based on Jest documentation: https://jestjs.io/docs/en/timer-mocks */
'use strict';
const { countdown } = require('./clock');
const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
// Jest can fake the passage of time for javascript timers
jest.useFakeTimers();
/**
* Countdown to be defined
*/
test('Countdown function exists', countdownExistsTest);
function countdownExistsTest(){
expect(countdown).toBeDefined();
}
/**
* Countdown to delay execution of callback by 2000 millis
*/
test('tests delay of countdown calls by 2 seconds', countdownDelayTest);
function countdownDelayTest() {
const t = 2000;
const callback = jest.fn();
countdown(t, 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.advanceTimersByTime(t);
// Now our callback should have been called!
expect(callback).toBeCalled();
expect(callback).toHaveBeenCalledTimes(1);
}