forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitializeNativeFabricUIManager.js
182 lines (167 loc) · 4.6 KB
/
InitializeNativeFabricUIManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable react-internal/invariant-args */
'use strict';
// Mock of the Native Hooks
import invariant from 'shared/invariant';
const roots = new Map();
const allocatedTags = new Set();
function dumpSubtree(info, indent) {
let out = '';
out += ' '.repeat(indent) + info.viewName + ' ' + JSON.stringify(info.props);
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const child of info.children) {
out += '\n' + dumpSubtree(child, indent + 2);
}
return out;
}
const RCTFabricUIManager = {
__dumpChildSetForJestTestsOnly: function(childSet) {
const result = [];
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const child of childSet) {
result.push(dumpSubtree(child, 0));
}
return result.join('\n');
},
__dumpHierarchyForJestTestsOnly: function() {
const result = [];
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const [rootTag, childSet] of roots) {
result.push(rootTag);
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const child of childSet) {
result.push(dumpSubtree(child, 1));
}
}
return result.join('\n');
},
createNode: jest.fn(function createNode(
reactTag,
viewName,
rootTag,
props,
eventTarget,
) {
invariant(
!allocatedTags.has(reactTag),
'Created two native views with tag %s',
reactTag,
);
allocatedTags.add(reactTag);
return {
reactTag: reactTag,
viewName: viewName,
props: props,
children: [],
};
}),
cloneNode: jest.fn(function cloneNode(node) {
return {
reactTag: node.reactTag,
viewName: node.viewName,
props: node.props,
children: node.children,
};
}),
cloneNodeWithNewChildren: jest.fn(function cloneNodeWithNewChildren(node) {
return {
reactTag: node.reactTag,
viewName: node.viewName,
props: node.props,
children: [],
};
}),
cloneNodeWithNewProps: jest.fn(function cloneNodeWithNewProps(
node,
newPropsDiff,
) {
return {
reactTag: node.reactTag,
viewName: node.viewName,
props: {...node.props, ...newPropsDiff},
children: node.children,
};
}),
cloneNodeWithNewChildrenAndProps: jest.fn(
function cloneNodeWithNewChildrenAndProps(node, newPropsDiff) {
return {
reactTag: node.reactTag,
viewName: node.viewName,
props: {...node.props, ...newPropsDiff},
children: [],
};
},
),
appendChild: jest.fn(function appendChild(parentNode, childNode) {
parentNode.children.push(childNode);
}),
createChildSet: jest.fn(function createChildSet() {
return [];
}),
appendChildToSet: jest.fn(function appendChildToSet(childSet, childNode) {
childSet.push(childNode);
}),
completeRoot: jest.fn(function completeRoot(rootTag, newChildSet) {
roots.set(rootTag, newChildSet);
}),
dispatchCommand: jest.fn(),
sendAccessibilityEvent: jest.fn(),
registerEventHandler: jest.fn(function registerEventHandler(callback) {}),
measure: jest.fn(function measure(node, callback) {
invariant(
typeof node === 'object',
'Expected node to be an object, was passed "%s"',
typeof node,
);
invariant(
typeof node.viewName === 'string',
'Expected node to be a host node.',
);
callback(10, 10, 100, 100, 0, 0);
}),
measureInWindow: jest.fn(function measureInWindow(node, callback) {
invariant(
typeof node === 'object',
'Expected node to be an object, was passed "%s"',
typeof node,
);
invariant(
typeof node.viewName === 'string',
'Expected node to be a host node.',
);
callback(10, 10, 100, 100);
}),
measureLayout: jest.fn(function measureLayout(
node,
relativeNode,
fail,
success,
) {
invariant(
typeof node === 'object',
'Expected node to be an object, was passed "%s"',
typeof node,
);
invariant(
typeof node.viewName === 'string',
'Expected node to be a host node.',
);
invariant(
typeof relativeNode === 'object',
'Expected relative node to be an object, was passed "%s"',
typeof relativeNode,
);
invariant(
typeof relativeNode.viewName === 'string',
'Expected relative node to be a host node.',
);
success(1, 1, 100, 100);
}),
setIsJSResponder: jest.fn(),
};
global.nativeFabricUIManager = RCTFabricUIManager;