|
| 1 | +import Tree from '../../models/tree'; |
| 2 | +import routes from '../../models/routes'; |
| 3 | +import { ComponentData, Fiber } from '../../types/backendTypes'; |
| 4 | +import { FunctionComponent, ClassComponent, HostRoot } from '../../types/backendTypes'; |
| 5 | +import IncrementFunc from './IncrementFunc'; |
| 6 | +import IncrementClass from './IncrementClass'; |
| 7 | +import componentActionsRecord from '../../models/masterState'; |
| 8 | +import deepCopy from './deepCopy'; |
| 9 | + |
| 10 | +// ----------------------------TEST CASES FOR ROOT------------------------------ |
| 11 | +export const root: Fiber = { |
| 12 | + tag: HostRoot, |
| 13 | + elementType: null, |
| 14 | + sibling: null, |
| 15 | + stateNode: null, |
| 16 | + child: null, |
| 17 | + memoizedState: null, |
| 18 | + memoizedProps: null, |
| 19 | + actualDuration: 1, |
| 20 | + actualStartTime: 2, |
| 21 | + selfBaseDuration: 3, |
| 22 | + treeBaseDuration: 4, |
| 23 | + _debugHookTypes: null, |
| 24 | +}; |
| 25 | +export const rootPayload = new Tree('root', 'root'); |
| 26 | +rootPayload.route = routes.addRoute('http://localhost/'); |
| 27 | + |
| 28 | +// ----------------------TEST CASE FOR FUNCTIONAL COMPONENT--------------------- |
| 29 | +export const functionalComponent: Fiber = { |
| 30 | + tag: FunctionComponent, |
| 31 | + elementType: IncrementFunc, |
| 32 | + sibling: null, |
| 33 | + stateNode: null, |
| 34 | + child: null, |
| 35 | + memoizedState: { |
| 36 | + memoizedState: 0, |
| 37 | + queue: { |
| 38 | + dispatch: function (newState) { |
| 39 | + this.memoizedState = newState; |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + memoizedProps: {}, |
| 44 | + actualDuration: 1, |
| 45 | + actualStartTime: 2, |
| 46 | + selfBaseDuration: 3, |
| 47 | + treeBaseDuration: 4, |
| 48 | + _debugHookTypes: ['useState'], |
| 49 | +}; |
| 50 | + |
| 51 | +const functionalComponentData: ComponentData = { |
| 52 | + actualDuration: 1, |
| 53 | + actualStartTime: 2, |
| 54 | + selfBaseDuration: 3, |
| 55 | + treeBaseDuration: 4, |
| 56 | + context: {}, |
| 57 | + hooksIndex: [0], |
| 58 | + hooksState: { count: 0 }, |
| 59 | + index: null, |
| 60 | + props: {}, |
| 61 | + state: null, |
| 62 | +}; |
| 63 | + |
| 64 | +componentActionsRecord.clear(); |
| 65 | +export const functionalPayload: Tree = new Tree('root', 'root'); |
| 66 | +functionalPayload.route = rootPayload.route; |
| 67 | +functionalPayload.addChild({ count: 0 }, 'IncrementFunc', functionalComponentData, null); |
| 68 | + |
| 69 | +// -----------------------TEST CASE FOR CLASS COMPONENT------------------------- |
| 70 | + |
| 71 | +export const classComponent: Fiber = { |
| 72 | + tag: ClassComponent, |
| 73 | + elementType: IncrementClass, |
| 74 | + sibling: null, |
| 75 | + stateNode: { |
| 76 | + state: { count: 0 }, |
| 77 | + setState: function (callback) { |
| 78 | + this.state = { ...callback() }; |
| 79 | + }, |
| 80 | + }, |
| 81 | + child: null, |
| 82 | + memoizedState: { |
| 83 | + count: 0, |
| 84 | + }, |
| 85 | + memoizedProps: {}, |
| 86 | + actualDuration: 1, |
| 87 | + actualStartTime: 2, |
| 88 | + selfBaseDuration: 3, |
| 89 | + treeBaseDuration: 4, |
| 90 | + _debugHookTypes: null, |
| 91 | +}; |
| 92 | +classComponent.stateNode.setState = classComponent.stateNode.setState.bind( |
| 93 | + classComponent.stateNode, |
| 94 | +); |
| 95 | + |
| 96 | +const classComponentData: ComponentData = { |
| 97 | + actualDuration: 1, |
| 98 | + actualStartTime: 2, |
| 99 | + selfBaseDuration: 3, |
| 100 | + treeBaseDuration: 4, |
| 101 | + context: {}, |
| 102 | + hooksIndex: null, |
| 103 | + hooksState: null, |
| 104 | + index: 0, |
| 105 | + props: {}, |
| 106 | + state: { count: 0 }, |
| 107 | +}; |
| 108 | + |
| 109 | +componentActionsRecord.clear(); |
| 110 | +export const classPayload = new Tree('root', 'root'); |
| 111 | +classPayload.route = rootPayload.route; |
| 112 | +classPayload.addChild({ count: 0 }, 'IncrementClass', classComponentData, null); |
| 113 | + |
| 114 | +componentActionsRecord.clear(); |
| 115 | +export const updateClassPayload = new Tree('root', 'root'); |
| 116 | +updateClassPayload.route = rootPayload.route; |
| 117 | +updateClassPayload.addChild( |
| 118 | + { count: 2 }, |
| 119 | + 'IncrementClass', |
| 120 | + { ...classComponentData, state: { count: 2 } }, |
| 121 | + null, |
| 122 | +); |
| 123 | + |
| 124 | +// -----------------------TEST CASE FOR MIX OF COMPONENTS----------------------- |
| 125 | +componentActionsRecord.clear(); |
| 126 | +export const mixComponents: Fiber = deepCopy(root); |
| 127 | +mixComponents.child = deepCopy(functionalComponent); |
| 128 | +mixComponents.sibling = deepCopy(classComponent); |
| 129 | +mixComponents.child!.child = deepCopy(functionalComponent); |
| 130 | +mixComponents.child!.child!.sibling = deepCopy(classComponent); |
| 131 | +// console.dir(mixComponents, { depth: null }); |
| 132 | + |
| 133 | +export const mixPayload = new Tree('root', 'root'); |
| 134 | +mixPayload.route = rootPayload.route; |
| 135 | + |
| 136 | +// Outer Func Comp |
| 137 | +let funcPayloadMix = new Tree({ count: 0 }, 'IncrementFunc', functionalComponentData, null); |
| 138 | +funcPayloadMix.componentData = { |
| 139 | + ...funcPayloadMix.componentData, |
| 140 | + hooksState: { count: 0 }, |
| 141 | + hooksIndex: [0], |
| 142 | +}; |
| 143 | +mixPayload.children.push(deepCopy(funcPayloadMix)); |
| 144 | + |
| 145 | +// Outer Class Comp |
| 146 | +let classPayloadMix = new Tree({ count: 0 }, 'IncrementClass', classComponentData, null); |
| 147 | +classPayloadMix.componentData = { |
| 148 | + ...classPayloadMix.componentData, |
| 149 | + state: { count: 0 }, |
| 150 | + index: 3, |
| 151 | +}; |
| 152 | +mixPayload.children.push(deepCopy(classPayloadMix)); |
| 153 | + |
| 154 | +// Inner Func Comp |
| 155 | +funcPayloadMix = new Tree({ count: 0 }, 'IncrementFunc', functionalComponentData, null); |
| 156 | +funcPayloadMix.componentData = { |
| 157 | + ...funcPayloadMix.componentData, |
| 158 | + hooksState: { count: 0 }, |
| 159 | + hooksIndex: [1], |
| 160 | +}; |
| 161 | +funcPayloadMix.name = 'IncrementFunc1'; |
| 162 | +mixPayload.children[0].children.push(deepCopy(funcPayloadMix)); |
| 163 | + |
| 164 | +// Inner Class Comp |
| 165 | +classPayloadMix = new Tree({ count: 0 }, 'IncrementClass', classComponentData, null); |
| 166 | +classPayloadMix.componentData = { |
| 167 | + ...classPayloadMix.componentData, |
| 168 | + state: { count: 0 }, |
| 169 | + index: 2, |
| 170 | +}; |
| 171 | +mixPayload.children[0].children.push(deepCopy(classPayloadMix)); |
| 172 | + |
| 173 | +// console.dir(mixPayload, { depth: null }); |
0 commit comments