|
1 |
| -const index = require('../index'); |
| 1 | +const timeJumpRequire = require('../timeJump'); |
2 | 2 |
|
3 | 3 | jest.mock('../timeJump');
|
| 4 | +const timeJump = jest.fn(); |
| 5 | +timeJumpRequire.mockReturnValue(timeJump); |
| 6 | + |
| 7 | +const index = require('../index'); |
4 | 8 |
|
5 | 9 | describe('unit testing for index.js', () => {
|
6 |
| - test('index.js should be exporting the linkFiber, timeJump, getTree methods', () => { |
7 |
| - expect(typeof index.linkFiber).toBe('function'); |
8 |
| - expect(typeof index.timeJump).toBe('function'); |
9 |
| - expect(typeof index.getTree).toBe('function'); |
| 10 | + test('index.js should be exporting a function', () => { |
| 11 | + expect(typeof index).toBe('function'); |
10 | 12 | });
|
11 | 13 |
|
12 |
| - test('index.js should be listening to the window for the jumpToSnap message', (done) => { |
| 14 | + test('index.js should be calling timeJump for every jumpToSnap message', (done) => { |
13 | 15 | const calls = 10;
|
14 | 16 | let count = 0;
|
15 | 17 | global.addEventListener('message', ({ data: { action } }) => {
|
16 |
| - switch (action) { |
17 |
| - case 'jumpToSnap': |
18 |
| - count += 1; |
19 |
| - expect(index.timeJump.mock.calls.length).toBe(count); |
20 |
| - if (count === calls) done(); |
21 |
| - break; |
22 |
| - default: |
23 |
| - break; |
24 |
| - } |
25 |
| - }); |
26 |
| - [...Array(calls).keys()].forEach(() => global.postMessage({ action: 'jumpToSnap', payload: ['test'] }, '*')); |
27 |
| - }); |
28 |
| - test('setLock message should change the mode object to the given payload', (done) => { |
29 |
| - const mode = { paused: false }; |
30 |
| - const action = 'setLock'; |
31 |
| - const payloads = [true, false, true, false, true, false, false]; |
32 |
| - let count = 0; |
33 |
| - const calls = payloads.length; |
34 |
| - global.addEventListener('message', ({ data: { action: actionReceived, payload } }) => { |
35 |
| - switch (actionReceived) { |
36 |
| - case 'setLock': |
37 |
| - mode.locked = payload; |
38 |
| - expect(mode.locked).toBe(payloads[count]); |
39 |
| - count += 1; |
40 |
| - if (calls === count) done(); |
41 |
| - break; |
42 |
| - default: |
43 |
| - break; |
44 |
| - } |
45 |
| - }); |
46 |
| - payloads.forEach(payload => global.postMessage({ action, payload }, '*')); |
47 |
| - }); |
48 |
| - |
49 |
| - test('setPause message should change the mode object to the given payload', (done) => { |
50 |
| - const mode = { paused: false }; |
51 |
| - const action = 'setPause'; |
52 |
| - const payloads = [true, false, true, false, true, false, false]; |
53 |
| - let count = 0; |
54 |
| - const calls = payloads.length; |
55 |
| - global.addEventListener('message', ({ data: { action: actionReceived, payload } }) => { |
56 |
| - switch (actionReceived) { |
57 |
| - case 'setPause': |
58 |
| - mode.locked = payload; |
59 |
| - expect(mode.locked).toBe(payloads[count]); |
60 |
| - count += 1; |
61 |
| - if (calls === count) done(); |
62 |
| - break; |
63 |
| - default: |
64 |
| - break; |
| 18 | + if (action === 'jumpToSnap') { |
| 19 | + count += 1; |
| 20 | + expect(timeJump.mock.calls.length).toBe(count); |
| 21 | + if (count === calls) done(); |
65 | 22 | }
|
66 | 23 | });
|
67 |
| - payloads.forEach(payload => global.postMessage({ action, payload }, '*')); |
| 24 | + for (let i = 0; i < calls; i += 1) { |
| 25 | + global.postMessage({ action: 'jumpToSnap', payload: ['test'] }, '*'); |
| 26 | + } |
68 | 27 | });
|
69 | 28 | });
|
0 commit comments