|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import emptyObject from 'fbjs/lib/emptyObject'; |
| 11 | + |
| 12 | +import * as TestRendererScheduling from './ReactTestRendererScheduling'; |
| 13 | + |
| 14 | +export type Instance = {| |
| 15 | + type: string, |
| 16 | + props: Object, |
| 17 | + children: Array<Instance | TextInstance>, |
| 18 | + rootContainerInstance: Container, |
| 19 | + tag: 'INSTANCE', |
| 20 | +|}; |
| 21 | + |
| 22 | +export type TextInstance = {| |
| 23 | + text: string, |
| 24 | + tag: 'TEXT', |
| 25 | +|}; |
| 26 | + |
| 27 | +type Container = {| |
| 28 | + children: Array<Instance | TextInstance>, |
| 29 | + createNodeMock: Function, |
| 30 | + tag: 'CONTAINER', |
| 31 | +|}; |
| 32 | + |
| 33 | +type Props = Object; |
| 34 | + |
| 35 | +const UPDATE_SIGNAL = {}; |
| 36 | + |
| 37 | +function getPublicInstance(inst: Instance | TextInstance): * { |
| 38 | + switch (inst.tag) { |
| 39 | + case 'INSTANCE': |
| 40 | + const createNodeMock = inst.rootContainerInstance.createNodeMock; |
| 41 | + return createNodeMock({ |
| 42 | + type: inst.type, |
| 43 | + props: inst.props, |
| 44 | + }); |
| 45 | + default: |
| 46 | + return inst; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function appendChild( |
| 51 | + parentInstance: Instance | Container, |
| 52 | + child: Instance | TextInstance, |
| 53 | +): void { |
| 54 | + const index = parentInstance.children.indexOf(child); |
| 55 | + if (index !== -1) { |
| 56 | + parentInstance.children.splice(index, 1); |
| 57 | + } |
| 58 | + parentInstance.children.push(child); |
| 59 | +} |
| 60 | + |
| 61 | +function insertBefore( |
| 62 | + parentInstance: Instance | Container, |
| 63 | + child: Instance | TextInstance, |
| 64 | + beforeChild: Instance | TextInstance, |
| 65 | +): void { |
| 66 | + const index = parentInstance.children.indexOf(child); |
| 67 | + if (index !== -1) { |
| 68 | + parentInstance.children.splice(index, 1); |
| 69 | + } |
| 70 | + const beforeIndex = parentInstance.children.indexOf(beforeChild); |
| 71 | + parentInstance.children.splice(beforeIndex, 0, child); |
| 72 | +} |
| 73 | + |
| 74 | +function removeChild( |
| 75 | + parentInstance: Instance | Container, |
| 76 | + child: Instance | TextInstance, |
| 77 | +): void { |
| 78 | + const index = parentInstance.children.indexOf(child); |
| 79 | + parentInstance.children.splice(index, 1); |
| 80 | +} |
| 81 | + |
| 82 | +const ReactTestHostConfig = { |
| 83 | + getRootHostContext() { |
| 84 | + return emptyObject; |
| 85 | + }, |
| 86 | + |
| 87 | + getChildHostContext() { |
| 88 | + return emptyObject; |
| 89 | + }, |
| 90 | + |
| 91 | + prepareForCommit(): void { |
| 92 | + // noop |
| 93 | + }, |
| 94 | + |
| 95 | + resetAfterCommit(): void { |
| 96 | + // noop |
| 97 | + }, |
| 98 | + |
| 99 | + createInstance( |
| 100 | + type: string, |
| 101 | + props: Props, |
| 102 | + rootContainerInstance: Container, |
| 103 | + hostContext: Object, |
| 104 | + internalInstanceHandle: Object, |
| 105 | + ): Instance { |
| 106 | + return { |
| 107 | + type, |
| 108 | + props, |
| 109 | + children: [], |
| 110 | + rootContainerInstance, |
| 111 | + tag: 'INSTANCE', |
| 112 | + }; |
| 113 | + }, |
| 114 | + |
| 115 | + appendInitialChild( |
| 116 | + parentInstance: Instance, |
| 117 | + child: Instance | TextInstance, |
| 118 | + ): void { |
| 119 | + const index = parentInstance.children.indexOf(child); |
| 120 | + if (index !== -1) { |
| 121 | + parentInstance.children.splice(index, 1); |
| 122 | + } |
| 123 | + parentInstance.children.push(child); |
| 124 | + }, |
| 125 | + |
| 126 | + finalizeInitialChildren( |
| 127 | + testElement: Instance, |
| 128 | + type: string, |
| 129 | + props: Props, |
| 130 | + rootContainerInstance: Container, |
| 131 | + ): boolean { |
| 132 | + return false; |
| 133 | + }, |
| 134 | + |
| 135 | + prepareUpdate( |
| 136 | + testElement: Instance, |
| 137 | + type: string, |
| 138 | + oldProps: Props, |
| 139 | + newProps: Props, |
| 140 | + rootContainerInstance: Container, |
| 141 | + hostContext: Object, |
| 142 | + ): null | {} { |
| 143 | + return UPDATE_SIGNAL; |
| 144 | + }, |
| 145 | + |
| 146 | + shouldSetTextContent(type: string, props: Props): boolean { |
| 147 | + return false; |
| 148 | + }, |
| 149 | + |
| 150 | + shouldDeprioritizeSubtree(type: string, props: Props): boolean { |
| 151 | + return false; |
| 152 | + }, |
| 153 | + |
| 154 | + createTextInstance( |
| 155 | + text: string, |
| 156 | + rootContainerInstance: Container, |
| 157 | + hostContext: Object, |
| 158 | + internalInstanceHandle: Object, |
| 159 | + ): TextInstance { |
| 160 | + return { |
| 161 | + text, |
| 162 | + tag: 'TEXT', |
| 163 | + }; |
| 164 | + }, |
| 165 | + |
| 166 | + getPublicInstance, |
| 167 | + |
| 168 | + scheduleDeferredCallback: TestRendererScheduling.scheduleDeferredCallback, |
| 169 | + cancelDeferredCallback: TestRendererScheduling.cancelDeferredCallback, |
| 170 | + // This approach enables `now` to be mocked by tests, |
| 171 | + // Even after the reconciler has initialized and read host config values. |
| 172 | + now: () => TestRendererScheduling.nowImplementation(), |
| 173 | + |
| 174 | + isPrimaryRenderer: true, |
| 175 | + |
| 176 | + mutation: { |
| 177 | + commitUpdate( |
| 178 | + instance: Instance, |
| 179 | + updatePayload: {}, |
| 180 | + type: string, |
| 181 | + oldProps: Props, |
| 182 | + newProps: Props, |
| 183 | + internalInstanceHandle: Object, |
| 184 | + ): void { |
| 185 | + instance.type = type; |
| 186 | + instance.props = newProps; |
| 187 | + }, |
| 188 | + |
| 189 | + commitMount( |
| 190 | + instance: Instance, |
| 191 | + type: string, |
| 192 | + newProps: Props, |
| 193 | + internalInstanceHandle: Object, |
| 194 | + ): void { |
| 195 | + // noop |
| 196 | + }, |
| 197 | + |
| 198 | + commitTextUpdate( |
| 199 | + textInstance: TextInstance, |
| 200 | + oldText: string, |
| 201 | + newText: string, |
| 202 | + ): void { |
| 203 | + textInstance.text = newText; |
| 204 | + }, |
| 205 | + resetTextContent(testElement: Instance): void { |
| 206 | + // noop |
| 207 | + }, |
| 208 | + |
| 209 | + appendChild: appendChild, |
| 210 | + appendChildToContainer: appendChild, |
| 211 | + insertBefore: insertBefore, |
| 212 | + insertInContainerBefore: insertBefore, |
| 213 | + removeChild: removeChild, |
| 214 | + removeChildFromContainer: removeChild, |
| 215 | + }, |
| 216 | +}; |
| 217 | + |
| 218 | +export default ReactTestHostConfig; |
0 commit comments