|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | + */ |
| 8 | + |
| 9 | +//import isArray from 'shared/isArray'; |
| 10 | +//import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities'; |
| 11 | + |
| 12 | +import { DefaultEventPriority } from 'react-reconciler/constants'; |
| 13 | + |
| 14 | +export type Type = string; |
| 15 | +export type Props = object; |
| 16 | + |
| 17 | +export type Container = { |
| 18 | + tag: 'CONTAINER'; |
| 19 | + children: Array<Instance | TextInstance>; |
| 20 | + createNodeMock: Function; |
| 21 | +}; |
| 22 | + |
| 23 | +export type Instance = { |
| 24 | + tag: 'INSTANCE'; |
| 25 | + type: string; |
| 26 | + props: object; |
| 27 | + isHidden: boolean; |
| 28 | + children: Array<Instance | TextInstance>; |
| 29 | + internalInstanceHandle: object; |
| 30 | + rootContainerInstance: Container; |
| 31 | +}; |
| 32 | + |
| 33 | +export type TextInstance = { |
| 34 | + tag: 'TEXT'; |
| 35 | + text: string; |
| 36 | + isHidden: boolean; |
| 37 | +}; |
| 38 | + |
| 39 | +export type HydratableInstance = Instance | TextInstance; |
| 40 | +export type PublicInstance = Instance | TextInstance; |
| 41 | +export type HostContext = object; |
| 42 | +export type UpdatePayload = object; |
| 43 | +export type ChildSet = void; // Unused |
| 44 | +export type TimeoutHandle = ReturnType<typeof setTimeout>; |
| 45 | +export type NoTimeout = -1; |
| 46 | +export type EventResponder = any; |
| 47 | + |
| 48 | +export type RendererInspectionConfig = Readonly<{}>; |
| 49 | + |
| 50 | +// export * from 'react-reconciler/src/ReactFiberHostConfigWithNoPersistence'; |
| 51 | +// export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration'; |
| 52 | +// export * from 'react-reconciler/src/ReactFiberHostConfigWithNoTestSelectors'; |
| 53 | +// export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks'; |
| 54 | + |
| 55 | +const NO_CONTEXT = {}; |
| 56 | +const UPDATE_SIGNAL = {}; |
| 57 | +const nodeToInstanceMap = new WeakMap(); |
| 58 | + |
| 59 | +if (__DEV__) { |
| 60 | + Object.freeze(NO_CONTEXT); |
| 61 | + Object.freeze(UPDATE_SIGNAL); |
| 62 | +} |
| 63 | + |
| 64 | +export function getPublicInstance(inst: Instance | TextInstance) { |
| 65 | + switch (inst.tag) { |
| 66 | + case 'INSTANCE': { |
| 67 | + const createNodeMock = inst.rootContainerInstance.createNodeMock; |
| 68 | + const mockNode = createNodeMock({ |
| 69 | + type: inst.type, |
| 70 | + props: inst.props, |
| 71 | + }); |
| 72 | + if (typeof mockNode === 'object' && mockNode !== null) { |
| 73 | + nodeToInstanceMap.set(mockNode, inst); |
| 74 | + } |
| 75 | + |
| 76 | + return mockNode; |
| 77 | + } |
| 78 | + |
| 79 | + default: |
| 80 | + return inst; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export function appendChild( |
| 85 | + parentInstance: Instance | Container, |
| 86 | + child: Instance | TextInstance, |
| 87 | +): void { |
| 88 | + if (__DEV__) { |
| 89 | + if (!Array.isArray(parentInstance.children)) { |
| 90 | + // eslint-disable-next-line no-console |
| 91 | + console.error( |
| 92 | + 'An invalid container has been provided. ' + |
| 93 | + 'This may indicate that another renderer is being used in addition to the test renderer. ' + |
| 94 | + '(For example, ReactDOM.createPortal inside of a ReactTestRenderer tree.) ' + |
| 95 | + 'This is not supported.', |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
| 99 | + const index = parentInstance.children.indexOf(child); |
| 100 | + if (index !== -1) { |
| 101 | + parentInstance.children.splice(index, 1); |
| 102 | + } |
| 103 | + |
| 104 | + parentInstance.children.push(child); |
| 105 | +} |
| 106 | + |
| 107 | +export function insertBefore( |
| 108 | + parentInstance: Instance | Container, |
| 109 | + child: Instance | TextInstance, |
| 110 | + beforeChild: Instance | TextInstance, |
| 111 | +): void { |
| 112 | + const index = parentInstance.children.indexOf(child); |
| 113 | + if (index !== -1) { |
| 114 | + parentInstance.children.splice(index, 1); |
| 115 | + } |
| 116 | + |
| 117 | + const beforeIndex = parentInstance.children.indexOf(beforeChild); |
| 118 | + parentInstance.children.splice(beforeIndex, 0, child); |
| 119 | +} |
| 120 | + |
| 121 | +export function removeChild( |
| 122 | + parentInstance: Instance | Container, |
| 123 | + child: Instance | TextInstance, |
| 124 | +): void { |
| 125 | + const index = parentInstance.children.indexOf(child); |
| 126 | + parentInstance.children.splice(index, 1); |
| 127 | +} |
| 128 | + |
| 129 | +export function clearContainer(container: Container): void { |
| 130 | + container.children.splice(0); |
| 131 | +} |
| 132 | + |
| 133 | +export function getRootHostContext(_rootContainerInstance: Container): HostContext { |
| 134 | + return NO_CONTEXT; |
| 135 | +} |
| 136 | + |
| 137 | +export function getChildHostContext( |
| 138 | + _parentHostContext: HostContext, |
| 139 | + _type: string, |
| 140 | + _rootContainerInstance: Container, |
| 141 | +): HostContext { |
| 142 | + return NO_CONTEXT; |
| 143 | +} |
| 144 | + |
| 145 | +export function prepareForCommit(_containerInfo: Container): object | null { |
| 146 | + // noop |
| 147 | + return null; |
| 148 | +} |
| 149 | + |
| 150 | +export function resetAfterCommit(_containerInfo: Container): void { |
| 151 | + // noop |
| 152 | +} |
| 153 | + |
| 154 | +export function createInstance( |
| 155 | + type: string, |
| 156 | + props: Props, |
| 157 | + rootContainerInstance: Container, |
| 158 | + _hostContext: object, |
| 159 | + internalInstanceHandle: object, |
| 160 | +): Instance { |
| 161 | + return { |
| 162 | + type, |
| 163 | + props, |
| 164 | + isHidden: false, |
| 165 | + children: [], |
| 166 | + internalInstanceHandle, |
| 167 | + rootContainerInstance, |
| 168 | + tag: 'INSTANCE', |
| 169 | + }; |
| 170 | +} |
| 171 | + |
| 172 | +export function appendInitialChild(parentInstance: Instance, child: Instance | TextInstance): void { |
| 173 | + const index = parentInstance.children.indexOf(child); |
| 174 | + if (index !== -1) { |
| 175 | + parentInstance.children.splice(index, 1); |
| 176 | + } |
| 177 | + |
| 178 | + parentInstance.children.push(child); |
| 179 | +} |
| 180 | + |
| 181 | +export function finalizeInitialChildren( |
| 182 | + _testElement: Instance, |
| 183 | + _type: string, |
| 184 | + _props: Props, |
| 185 | + _rootContainerInstance: Container, |
| 186 | + _hostContext: object, |
| 187 | +): boolean { |
| 188 | + return false; |
| 189 | +} |
| 190 | + |
| 191 | +export function prepareUpdate( |
| 192 | + _testElement: Instance, |
| 193 | + _type: string, |
| 194 | + _oldProps: Props, |
| 195 | + _newProps: Props, |
| 196 | + _rootContainerInstance: Container, |
| 197 | + _hostContext: object, |
| 198 | +): object | null { |
| 199 | + return UPDATE_SIGNAL; |
| 200 | +} |
| 201 | + |
| 202 | +export function shouldSetTextContent(_type: string, _props: Props): boolean { |
| 203 | + return false; |
| 204 | +} |
| 205 | + |
| 206 | +export function createTextInstance( |
| 207 | + text: string, |
| 208 | + _rootContainerInstance: Container, |
| 209 | + _hostContext: object, |
| 210 | + _internalInstanceHandle: object, |
| 211 | +): TextInstance { |
| 212 | + return { |
| 213 | + tag: 'TEXT', |
| 214 | + text, |
| 215 | + isHidden: false, |
| 216 | + }; |
| 217 | +} |
| 218 | + |
| 219 | +export function getCurrentEventPriority(): number { |
| 220 | + return DefaultEventPriority; |
| 221 | +} |
| 222 | + |
| 223 | +export const isPrimaryRenderer = false; |
| 224 | +export const warnsIfNotActing = true; |
| 225 | + |
| 226 | +export const scheduleTimeout = setTimeout; |
| 227 | +export const cancelTimeout = clearTimeout; |
| 228 | + |
| 229 | +export const noTimeout = -1; |
| 230 | + |
| 231 | +// ------------------- |
| 232 | +// Mutation |
| 233 | +// ------------------- |
| 234 | + |
| 235 | +export const supportsMutation = true; |
| 236 | + |
| 237 | +export function commitUpdate( |
| 238 | + instance: Instance, |
| 239 | + _updatePayload: object, |
| 240 | + type: string, |
| 241 | + _oldProps: Props, |
| 242 | + newProps: Props, |
| 243 | + _internalInstanceHandle: object, |
| 244 | +): void { |
| 245 | + instance.type = type; |
| 246 | + instance.props = newProps; |
| 247 | +} |
| 248 | + |
| 249 | +export function commitMount( |
| 250 | + _instance: Instance, |
| 251 | + _type: string, |
| 252 | + _newProps: Props, |
| 253 | + _internalInstanceHandle: object, |
| 254 | +): void { |
| 255 | + // noop |
| 256 | +} |
| 257 | + |
| 258 | +export function commitTextUpdate( |
| 259 | + textInstance: TextInstance, |
| 260 | + _oldText: string, |
| 261 | + newText: string, |
| 262 | +): void { |
| 263 | + textInstance.text = newText; |
| 264 | +} |
| 265 | + |
| 266 | +export function resetTextContent(_testElement: Instance): void { |
| 267 | + // noop |
| 268 | +} |
| 269 | + |
| 270 | +export const appendChildToContainer = appendChild; |
| 271 | +export const insertInContainerBefore = insertBefore; |
| 272 | +export const removeChildFromContainer = removeChild; |
| 273 | + |
| 274 | +export function hideInstance(instance: Instance): void { |
| 275 | + instance.isHidden = true; |
| 276 | +} |
| 277 | + |
| 278 | +export function hideTextInstance(textInstance: TextInstance): void { |
| 279 | + textInstance.isHidden = true; |
| 280 | +} |
| 281 | + |
| 282 | +export function unhideInstance(instance: Instance, _props: Props): void { |
| 283 | + instance.isHidden = false; |
| 284 | +} |
| 285 | + |
| 286 | +export function unhideTextInstance(textInstance: TextInstance, _text: string): void { |
| 287 | + textInstance.isHidden = false; |
| 288 | +} |
| 289 | + |
| 290 | +export function getInstanceFromNode(mockNode: object) { |
| 291 | + const instance = nodeToInstanceMap.get(mockNode); |
| 292 | + if (instance !== undefined) { |
| 293 | + return instance.internalInstanceHandle; |
| 294 | + } |
| 295 | + return null; |
| 296 | +} |
| 297 | + |
| 298 | +export function beforeActiveInstanceBlur(_internalInstanceHandle: object) { |
| 299 | + // noop |
| 300 | +} |
| 301 | + |
| 302 | +export function afterActiveInstanceBlur() { |
| 303 | + // noop |
| 304 | +} |
| 305 | + |
| 306 | +export function preparePortalMount(_portalInstance: Instance): void { |
| 307 | + // noop |
| 308 | +} |
| 309 | + |
| 310 | +export function prepareScopeUpdate(scopeInstance: object, inst: object): void { |
| 311 | + nodeToInstanceMap.set(scopeInstance, inst); |
| 312 | +} |
| 313 | + |
| 314 | +export function getInstanceFromScope(scopeInstance: object): object | null { |
| 315 | + return nodeToInstanceMap.get(scopeInstance) || null; |
| 316 | +} |
| 317 | + |
| 318 | +export function detachDeletedInstance(_node: Instance): void { |
| 319 | + // noop |
| 320 | +} |
| 321 | + |
| 322 | +export function logRecoverableError(_error: unknown): void { |
| 323 | + // noop |
| 324 | +} |
0 commit comments