Skip to content

Commit cf2a107

Browse files
committed
refactor: bring config host
1 parent 60dd2e6 commit cf2a107

File tree

3 files changed

+349
-0
lines changed

3 files changed

+349
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"dependencies": {
4949
"jest-matcher-utils": "^29.7.0",
5050
"pretty-format": "^29.7.0",
51+
"react-reconciler": "0.29.2",
5152
"redent": "^3.0.0"
5253
},
5354
"peerDependencies": {
@@ -74,6 +75,7 @@
7475
"@relmify/jest-serializer-strip-ansi": "^1.0.2",
7576
"@types/jest": "^29.5.12",
7677
"@types/react": "^18.3.3",
78+
"@types/react-reconciler": "^0",
7779
"@types/react-test-renderer": "^18.3.0",
7880
"babel-jest": "^29.7.0",
7981
"del-cli": "^5.1.0",
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
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+
}

yarn.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,6 +2875,7 @@ __metadata:
28752875
"@relmify/jest-serializer-strip-ansi": "npm:^1.0.2"
28762876
"@types/jest": "npm:^29.5.12"
28772877
"@types/react": "npm:^18.3.3"
2878+
"@types/react-reconciler": "npm:^0"
28782879
"@types/react-test-renderer": "npm:^18.3.0"
28792880
babel-jest: "npm:^29.7.0"
28802881
del-cli: "npm:^5.1.0"
@@ -2888,6 +2889,7 @@ __metadata:
28882889
pretty-format: "npm:^29.7.0"
28892890
react: "npm:18.3.1"
28902891
react-native: "npm:0.75.1"
2892+
react-reconciler: "npm:0.29.2"
28912893
react-test-renderer: "npm:18.3.1"
28922894
redent: "npm:^3.0.0"
28932895
release-it: "npm:^17.6.0"
@@ -3049,6 +3051,15 @@ __metadata:
30493051
languageName: node
30503052
linkType: hard
30513053

3054+
"@types/react-reconciler@npm:^0":
3055+
version: 0.28.8
3056+
resolution: "@types/react-reconciler@npm:0.28.8"
3057+
dependencies:
3058+
"@types/react": "npm:*"
3059+
checksum: 10c0/ca95cffcdf58591679c6c87dcc6f2c50cef9c6b2772d089ec0c695567656f34a30a0f2592f391d99b0e877f94afd67347082c55eb1dc5cb8000e23c8efc0fafc
3060+
languageName: node
3061+
linkType: hard
3062+
30523063
"@types/react-test-renderer@npm:^18.3.0":
30533064
version: 18.3.0
30543065
resolution: "@types/react-test-renderer@npm:18.3.0"
@@ -10082,6 +10093,18 @@ __metadata:
1008210093
languageName: node
1008310094
linkType: hard
1008410095

10096+
"react-reconciler@npm:0.29.2":
10097+
version: 0.29.2
10098+
resolution: "react-reconciler@npm:0.29.2"
10099+
dependencies:
10100+
loose-envify: "npm:^1.1.0"
10101+
scheduler: "npm:^0.23.2"
10102+
peerDependencies:
10103+
react: ^18.3.1
10104+
checksum: 10c0/94f48ddc348a974256cf13c859f5a94efdb0cd72e04c51b1a4d5c72a8b960ccd35df2196057ee6a4cbcb26145e12b01e3f9ba3b183fddb901414db36a07cbf43
10105+
languageName: node
10106+
linkType: hard
10107+
1008510108
"react-refresh@npm:^0.14.0":
1008610109
version: 0.14.2
1008710110
resolution: "react-refresh@npm:0.14.2"

0 commit comments

Comments
 (0)