Skip to content

Commit b2625ce

Browse files
authored
Merge pull request #14 from oslabs-beta/rydang/jest-timejump
test for timeJump.js
2 parents fc10c10 + 9c90e4c commit b2625ce

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
dist/
44
extension/bundle.js
55
package/react-time-travel-1.0.1.tgz
6+
tictactoe
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const index = require('../package/index');
1+
const index = require('../index');
22

3-
jest.mock('../package/timeJump');
3+
jest.mock('../timeJump');
44

55
describe('unit testing for index.js', () => {
66
test('index.js should be exporting the linkState and timeJump methods', () => {

package/__tests__/timeJump.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const timeJumpExport = require('../timeJump');
2+
3+
describe('unit testing for timeJump.js', () => {
4+
let timeJump;
5+
let snapShot;
6+
let mode;
7+
let mockFuncs;
8+
const count = 3;
9+
10+
beforeEach(() => {
11+
snapShot = [];
12+
mode = { jumping: false };
13+
mockFuncs = [];
14+
timeJump = timeJumpExport(snapShot, mode);
15+
16+
const createSetStateAsync = (i) => {
17+
return (state) => {
18+
return new Promise((resolve) => {
19+
mockFuncs[i](state);
20+
resolve();
21+
});
22+
};
23+
};
24+
25+
for (let i = 0; i < count; i += 1) {
26+
mockFuncs.push(jest.fn());
27+
snapShot.push({ setStateAsync: createSetStateAsync(i) });
28+
}
29+
});
30+
31+
test('calling the initial require should return a function', () => {
32+
expect(typeof timeJump).toBe('function');
33+
});
34+
35+
test('timeJump should iterate through snapshot and call setStateAsync on each state', () => {
36+
const calls = 10;
37+
for (let i = 1; i <= calls; i += 1) {
38+
timeJump(Array(count).fill('test'));
39+
mockFuncs.forEach(mockFunc => expect(mockFunc.mock.calls.length).toBe(i));
40+
}
41+
});
42+
test('timeJump should pass the state from new snapshot to setStateAsync', () => {
43+
const newSnapShot = [];
44+
for (let i = 0; i < count; i += 1) {
45+
newSnapShot.push(`testState${i}`);
46+
}
47+
timeJump(newSnapShot);
48+
mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[0][0]).toBe(`testState${i}`));
49+
50+
for (let i = 0; i < count; i += 1) {
51+
newSnapShot[i] = { testkey: `testval${i}` };
52+
}
53+
timeJump(newSnapShot);
54+
mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[1][0]).toEqual({ testkey: `testval${i}` }));
55+
});
56+
});

0 commit comments

Comments
 (0)