|
| 1 | +import createComponentActionsRecord from '../controllers/createTree/createComponentActionsRecord'; |
| 2 | +import { Fiber } from '../types/backendTypes'; |
| 3 | +import componentActionsRecord from '../models/masterState'; |
| 4 | + |
| 5 | +describe('master tree tests', () => { |
| 6 | + describe('createComponentActionsRecord', () => { |
| 7 | + let mockFiberNode: Fiber; |
| 8 | + beforeEach(() => { |
| 9 | + // create a mock Fiber node with relevant properties |
| 10 | + mockFiberNode = { |
| 11 | + sibling: null, |
| 12 | + stateNode: {}, |
| 13 | + child: null, |
| 14 | + memoizedState: null, |
| 15 | + elementType: {}, |
| 16 | + tag: 2, // IndeterminateComponent |
| 17 | + key: null, |
| 18 | + type: null, |
| 19 | + index: 0, |
| 20 | + memoizedProps: null, |
| 21 | + dependencies: null, |
| 22 | + _debugHookTypes: [], |
| 23 | + }; |
| 24 | + // clear the saved component actions record |
| 25 | + componentActionsRecord.clear(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should save a new component action record if the Fiber node is a stateful class component', () => { |
| 29 | + mockFiberNode.tag = 1; // ClassComponent |
| 30 | + mockFiberNode.stateNode = { |
| 31 | + state: { counter: 0 }, // a mock state object |
| 32 | + setState: jest.fn(), // a mock setState method |
| 33 | + }; |
| 34 | + createComponentActionsRecord(mockFiberNode); |
| 35 | + expect(componentActionsRecord.getComponentByIndex(0)).toBe(mockFiberNode.stateNode); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should save a new component action record if the Fiber node is a stateful class component with props', () => { |
| 39 | + mockFiberNode.tag = 1; // ClassComponent |
| 40 | + // a mock state object |
| 41 | + mockFiberNode.stateNode = { |
| 42 | + state: { counter: 0 }, |
| 43 | + props: { start: 0 }, |
| 44 | + setState: jest.fn(), // a mock setState method |
| 45 | + }; |
| 46 | + createComponentActionsRecord(mockFiberNode); |
| 47 | + expect(componentActionsRecord.getComponentByIndex(0)).toMatchObject({ |
| 48 | + props: mockFiberNode.stateNode.props, |
| 49 | + state: mockFiberNode.stateNode.state, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should save a new component action record if the Fiber node is a functional component with state', () => { |
| 54 | + mockFiberNode.tag = 0; // FunctionComponent |
| 55 | + mockFiberNode.memoizedState = { |
| 56 | + queue: [{}, { state: { value: 'test' } }], // a mock memoizedState object |
| 57 | + }; |
| 58 | + createComponentActionsRecord(mockFiberNode); |
| 59 | + expect(componentActionsRecord.getComponentByIndex(0)).toBe(mockFiberNode.memoizedState.queue); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should save multiple component action records when called multiple times with different Fiber nodes', () => { |
| 63 | + mockFiberNode.tag = 1; // ClassComponent |
| 64 | + mockFiberNode.stateNode = { |
| 65 | + state: { counter: 0 }, |
| 66 | + props: { start: 0 }, // a mock state object |
| 67 | + setState: jest.fn(), // a mock setState method |
| 68 | + }; |
| 69 | + createComponentActionsRecord(mockFiberNode); |
| 70 | + expect(componentActionsRecord.getComponentByIndex(0)).toMatchObject({ |
| 71 | + state: mockFiberNode.stateNode.state, |
| 72 | + props: mockFiberNode.stateNode.props, |
| 73 | + }); |
| 74 | + |
| 75 | + const mockFiberNode2: Fiber = { ...mockFiberNode }; |
| 76 | + mockFiberNode2.stateNode.props = { start: 1 }; // a different mock memoizedProps object |
| 77 | + createComponentActionsRecord(mockFiberNode2); |
| 78 | + expect(componentActionsRecord.getComponentByIndex(1)).toMatchObject({ |
| 79 | + state: mockFiberNode2.stateNode.state, |
| 80 | + props: mockFiberNode2.stateNode.props, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return the correct hooks array for a given component index', () => { |
| 85 | + const component1 = { state: 'dummy state', props: {} }; |
| 86 | + const component2 = { state: 'dummy state2', props: {} }; |
| 87 | + const component3 = { state: 'dummy state3', props: {} }; |
| 88 | + componentActionsRecord.saveNew(component1); |
| 89 | + componentActionsRecord.saveNew(component2); |
| 90 | + componentActionsRecord.saveNew(component3); |
| 91 | + // create a mock component action record |
| 92 | + const mockComponentActionRecord = { |
| 93 | + index: 0, |
| 94 | + hooks: ['mock hook 1', 'mock hook 2', 'mock hook 3'], |
| 95 | + }; |
| 96 | + componentActionsRecord.addOrUpdateComponent(mockComponentActionRecord); |
| 97 | + |
| 98 | + // call the getComponentByIndexHooks function with the mock index |
| 99 | + const hooksArray = componentActionsRecord.getComponentByIndexHooks(0); |
| 100 | + |
| 101 | + // assert that the returned hooks array matches the expected value |
| 102 | + expect(hooksArray).toEqual(mockComponentActionRecord.hooks); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not save a new component action record if the Fiber node is not a relevant component type', () => { |
| 106 | + mockFiberNode.tag = 4; // HostRoot |
| 107 | + createComponentActionsRecord(mockFiberNode); |
| 108 | + expect(componentActionsRecord.getAllComponents()).toHaveLength(0); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments