-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ReactSixteenAdapter.js
993 lines (916 loc) · 29 KB
/
ReactSixteenAdapter.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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/* eslint no-use-before-define: 0 */
import React from 'react';
import ReactDOM from 'react-dom';
// eslint-disable-next-line import/no-unresolved
import ReactDOMServer from 'react-dom/server';
// eslint-disable-next-line import/no-unresolved
import ShallowRenderer from 'react-test-renderer/shallow';
import { version as testRendererVersion } from 'react-test-renderer/package.json';
// eslint-disable-next-line import/no-unresolved
import TestUtils from 'react-dom/test-utils';
import semver from 'semver';
import checkPropTypes from 'prop-types/checkPropTypes';
import has from 'has';
import {
AsyncMode,
ConcurrentMode,
ContextConsumer,
ContextProvider,
Element,
ForwardRef,
Fragment,
isContextConsumer,
isContextProvider,
isElement,
isForwardRef,
isPortal,
isSuspense,
isValidElementType,
Lazy,
Memo,
Portal,
Profiler,
StrictMode,
Suspense,
} from 'react-is';
import { EnzymeAdapter } from 'enzyme';
import { typeOfNode } from 'enzyme/build/Utils';
import shallowEqual from 'enzyme-shallow-equal';
import {
displayNameOfNode,
elementToTree as utilElementToTree,
nodeTypeFromType as utilNodeTypeFromType,
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
withSetStateAllowed,
createRenderWrapper,
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
wrap,
getMaskedContext,
getComponentStack,
RootFinder,
getNodeFromRootFinder,
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
compareNodeTypeOf,
spyMethod,
} from 'enzyme-adapter-utils';
import findCurrentFiberUsingSlowPath from './findCurrentFiberUsingSlowPath';
import detectFiberTags from './detectFiberTags';
const is164 = !!TestUtils.Simulate.touchStart; // 16.4+
const is165 = !!TestUtils.Simulate.auxClick; // 16.5+
const is166 = is165 && !React.unstable_AsyncMode; // 16.6+
const is168 = is166 && typeof TestUtils.act === 'function';
const hasShouldComponentUpdateBug = semver.satisfies(testRendererVersion, '< 16.8');
// Lazily populated if DOM is available.
let FiberTags = null;
function nodeAndSiblingsArray(nodeWithSibling) {
const array = [];
let node = nodeWithSibling;
while (node != null) {
array.push(node);
node = node.sibling;
}
return array;
}
function flatten(arr) {
const result = [];
const stack = [{ i: 0, array: arr }];
while (stack.length) {
const n = stack.pop();
while (n.i < n.array.length) {
const el = n.array[n.i];
n.i += 1;
if (Array.isArray(el)) {
stack.push(n);
stack.push({ i: 0, array: el });
break;
}
result.push(el);
}
}
return result;
}
function nodeTypeFromType(type) {
if (type === Portal) {
return 'portal';
}
return utilNodeTypeFromType(type);
}
function isMemo(type) {
return compareNodeTypeOf(type, Memo);
}
function isLazy(type) {
return compareNodeTypeOf(type, Lazy);
}
function unmemoType(type) {
return isMemo(type) ? type.type : type;
}
function transformSuspense(renderedEl, prerenderEl, { suspenseFallback }) {
if (!isSuspense(renderedEl)) {
return renderedEl;
}
let { children } = renderedEl.props;
if (suspenseFallback) {
const { fallback } = renderedEl.props;
children = replaceLazyWithFallback(children, fallback);
}
const {
propTypes,
defaultProps,
contextTypes,
contextType,
childContextTypes,
} = renderedEl.type;
const FakeSuspense = Object.assign(
isStateful(prerenderEl.type)
? class FakeSuspense extends prerenderEl.type {
render() {
const { type, props } = prerenderEl;
return React.createElement(
type,
{ ...props, ...this.props },
children,
);
}
}
: function FakeSuspense(props) { // eslint-disable-line prefer-arrow-callback
return React.createElement(
renderedEl.type,
{ ...renderedEl.props, ...props },
children,
);
},
{
propTypes,
defaultProps,
contextTypes,
contextType,
childContextTypes,
},
);
return React.createElement(FakeSuspense, null, children);
}
function elementToTree(el) {
if (!isPortal(el)) {
return utilElementToTree(el, elementToTree);
}
const { children, containerInfo } = el;
const props = { children, containerInfo };
return {
nodeType: 'portal',
type: Portal,
props,
key: ensureKeyOrUndefined(el.key),
ref: el.ref || null,
instance: null,
rendered: elementToTree(el.children),
};
}
function toTree(vnode) {
if (vnode == null) {
return null;
}
// TODO(lmr): I'm not really sure I understand whether or not this is what
// i should be doing, or if this is a hack for something i'm doing wrong
// somewhere else. Should talk to sebastian about this perhaps
const node = findCurrentFiberUsingSlowPath(vnode);
switch (node.tag) {
case FiberTags.HostRoot:
return childrenToTree(node.child);
case FiberTags.HostPortal: {
const {
stateNode: { containerInfo },
memoizedProps: children,
} = node;
const props = { containerInfo, children };
return {
nodeType: 'portal',
type: Portal,
props,
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
}
case FiberTags.ClassComponent:
return {
nodeType: 'class',
type: node.type,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: node.stateNode,
rendered: childrenToTree(node.child),
};
case FiberTags.FunctionalComponent:
return {
nodeType: 'function',
type: node.type,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
case FiberTags.MemoClass:
return {
nodeType: 'class',
type: node.elementType.type,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: node.stateNode,
rendered: childrenToTree(node.child.child),
};
case FiberTags.MemoSFC: {
let renderedNodes = flatten(nodeAndSiblingsArray(node.child).map(toTree));
if (renderedNodes.length === 0) {
renderedNodes = childrenToTree(node.memoizedProps.children);
}
return {
nodeType: 'function',
type: node.elementType,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: renderedNodes,
};
}
case FiberTags.HostComponent: {
let renderedNodes = flatten(nodeAndSiblingsArray(node.child).map(toTree));
if (renderedNodes.length === 0) {
renderedNodes = [node.memoizedProps.children];
}
return {
nodeType: 'host',
type: node.type,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: node.stateNode,
rendered: renderedNodes,
};
}
case FiberTags.HostText:
return node.memoizedProps;
case FiberTags.Fragment:
case FiberTags.Mode:
case FiberTags.ContextProvider:
case FiberTags.ContextConsumer:
return childrenToTree(node.child);
case FiberTags.Profiler:
case FiberTags.ForwardRef: {
return {
nodeType: 'function',
type: node.type,
props: { ...node.pendingProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
}
case FiberTags.Suspense: {
return {
nodeType: 'function',
type: Suspense,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
}
case FiberTags.Lazy:
return childrenToTree(node.child);
default:
throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`);
}
}
function childrenToTree(node) {
if (!node) {
return null;
}
const children = nodeAndSiblingsArray(node);
if (children.length === 0) {
return null;
}
if (children.length === 1) {
return toTree(children[0]);
}
return flatten(children.map(toTree));
}
function nodeToHostNode(_node) {
// NOTE(lmr): node could be a function component
// which wont have an instance prop, but we can get the
// host node associated with its return value at that point.
// Although this breaks down if the return value is an array,
// as is possible with React 16.
let node = _node;
while (node && !Array.isArray(node) && node.instance === null) {
node = node.rendered;
}
// if the SFC returned null effectively, there is no host node.
if (!node) {
return null;
}
const mapper = (item) => {
if (item && item.instance) return ReactDOM.findDOMNode(item.instance);
return null;
};
if (Array.isArray(node)) {
return node.map(mapper);
}
if (Array.isArray(node.rendered) && node.nodeType === 'class') {
return node.rendered.map(mapper);
}
return mapper(node);
}
function replaceLazyWithFallback(node, fallback) {
if (!node) {
return null;
}
if (Array.isArray(node)) {
return node.map((el) => replaceLazyWithFallback(el, fallback));
}
if (isLazy(node.type)) {
return fallback;
}
return {
...node,
props: {
...node.props,
children: replaceLazyWithFallback(node.props.children, fallback),
},
};
}
const eventOptions = {
animation: true,
pointerEvents: is164,
auxClick: is165,
};
function getEmptyStateValue() {
// this handles a bug in React 16.0 - 16.2
// see https://github.com/facebook/react/commit/39be83565c65f9c522150e52375167568a2a1459
// also see https://github.com/facebook/react/pull/11965
// eslint-disable-next-line react/prefer-stateless-function
class EmptyState extends React.Component {
render() {
return null;
}
}
const testRenderer = new ShallowRenderer();
testRenderer.render(React.createElement(EmptyState));
return testRenderer._instance.state;
}
function wrapAct(fn) {
if (!is168) {
return fn();
}
let returnVal;
TestUtils.act(() => { returnVal = fn(); });
return returnVal;
}
function getProviderDefaultValue(Provider) {
// React stores references to the Provider's defaultValue differently across versions.
if ('_defaultValue' in Provider._context) {
return Provider._context._defaultValue;
}
if ('_currentValue' in Provider._context) {
return Provider._context._currentValue;
}
throw new Error('Enzyme Internal Error: can’t figure out how to get Provider’s default value');
}
function makeFakeElement(type) {
return { $$typeof: Element, type };
}
function isStateful(Component) {
return Component.prototype && (
Component.prototype.isReactComponent
|| Array.isArray(Component.__reactAutoBindPairs) // fallback for createClass components
);
}
class ReactSixteenAdapter extends EnzymeAdapter {
constructor() {
super();
const { lifecycles } = this.options;
this.options = {
...this.options,
enableComponentDidUpdateOnSetState: true, // TODO: remove, semver-major
legacyContextMode: 'parent',
lifecycles: {
...lifecycles,
componentDidUpdate: {
onSetState: true,
},
getDerivedStateFromProps: {
hasShouldComponentUpdateBug,
},
getSnapshotBeforeUpdate: true,
setState: {
skipsComponentDidUpdateOnNullish: true,
},
getChildContext: {
calledByRenderer: false,
},
getDerivedStateFromError: is166,
},
};
}
createMountRenderer(options) {
assertDomAvailable('mount');
if (has(options, 'suspenseFallback')) {
throw new TypeError('`suspenseFallback` is not supported by the `mount` renderer');
}
if (FiberTags === null) {
// Requires DOM.
FiberTags = detectFiberTags();
}
const { attachTo, hydrateIn, wrappingComponentProps } = options;
const domNode = hydrateIn || attachTo || global.document.createElement('div');
let instance = null;
const adapter = this;
return {
render(el, context, callback) {
return wrapAct(() => {
if (instance === null) {
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
wrappingComponentProps,
context,
...(ref && { refProp: ref }),
};
const ReactWrapperComponent = createMountWrapper(el, { ...options, adapter });
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = hydrateIn
? ReactDOM.hydrate(wrappedEl, domNode)
: ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
}
} else {
instance.setChildProps(el.props, context, callback);
}
});
},
unmount() {
ReactDOM.unmountComponentAtNode(domNode);
instance = null;
},
getNode() {
if (!instance) {
return null;
}
return getNodeFromRootFinder(
adapter.isCustomComponent,
toTree(instance._reactInternalFiber),
options,
);
},
simulateError(nodeHierarchy, rootNode, error) {
const isErrorBoundary = ({ instance: elInstance, type }) => {
if (is166 && type && type.getDerivedStateFromError) {
return true;
}
return elInstance && elInstance.componentDidCatch;
};
const {
instance: catchingInstance,
type: catchingType,
} = nodeHierarchy.find(isErrorBoundary) || {};
simulateError(
error,
catchingInstance,
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode.bind(adapter),
is166 ? catchingType : undefined,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
if (!eventFn) {
throw new TypeError(`ReactWrapper::simulate() event '${event}' does not exist`);
}
wrapAct(() => {
eventFn(adapter.nodeToHostNode(node), mock);
});
},
batchedUpdates(fn) {
return fn();
// return ReactDOM.unstable_batchedUpdates(fn);
},
getWrappingComponentRenderer() {
return {
...this,
...getWrappingComponentMountRenderer({
toTree: (inst) => toTree(inst._reactInternalFiber),
getMountWrapperInstance: () => instance,
}),
};
},
...(is168 && { wrapInvoke: wrapAct }),
};
}
createShallowRenderer(options = {}) {
const adapter = this;
const renderer = new ShallowRenderer();
const { suspenseFallback } = options;
if (typeof suspenseFallback !== 'undefined' && typeof suspenseFallback !== 'boolean') {
throw TypeError('`options.suspenseFallback` should be boolean or undefined');
}
let isDOM = false;
let cachedNode = null;
let lastComponent = null;
let wrappedComponent = null;
const sentinel = {};
// wrap memo components with a PureComponent, or a class component with sCU
const wrapPureComponent = (Component, compare) => {
if (!is166) {
throw new RangeError('this function should not be called in React < 16.6. Please report this!');
}
if (lastComponent !== Component) {
if (isStateful(Component)) {
wrappedComponent = class extends Component {}; // eslint-disable-line react/prefer-stateless-function
if (compare) {
wrappedComponent.prototype.shouldComponentUpdate = (nextProps) => !compare(this.props, nextProps);
} else {
wrappedComponent.prototype.isPureReactComponent = true;
}
} else {
let memoized = sentinel;
let prevProps;
wrappedComponent = function (props, ...args) {
const shouldUpdate = memoized === sentinel || (compare
? !compare(prevProps, props)
: !shallowEqual(prevProps, props)
);
if (shouldUpdate) {
memoized = Component({ ...Component.defaultProps, ...props }, ...args);
prevProps = props;
}
return memoized;
};
}
Object.assign(
wrappedComponent,
Component,
{ displayName: adapter.displayNameOfNode({ type: Component }) },
);
lastComponent = Component;
}
return wrappedComponent;
};
// Wrap functional components on versions prior to 16.5,
// to avoid inadvertently pass a `this` instance to it.
const wrapFunctionalComponent = (Component) => {
if (is166 && has(Component, 'defaultProps')) {
if (lastComponent !== Component) {
wrappedComponent = Object.assign(
// eslint-disable-next-line new-cap
(props, ...args) => Component({ ...Component.defaultProps, ...props }, ...args),
Component,
{ displayName: adapter.displayNameOfNode({ type: Component }) },
);
lastComponent = Component;
}
return wrappedComponent;
}
if (is165) {
return Component;
}
if (lastComponent !== Component) {
wrappedComponent = Object.assign(
(...args) => Component(...args), // eslint-disable-line new-cap
Component,
);
lastComponent = Component;
}
return wrappedComponent;
};
const renderElement = (elConfig, ...rest) => {
const renderedEl = renderer.render(elConfig, ...rest);
const typeIsExisted = !!(renderedEl && renderedEl.type);
if (is166 && typeIsExisted) {
const clonedEl = transformSuspense(renderedEl, elConfig, { suspenseFallback });
const elementIsChanged = clonedEl.type !== renderedEl.type;
if (elementIsChanged) {
return renderer.render({ ...elConfig, type: clonedEl.type }, ...rest);
}
}
return renderedEl;
};
return {
render(el, unmaskedContext, {
providerValues = new Map(),
} = {}) {
cachedNode = el;
/* eslint consistent-return: 0 */
if (typeof el.type === 'string') {
isDOM = true;
} else if (isContextProvider(el)) {
providerValues.set(el.type, el.props.value);
const MockProvider = Object.assign(
(props) => props.children,
el.type,
);
return withSetStateAllowed(() => renderElement({ ...el, type: MockProvider }));
} else if (isContextConsumer(el)) {
const Provider = adapter.getProviderFromConsumer(el.type);
const value = providerValues.has(Provider)
? providerValues.get(Provider)
: getProviderDefaultValue(Provider);
const MockConsumer = Object.assign(
(props) => props.children(value),
el.type,
);
return withSetStateAllowed(() => renderElement({ ...el, type: MockConsumer }));
} else {
isDOM = false;
let renderedEl = el;
if (isLazy(renderedEl)) {
throw TypeError('`React.lazy` is not supported by shallow rendering.');
}
renderedEl = transformSuspense(renderedEl, renderedEl, { suspenseFallback });
const { type: Component } = renderedEl;
const context = getMaskedContext(Component.contextTypes, unmaskedContext);
if (isMemo(el.type)) {
const { type: InnerComp, compare } = el.type;
return withSetStateAllowed(() => renderElement(
{ ...el, type: wrapPureComponent(InnerComp, compare) },
context,
));
}
const isComponentStateful = isStateful(Component);
if (!isComponentStateful && typeof Component === 'function') {
return withSetStateAllowed(() => renderElement(
{ ...renderedEl, type: wrapFunctionalComponent(Component) },
context,
));
}
if (isComponentStateful) {
if (
renderer._instance
&& el.props === renderer._instance.props
&& !shallowEqual(context, renderer._instance.context)
) {
const { restore } = spyMethod(
renderer,
'_updateClassComponent',
(originalMethod) => function _updateClassComponent(...args) {
const { props } = renderer._instance;
const clonedProps = { ...props };
renderer._instance.props = clonedProps;
const result = originalMethod.apply(renderer, args);
renderer._instance.props = props;
restore();
return result;
},
);
}
// fix react bug; see implementation of `getEmptyStateValue`
const emptyStateValue = getEmptyStateValue();
if (emptyStateValue) {
Object.defineProperty(Component.prototype, 'state', {
configurable: true,
enumerable: true,
get() {
return null;
},
set(value) {
if (value !== emptyStateValue) {
Object.defineProperty(this, 'state', {
configurable: true,
enumerable: true,
value,
writable: true,
});
}
},
});
}
}
return withSetStateAllowed(() => renderElement(renderedEl, context));
}
},
unmount() {
renderer.unmount();
},
getNode() {
if (isDOM) {
return elementToTree(cachedNode);
}
const output = renderer.getRenderOutput();
return {
nodeType: nodeTypeFromType(cachedNode.type),
type: cachedNode.type,
props: cachedNode.props,
key: ensureKeyOrUndefined(cachedNode.key),
ref: cachedNode.ref,
instance: renderer._instance,
rendered: Array.isArray(output)
? flatten(output).map((el) => elementToTree(el))
: elementToTree(output),
};
},
simulateError(nodeHierarchy, rootNode, error) {
simulateError(
error,
renderer._instance,
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode.bind(adapter),
is166 ? cachedNode.type : undefined,
);
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
// TODO(lmr): emulate React's event propagation
// ReactDOM.unstable_batchedUpdates(() => {
handler(...args);
// });
});
}
},
batchedUpdates(fn) {
return fn();
// return ReactDOM.unstable_batchedUpdates(fn);
},
checkPropTypes(typeSpecs, values, location, hierarchy) {
return checkPropTypes(
typeSpecs,
values,
location,
displayNameOfNode(cachedNode),
() => getComponentStack(hierarchy.concat([cachedNode])),
);
},
};
}
createStringRenderer(options) {
if (has(options, 'suspenseFallback')) {
throw new TypeError('`suspenseFallback` should not be specified in options of string renderer');
}
return {
render(el, context) {
if (options.context && (el.type.contextTypes || options.childContextTypes)) {
const childContextTypes = {
...(el.type.contextTypes || {}),
...options.childContextTypes,
};
const ContextWrapper = createRenderWrapper(el, context, childContextTypes);
return ReactDOMServer.renderToStaticMarkup(React.createElement(ContextWrapper));
}
return ReactDOMServer.renderToStaticMarkup(el);
},
};
}
// Provided a bag of options, return an `EnzymeRenderer`. Some options can be implementation
// specific, like `attach` etc. for React, but not part of this interface explicitly.
// eslint-disable-next-line class-methods-use-this
createRenderer(options) {
switch (options.mode) {
case EnzymeAdapter.MODES.MOUNT: return this.createMountRenderer(options);
case EnzymeAdapter.MODES.SHALLOW: return this.createShallowRenderer(options);
case EnzymeAdapter.MODES.STRING: return this.createStringRenderer(options);
default:
throw new Error(`Enzyme Internal Error: Unrecognized mode: ${options.mode}`);
}
}
wrap(element) {
return wrap(element);
}
// converts an RSTNode to the corresponding JSX Pragma Element. This will be needed
// in order to implement the `Wrapper.mount()` and `Wrapper.shallow()` methods, but should
// be pretty straightforward for people to implement.
// eslint-disable-next-line class-methods-use-this
nodeToElement(node) {
if (!node || typeof node !== 'object') return null;
const { type } = node;
return React.createElement(unmemoType(type), propsWithKeysAndRef(node));
}
// eslint-disable-next-line class-methods-use-this
matchesElementType(node, matchingType) {
if (!node) {
return node;
}
const { type } = node;
return unmemoType(type) === unmemoType(matchingType);
}
elementToNode(element) {
return elementToTree(element);
}
nodeToHostNode(node, supportsArray = false) {
const nodes = nodeToHostNode(node);
if (Array.isArray(nodes) && !supportsArray) {
return nodes[0];
}
return nodes;
}
displayNameOfNode(node) {
if (!node) return null;
const { type, $$typeof } = node;
const adapter = this;
const nodeType = type || $$typeof;
// newer node types may be undefined, so only test if the nodeType exists
if (nodeType) {
switch (nodeType) {
case (is166 ? ConcurrentMode : AsyncMode) || NaN: return is166 ? 'ConcurrentMode' : 'AsyncMode';
case Fragment || NaN: return 'Fragment';
case StrictMode || NaN: return 'StrictMode';
case Profiler || NaN: return 'Profiler';
case Portal || NaN: return 'Portal';
case Suspense || NaN: return 'Suspense';
default:
}
}
const $$typeofType = type && type.$$typeof;
switch ($$typeofType) {
case ContextConsumer || NaN: return 'ContextConsumer';
case ContextProvider || NaN: return 'ContextProvider';
case Memo || NaN: {
const nodeName = displayNameOfNode(node);
return typeof nodeName === 'string' ? nodeName : `Memo(${adapter.displayNameOfNode(type)})`;
}
case ForwardRef || NaN: {
if (type.displayName) {
return type.displayName;
}
const name = adapter.displayNameOfNode({ type: type.render });
return name ? `ForwardRef(${name})` : 'ForwardRef';
}
case Lazy || NaN: {
return 'lazy';
}
default: return displayNameOfNode(node);
}
}
isValidElement(element) {
return isElement(element);
}
isValidElementType(object) {
return !!object && isValidElementType(object);
}
isFragment(fragment) {
return typeOfNode(fragment) === Fragment;
}
isCustomComponent(type) {
const fakeElement = makeFakeElement(type);
return !!type && (
typeof type === 'function'
|| isForwardRef(fakeElement)
|| isContextProvider(fakeElement)
|| isContextConsumer(fakeElement)
|| isSuspense(fakeElement)
);
}
isContextConsumer(type) {
return !!type && isContextConsumer(makeFakeElement(type));
}
isCustomComponentElement(inst) {
if (!inst || !this.isValidElement(inst)) {
return false;
}
return this.isCustomComponent(inst.type);
}
getProviderFromConsumer(Consumer) {
// React stores references to the Provider on a Consumer differently across versions.
if (Consumer) {
let Provider;
if (Consumer._context) { // check this first, to avoid a deprecation warning
({ Provider } = Consumer._context);
} else if (Consumer.Provider) {
({ Provider } = Consumer);
}
if (Provider) {
return Provider;
}
}
throw new Error('Enzyme Internal Error: can’t figure out how to get Provider from Consumer');
}
createElement(...args) {
return React.createElement(...args);
}
wrapWithWrappingComponent(node, options) {
return {
RootFinder,
node: wrapWithWrappingComponent(React.createElement, node, options),
};
}
}
export default ReactSixteenAdapter;