-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
538 lines (457 loc) · 14.2 KB
/
index.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import {
ForwardRef,
Fragment,
Memo,
Portal,
Profiler,
StrictMode,
Suspense,
} from "./reactSymbols";
import type {
ChildrenFiberOrInternalInstance,
FiberOrInternalInstance,
ReactComponent,
ReactTestChild,
} from "./types";
const testSymbol =
typeof Symbol === "function" && Symbol.for
? Symbol.for("react.test.json")
: 0xe_a7_13_57;
/**
* Transforms a HTML element into a shallow representation of a React component
*/
export function shallow(
rootElement: Element | null,
RootReactComponent: ReactComponent | string,
): ReactTestChild | null {
if (rootElement === null) {
return null;
}
if (!RootReactComponent) {
throw new Error(
"Shallow: Expected syntax 'shallow(container, RootReactComponent)'. Did you forget to provide RootReactComponent? This is usually the same component that you are testing. See the documentation for more information.",
);
}
const fiberOrInternalInstance =
getFirstNestedFiberOrInternalInstance(rootElement);
if (!fiberOrInternalInstance) {
throw new Error(
"Shallow: No React component found in the provided element, or its children. Are you sure there is a React component rendered?",
);
}
let rootReactComponent = getFirstChildOfRootReactComponent(
fiberOrInternalInstance,
RootReactComponent,
);
// If the root component is using state, then the props that we are seeing might not be up-to-date
if (rootReactComponent.return?.memoizedState) {
const rootReactComponentCandidate = findCurrentlyRenderedState(
rootReactComponent.return,
).child;
if (!rootReactComponentCandidate) {
throw new Error(
"Shallow: Unable to find the currently rendered state. This should not happen. Please, report this issue.",
);
}
rootReactComponent = rootReactComponentCandidate;
}
return renderReactComponentWithChildren(rootReactComponent);
}
/**
* Search through alternate versions of current node to find the currently rendered state
*/
function findCurrentlyRenderedState(
node: FiberOrInternalInstance,
history: FiberOrInternalInstance[] = [],
): FiberOrInternalInstance {
let current = node;
const isLastRenderedState = isClassComponentState(current)
? isLastRenderedStateClassComponent
: isLastRenderedStateFunctionalComponent;
while (!isLastRenderedState(current)) {
// There should always be an alternate component
if (!current.alternate) {
throw new Error(
"Shallow: Unable to find the currently rendered state. There is no alternate component. This should not happen. Please, report this issue.",
);
}
// This is here just to make sure we don't end up in an infinite loop
if (history.includes(current)) {
throw new Error(
"Shallow: Unable to find the currently rendered state. There is a circular reference. This should not happen. Please, report this issue.",
);
}
history.push(current);
current = current.alternate;
}
return current;
}
/**
* Checks if the node state definitions are specific for class components
*/
function isClassComponentState(node: FiberOrInternalInstance): boolean {
return !node._debugHookTypes; // Debug hook types are available only in functional components
}
/**
* Checks if the node state is the last rendered state (works for functional components)
*/
function isLastRenderedStateFunctionalComponent(
node: FiberOrInternalInstance,
): boolean {
// There is no queue, which means it is the last rendered state
if (!node.memoizedState.queue) {
return true;
}
return (
node.memoizedState.memoizedState ===
node.memoizedState.queue.lastRenderedState
);
}
/**
* Checks if the node state is the last rendered state (works for class components)
*/
function isLastRenderedStateClassComponent(
node: FiberOrInternalInstance,
): boolean {
// There is no queue, which means it is the last rendered state
if (!node.updateQueue) {
return true;
}
// React 16
if (node.updateQueue.lastBaseUpdate === undefined) {
return node.updateQueue.baseQueue === null;
}
// React 17+
return node.updateQueue.lastBaseUpdate === null;
}
/**
* Get first nested React Fiber or InternalInstance from a HTML element
*/
function getFirstNestedFiberOrInternalInstance(rootElement: Element) {
let current = rootElement;
let fiberOrInternalInstance = getFiberOrInternalInstance(current);
// If the root element is not a React component, then we need to find the first child that is a React component
while (!fiberOrInternalInstance && current.firstElementChild) {
current = current.firstElementChild;
fiberOrInternalInstance = getFiberOrInternalInstance(current);
}
return fiberOrInternalInstance;
}
/**
* Transforms React components in filter to their actual components
* This is needed because React components are sometimes wrapped in React.memo or React.forwardRef
*/
function transformRootReactComponent(
RootReactComponent: ReactComponent,
): ReactComponent {
if (typeof RootReactComponent === "string") {
return RootReactComponent;
}
if (Memo && RootReactComponent.$$typeof === Memo) {
return RootReactComponent.type;
}
return RootReactComponent;
}
/**
* Climb up the tree to find the root React component matching the filter
*/
function getFirstChildOfRootReactComponent(
fiberOrInternalInstance: FiberOrInternalInstance,
RootReactComponent: ReactComponent,
): FiberOrInternalInstance {
const componentStorage: string[] = [];
const TransformedRootReactComponent =
transformRootReactComponent(RootReactComponent);
let current = fiberOrInternalInstance;
// Find first component related to our filter
// First we check in parent components
while (
current.return &&
!isParentComponentMatching(
current,
TransformedRootReactComponent,
componentStorage,
)
) {
current = current.return;
}
// If there is no matching parent component, then we need to check in children
// If a child has a sibling, then it is a hint, that something is wrong and we abort
// We could support search in siblings, but I cannot imagine a real-life scenario where it would be useful
while (
current.child &&
!current.child.sibling &&
!isParentComponentMatching(
current,
TransformedRootReactComponent,
componentStorage,
)
) {
current = current.child;
}
// If we didn't find any matching component, then we throw an error
if (!isParentComponentMatching(current, TransformedRootReactComponent)) {
const componentsInfo =
componentStorage.length > 0
? `Found components:\n ${componentStorage.join("\n ")}`
: "No components found in the tree.";
throw new Error(
`Shallow: None of the rendered components matches the provided RootReactComponent "${getComponentDisplayName(RootReactComponent)}"\n\n${componentsInfo}`,
);
}
return current;
}
/**
* Get React Fiber or InternalInstance from a HTML element
*/
function getFiberOrInternalInstance(element: Element): FiberOrInternalInstance {
return Object.entries(element).find(
([key]) =>
key.startsWith("__reactFiber$") || // Functional component
key.startsWith("__reactInternalInstance$"), // Class component
)?.[1];
}
/**
* Transform React component into a JSON representation
*/
function renderReactComponentWithChildren(
reactComponent: FiberOrInternalInstance | string | number,
): ReactTestChild {
if (typeof reactComponent !== "object") {
return reactComponent;
}
const siblings = getReactComponentSiblings(reactComponent);
// Only if the root component is wrapped in fragment, then there can be siblings
if (siblings.length > 0) {
return {
$$typeof: testSymbol,
type: "Fragment",
props: {},
children: [reactComponent, ...siblings]
.map((child) => {
return childrenReactComponentToTestObject(
reactComponentToChildren(child),
);
})
.filter(reactFalsyValuesFilter),
};
}
return childrenReactComponentToTestObject(
reactComponentToChildren(reactComponent),
);
}
/**
* Transform React component children into a ReactTestObject
*/
function childrenReactComponentToTestObject(
childrenReactComponent: ChildrenFiberOrInternalInstance | string | number,
): ReactTestChild {
if (typeof childrenReactComponent !== "object") {
return childrenReactComponent;
}
return {
$$typeof: testSymbol,
type: getType(childrenReactComponent),
props: getProps(childrenReactComponent),
children: getChildrenFromProps(childrenReactComponent),
};
}
/**
* Get siblings of the react component
*/
function getReactComponentSiblings(
reactComponent: FiberOrInternalInstance,
): FiberOrInternalInstance[] {
const siblings = [];
let current = reactComponent;
while (current.sibling) {
siblings.push(current.sibling);
current = current.sibling;
}
return siblings;
}
/**
* Transform React component into a ChildrenFiberOrInternalInstance
*/
function reactComponentToChildren(
reactComponent: FiberOrInternalInstance | string | number,
): ChildrenFiberOrInternalInstance | string | number {
if (typeof reactComponent !== "object") {
return reactComponent;
}
if (
reactComponent.memoizedProps &&
typeof reactComponent.memoizedProps !== "object"
) {
return reactComponent.memoizedProps;
}
// React.Fragment
if (!reactComponent.elementType) {
return {
$$typeof: testSymbol,
type: "Fragment",
props: { children: reactComponent.memoizedProps },
};
}
return {
$$typeof: testSymbol,
type: reactComponent.elementType,
props: reactComponent.memoizedProps,
};
}
/**
* Check if parent of reactComponent is matching the Component
*/
function isParentComponentMatching(
reactComponent: FiberOrInternalInstance,
Component: ReactComponent,
componentStorage?: string[],
): boolean {
if (!reactComponent.return) {
return false;
}
const { type } = reactComponent.return;
// Not sure if this can happen, but since we are dealing with internal React structures,
// it is better to be safe than sorry
if (!type) {
return false;
}
const displayName = getComponentDisplayName(type);
// Store component names for debugging purposes and better error messages
if (componentStorage && !componentStorage.includes(displayName)) {
componentStorage.push(displayName);
}
if (typeof Component === "string") {
return displayName === Component;
}
return type === Component;
}
/**
* Get display name of the react component
*/
function getComponentDisplayName(type: ReactComponent): string {
return typeof type === "string"
? type // native elements
: type.displayName || type.name || type.constructor?.name; // functional components || class components
}
/**
* Get type of the react component
* Inspired by https://github.com/enzymejs/enzyme/blob/67b9ebeb3cc66ec1b3d43055c6463a595387fb14/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js#L888
*/
function getType(instance: ChildrenFiberOrInternalInstance): string {
const { type, $$typeof } = instance;
if (Portal && $$typeof && $$typeof === Portal) {
return "Portal";
}
if (!type) {
throw new Error(
"Shallow: Unable to get type of the component. This should not happen. Please, report this issue.",
);
}
// Native elements
if (typeof type === "string") {
return type;
}
// Functional components
if (typeof type === "function") {
return type.displayName || type.name;
}
// Class components
if (type.prototype?.isReactComponent) {
return type.constructor.name;
}
// React.memo
if (Memo && type.$$typeof === Memo) {
return `Memo(${getType(type)})`;
}
// React.forwardRef
if (ForwardRef && type.$$typeof === ForwardRef) {
return `ForwardRef(${getType({ type: type.render } as ChildrenFiberOrInternalInstance)})`;
}
// React.Fragment
if (Fragment && type === Fragment) {
return "Fragment";
}
// React.Profiler
if (Profiler && type === Profiler) {
return "Profiler";
}
// React.StrictMode
if (StrictMode && type === StrictMode) {
return "StrictMode";
}
// React.Suspense
if (Suspense && type === Suspense) {
return "Suspense";
}
// Unhandled type, this error hints that we need to add support for a new type
throw new Error(`Unknown type ${type}`);
}
/**
* Get props of the react component
*/
function getProps(
node: ChildrenFiberOrInternalInstance,
): Record<string, unknown> {
const { props = {} } = node;
return Object.entries(props)
.filter(([key, value]) => {
// Skip children and undefined values
if (key === "children" || value === undefined) {
return false;
}
return true;
})
.reduce(
(acc, [key, value]) => {
acc[key] = value;
return acc;
},
{} as Record<string, unknown>,
);
}
/**
* Get children from props
*/
function getChildrenFromProps(
node: ChildrenFiberOrInternalInstance,
): ReactTestChild[] | null {
const { children } = node.props || node;
if (!children) {
return null;
}
const arrayOfChildren = flattenNestedArrays(
Array.isArray(children) ? children : [children],
) as ChildrenFiberOrInternalInstance[];
return arrayOfChildren.filter(reactFalsyValuesFilter).map((child) => {
// If child is any non-object value (number, string), return it as is
if (typeof child !== "object") {
return child;
}
return {
$$typeof: testSymbol,
type: getType(child),
props: getProps(child),
children: getChildrenFromProps(child),
};
});
}
/**
* Convert structures like `[<div />, <div />, [<div />, [<div />, <div />]]]` to `[<div />, <div />, <div />, <div />, <div />]`
*/
// biome-ignore lint/suspicious/noExplicitAny: We are very generic here, you can really pass anything
function flattenNestedArrays(array: any[]): any[] {
return array.reduce((acc, value) => {
if (Array.isArray(value)) {
return acc.concat(flattenNestedArrays(value));
}
return acc.concat(value);
}, []);
}
/**
* Filter falsy values from React children
*/
// biome-ignore lint/suspicious/noExplicitAny: We are very generic here, you can really pass anything
function reactFalsyValuesFilter(value: any): boolean {
return ![undefined, null, false].includes(value);
}