Skip to content

Commit f75343b

Browse files
committed
time jump test rewrote
1 parent 2d7820d commit f75343b

File tree

1 file changed

+62
-37
lines changed

1 file changed

+62
-37
lines changed

package/__tests__/timeJump.test.js

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,78 @@
1-
const timeJumpExport = require('../timeJump');
1+
const timeJumpRequire = require('../timeJump');
2+
3+
class Component {
4+
constructor(mockfn) {
5+
this.mockfn = mockfn;
6+
}
7+
8+
setState(state, func = () => { }) {
9+
this.mockfn(state);
10+
func();
11+
}
12+
}
13+
14+
class FiberNode {
15+
constructor(mockfn, state) {
16+
this.state = state;
17+
this.children = [];
18+
this.component = new Component(mockfn);
19+
}
20+
}
221

322
describe('unit testing for timeJump.js', () => {
423
let timeJump;
524
let snapShot;
625
let mode;
726
let mockFuncs;
8-
const count = 3;
927

1028
beforeEach(() => {
11-
snapShot = [];
1229
mode = { jumping: false };
1330
mockFuncs = [];
14-
timeJump = timeJumpExport(snapShot, mode);
31+
for (let i = 0; i < 4; i += 1) mockFuncs.push(jest.fn());
32+
33+
const tree = new FiberNode(mockFuncs[0]);
34+
tree.children = [
35+
new FiberNode(mockFuncs[1]),
36+
new FiberNode(mockFuncs[2]),
37+
new FiberNode(mockFuncs[3]),
38+
];
1539

16-
const createSetStateAsync = i => state => new Promise((resolve) => {
17-
mockFuncs[i](state);
18-
resolve();
40+
snapShot = { tree };
41+
timeJump = timeJumpRequire(snapShot, mode);
42+
});
43+
44+
test('calling the initial require should return a function', () => {
45+
expect(typeof timeJumpRequire).toBe('function');
46+
});
47+
48+
describe('testing iteration through snapshot tree', () => {
49+
const states = ['root', 'firstChild', 'secondChild', 'thirdChild'];
50+
const target = new FiberNode(null, states[0]);
51+
target.children = [
52+
new FiberNode(null, states[1]),
53+
new FiberNode(null, states[2]),
54+
new FiberNode(null, states[3]),
55+
];
56+
57+
beforeEach(() => {
58+
timeJump(target);
59+
});
60+
test('timeJump should call setState on each state in origin', () => {
61+
mockFuncs.forEach(mockFunc => expect(mockFunc.mock.calls.length).toBe(1));
1962
});
2063

21-
for (let i = 0; i < count; i += 1) {
22-
mockFuncs.push(jest.fn());
23-
snapShot.push({ setStateAsync: createSetStateAsync(i) });
24-
}
64+
test('timeJump should pass target state to origin setState', () => {
65+
mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[0][0]).toBe(states[i]));
66+
});
2567
});
2668

27-
// test('calling the initial require should return a function', () => {
28-
// expect(typeof timeJump).toBe('function');
29-
// });
30-
31-
// test('timeJump should iterate through snapshot and call setStateAsync on each state', () => {
32-
// const calls = 10;
33-
// for (let i = 1; i <= calls; i += 1) {
34-
// timeJump(Array(count).fill('test'));
35-
// mockFuncs.forEach(mockFunc => expect(mockFunc.mock.calls.length).toBe(i));
36-
// }
37-
// });
38-
39-
// test('timeJump should pass the state from new snapshot to setStateAsync', () => {
40-
// const newSnapShot = [];
41-
// for (let i = 0; i < count; i += 1) {
42-
// newSnapShot.push(`testState${i}`);
43-
// }
44-
// timeJump(newSnapShot);
45-
// mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[0][0]).toBe(`testState${i}`));
46-
47-
// for (let i = 0; i < count; i += 1) {
48-
// newSnapShot[i] = { testkey: `testval${i}` };
49-
// }
50-
// timeJump(newSnapShot);
51-
// mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[1][0]).toEqual({ testkey: `testval${i}` }));
52-
// });
69+
test('jumping mode should be set while timeJumping', () => {
70+
const logMode = jest.fn();
71+
logMode.mockImplementation(() => expect(mode.jumping).toBe(true));
72+
73+
snapShot.tree = new FiberNode(logMode);
74+
const target = new FiberNode(null, 'test');
75+
timeJump(target);
76+
expect(logMode).toHaveBeenCalled();
77+
});
5378
});

0 commit comments

Comments
 (0)