Skip to content

Commit 2d7820d

Browse files
committed
rewrote index.js test
1 parent 89ded7b commit 2d7820d

File tree

2 files changed

+15
-57
lines changed

2 files changed

+15
-57
lines changed

package/__mocks__/timeJump.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package/__tests__/index.test.js

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,28 @@
1-
const index = require('../index');
1+
const timeJumpRequire = require('../timeJump');
22

33
jest.mock('../timeJump');
4+
const timeJump = jest.fn();
5+
timeJumpRequire.mockReturnValue(timeJump);
6+
7+
const index = require('../index');
48

59
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');
1012
});
1113

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) => {
1315
const calls = 10;
1416
let count = 0;
1517
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();
6522
}
6623
});
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+
}
6827
});
6928
});

0 commit comments

Comments
 (0)