|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +import type { |
| 13 | + LayoutAnimationConfig, |
| 14 | + MeasureInWindowOnSuccessCallback, |
| 15 | + MeasureLayoutOnSuccessCallback, |
| 16 | + MeasureOnSuccessCallback, |
| 17 | + Node, |
| 18 | +} from '../../Renderer/shims/ReactNativeTypes'; |
| 19 | +import type {RootTag} from '../../Types/RootTagTypes'; |
| 20 | +import type { |
| 21 | + InstanceHandle, |
| 22 | + NodeProps, |
| 23 | + NodeSet, |
| 24 | + Spec as FabricUIManager, |
| 25 | +} from '../FabricUIManager'; |
| 26 | + |
| 27 | +type NodeMock = { |
| 28 | + reactTag: number, |
| 29 | + rootTag: RootTag, |
| 30 | + props: NodeProps, |
| 31 | + instanceHandle: InstanceHandle, |
| 32 | + children: NodeSet, |
| 33 | +}; |
| 34 | + |
| 35 | +function fromNode(node: Node): NodeMock { |
| 36 | + // $FlowExpectedError[incompatible-return] |
| 37 | + return node; |
| 38 | +} |
| 39 | + |
| 40 | +function toNode(node: NodeMock): Node { |
| 41 | + // $FlowExpectedError[incompatible-return] |
| 42 | + return node; |
| 43 | +} |
| 44 | + |
| 45 | +const FabricUIManagerMock: FabricUIManager = { |
| 46 | + createNode: jest.fn( |
| 47 | + ( |
| 48 | + reactTag: number, |
| 49 | + viewName: string, |
| 50 | + rootTag: RootTag, |
| 51 | + props: NodeProps, |
| 52 | + instanceHandle: InstanceHandle, |
| 53 | + ): Node => { |
| 54 | + return toNode({ |
| 55 | + reactTag, |
| 56 | + rootTag, |
| 57 | + props, |
| 58 | + instanceHandle, |
| 59 | + children: [], |
| 60 | + }); |
| 61 | + }, |
| 62 | + ), |
| 63 | + cloneNode: jest.fn((node: Node): Node => { |
| 64 | + return toNode({...fromNode(node)}); |
| 65 | + }), |
| 66 | + cloneNodeWithNewChildren: jest.fn((node: Node): Node => { |
| 67 | + return toNode({...fromNode(node), children: []}); |
| 68 | + }), |
| 69 | + cloneNodeWithNewProps: jest.fn((node: Node, newProps: NodeProps): Node => { |
| 70 | + return toNode({...fromNode(node), props: newProps}); |
| 71 | + }), |
| 72 | + cloneNodeWithNewChildrenAndProps: jest.fn( |
| 73 | + (node: Node, newProps: NodeProps): Node => { |
| 74 | + return toNode({...fromNode(node), children: [], props: newProps}); |
| 75 | + }, |
| 76 | + ), |
| 77 | + createChildSet: jest.fn((rootTag: RootTag): NodeSet => { |
| 78 | + return []; |
| 79 | + }), |
| 80 | + appendChild: jest.fn((parentNode: Node, child: Node): Node => { |
| 81 | + return toNode({ |
| 82 | + ...fromNode(parentNode), |
| 83 | + children: fromNode(parentNode).children.concat(child), |
| 84 | + }); |
| 85 | + }), |
| 86 | + appendChildToSet: jest.fn((childSet: NodeSet, child: Node): void => { |
| 87 | + childSet.push(child); |
| 88 | + }), |
| 89 | + completeRoot: jest.fn((rootTag: RootTag, childSet: NodeSet): void => {}), |
| 90 | + measure: jest.fn((node: Node, callback: MeasureOnSuccessCallback): void => { |
| 91 | + callback(10, 10, 100, 100, 0, 0); |
| 92 | + }), |
| 93 | + measureInWindow: jest.fn( |
| 94 | + (node: Node, callback: MeasureInWindowOnSuccessCallback): void => { |
| 95 | + callback(10, 10, 100, 100); |
| 96 | + }, |
| 97 | + ), |
| 98 | + measureLayout: jest.fn( |
| 99 | + ( |
| 100 | + node: Node, |
| 101 | + relativeNode: Node, |
| 102 | + onFail: () => void, |
| 103 | + onSuccess: MeasureLayoutOnSuccessCallback, |
| 104 | + ): void => { |
| 105 | + onSuccess(1, 1, 100, 100); |
| 106 | + }, |
| 107 | + ), |
| 108 | + configureNextLayoutAnimation: jest.fn( |
| 109 | + ( |
| 110 | + config: LayoutAnimationConfig, |
| 111 | + callback: () => void, // check what is returned here |
| 112 | + errorCallback: () => void, |
| 113 | + ): void => {}, |
| 114 | + ), |
| 115 | + sendAccessibilityEvent: jest.fn((node: Node, eventType: string): void => {}), |
| 116 | + findShadowNodeByTag_DEPRECATED: jest.fn((reactTag: number): ?Node => {}), |
| 117 | + getBoundingClientRect: jest.fn( |
| 118 | + ( |
| 119 | + node: Node, |
| 120 | + ): [ |
| 121 | + /* x:*/ number, |
| 122 | + /* y:*/ number, |
| 123 | + /* width:*/ number, |
| 124 | + /* height:*/ number, |
| 125 | + ] => { |
| 126 | + return [1, 1, 100, 100]; |
| 127 | + }, |
| 128 | + ), |
| 129 | + setNativeProps: jest.fn((node: Node, newProps: NodeProps): void => {}), |
| 130 | + dispatchCommand: jest.fn( |
| 131 | + (node: Node, commandName: string, args: Array<mixed>): void => {}, |
| 132 | + ), |
| 133 | +}; |
| 134 | + |
| 135 | +global.nativeFabricUIManager = FabricUIManagerMock; |
| 136 | + |
| 137 | +export function getFabricUIManager(): ?FabricUIManager { |
| 138 | + return FabricUIManagerMock; |
| 139 | +} |
0 commit comments