|
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 | +} |
2 | 21 |
|
3 | 22 | describe('unit testing for timeJump.js', () => {
|
4 | 23 | let timeJump;
|
5 | 24 | let snapShot;
|
6 | 25 | let mode;
|
7 | 26 | let mockFuncs;
|
8 |
| - const count = 3; |
9 | 27 |
|
10 | 28 | beforeEach(() => {
|
11 |
| - snapShot = []; |
12 | 29 | mode = { jumping: false };
|
13 | 30 | 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 | + ]; |
15 | 39 |
|
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)); |
19 | 62 | });
|
20 | 63 |
|
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 | + }); |
25 | 67 | });
|
26 | 68 |
|
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 | + }); |
53 | 78 | });
|
0 commit comments