|
| 1 | +/* eslint-disable @typescript-eslint/no-empty-function */ |
| 2 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 3 | +/* eslint-disable max-classes-per-file */ |
| 4 | +import timeJumpRequire from '../timeJump'; |
| 5 | +import componentActionsRecord from '../masterState'; |
| 6 | +// import { ComponentData } from '../types/backendTypes'; |
| 7 | +// const timeJumpRequire = require('../timeJump'); |
| 8 | + |
| 9 | +class Component { |
| 10 | + mockfn: (state) => void |
| 11 | + |
| 12 | + state: Record<string, unknown> |
| 13 | + |
| 14 | + componentData: {} |
| 15 | + |
| 16 | + constructor(mockfn) { |
| 17 | + this.mockfn = mockfn; |
| 18 | + } |
| 19 | + |
| 20 | + setState(state, func = () => { }) { |
| 21 | + this.mockfn(state); |
| 22 | + func(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class FiberNode { |
| 27 | + private state: Record<null, unknown>; |
| 28 | + |
| 29 | + children: FiberNode[]; |
| 30 | + |
| 31 | + component: Component; |
| 32 | + |
| 33 | + componentData: { |
| 34 | + index?: number; |
| 35 | + }; |
| 36 | + |
| 37 | + constructor(mockfn, state) { |
| 38 | + this.state = state; |
| 39 | + this.children = []; |
| 40 | + this.component = new Component(mockfn); |
| 41 | + this.componentData = { index: 0 }; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +describe('unit testing for timeJump.ts', () => { |
| 46 | + let timeJump: (target) => void; |
| 47 | + let snapShot: Record<string, FiberNode>; |
| 48 | + let mode; |
| 49 | + let mockFuncs; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + const mockFunc = jest.fn(); |
| 53 | + mode = { jumping: false }; |
| 54 | + mockFuncs = []; |
| 55 | + for (let i = 0; i < 4; i += 1) mockFuncs.push(mockFunc); |
| 56 | + |
| 57 | + const tree: FiberNode = new FiberNode(mockFuncs[0], '*'); |
| 58 | + tree.children = [ |
| 59 | + new FiberNode(mockFuncs[1], '*'), |
| 60 | + new FiberNode(mockFuncs[2], '*'), |
| 61 | + new FiberNode(mockFuncs[3], '*'), |
| 62 | + ]; |
| 63 | + |
| 64 | + snapShot = { tree }; |
| 65 | + timeJump = timeJumpRequire(snapShot, mode); |
| 66 | + // mockFunc.mockClear() |
| 67 | + }); |
| 68 | + test('calling the initial require should return a function', () => { |
| 69 | + expect(typeof timeJumpRequire).toBe('function'); |
| 70 | + }); |
| 71 | + |
| 72 | + // xdescribe('testing iteration through snapshot tree', () => { |
| 73 | + // const states = ['root', 'firstChild', 'secondChild', 'thirdChild']; |
| 74 | + // const target = new FiberNode(null, states[0]); |
| 75 | + // target.children = [ |
| 76 | + // new FiberNode(null, states[1]), |
| 77 | + // new FiberNode(null, states[2]), |
| 78 | + // new FiberNode(null, states[3]), |
| 79 | + // ]; |
| 80 | + |
| 81 | + // target.componentData = {index: 0} |
| 82 | + |
| 83 | + // beforeEach((): void => { |
| 84 | + // timeJump(target); |
| 85 | + // }); |
| 86 | + // test('timeJump should call setState on each state in origin', () => { |
| 87 | + // mockFuncs.forEach(mockFunc => expect(mockFunc.mock.calls.length).toBe(1)); |
| 88 | + // }); |
| 89 | + |
| 90 | + // test('timeJump should pass target state to origin setState', () => { |
| 91 | + // mockFuncs.forEach((mockFunc, i) => expect(mockFunc.mock.calls[0][0]).toBe(states[i])); |
| 92 | + // }); |
| 93 | + // }); |
| 94 | + |
| 95 | + test('jumping mode should be set while timeJumping', () => { |
| 96 | + mode = { jumping: true }; |
| 97 | + const logMode = jest.fn(); |
| 98 | + logMode.mockImplementation(() => expect(mode.jumping).toBe(true)); |
| 99 | + |
| 100 | + snapShot.tree = new FiberNode(logMode, null); |
| 101 | + const target = new FiberNode(null, 'test'); |
| 102 | + logMode(target); |
| 103 | + expect(logMode).toHaveBeenCalled(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments