Skip to content

Commit 4e91f04

Browse files
gaearonljharb
authored andcommitted
Detect Fiber types to avoid inlining constants
1 parent b7c054c commit 4e91f04

File tree

2 files changed

+81
-22
lines changed

2 files changed

+81
-22
lines changed

packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,10 @@ import {
3737
ensureKeyOrUndefined,
3838
} from 'enzyme-adapter-utils';
3939
import findCurrentFiberUsingSlowPath from './findCurrentFiberUsingSlowPath';
40+
import detectFiberTags from './detectFiberTags';
4041

41-
const HostRoot = 3;
42-
const ClassComponent = 2;
43-
const FragmentType = 10;
44-
const FunctionalComponent = 1;
45-
const HostPortal = 4;
46-
const HostComponent = 5;
47-
const HostText = 6;
48-
const Mode = 11;
49-
const ContextConsumerType = 12;
50-
const ContextProviderType = 13;
51-
const ForwardRefType = 14;
42+
// Lazily populated if DOM is available.
43+
let FiberTags = null;
5244

5345
function nodeAndSiblingsArray(nodeWithSibling) {
5446
const array = [];
@@ -115,9 +107,9 @@ function toTree(vnode) {
115107
// somewhere else. Should talk to sebastian about this perhaps
116108
const node = findCurrentFiberUsingSlowPath(vnode);
117109
switch (node.tag) {
118-
case HostRoot: // 3
110+
case FiberTags.HostRoot:
119111
return childrenToTree(node.child);
120-
case HostPortal: { // 4
112+
case FiberTags.HostPortal: {
121113
const {
122114
stateNode: { containerInfo },
123115
memoizedProps: children,
@@ -133,7 +125,7 @@ function toTree(vnode) {
133125
rendered: childrenToTree(node.child),
134126
};
135127
}
136-
case ClassComponent:
128+
case FiberTags.ClassComponent:
137129
return {
138130
nodeType: 'class',
139131
type: node.type,
@@ -143,7 +135,7 @@ function toTree(vnode) {
143135
instance: node.stateNode,
144136
rendered: childrenToTree(node.child),
145137
};
146-
case FunctionalComponent: // 1
138+
case FiberTags.FunctionalComponent:
147139
return {
148140
nodeType: 'function',
149141
type: node.type,
@@ -154,7 +146,7 @@ function toTree(vnode) {
154146
rendered: childrenToTree(node.child),
155147
};
156148

157-
case HostComponent: { // 5
149+
case FiberTags.HostComponent: {
158150
let renderedNodes = flatten(nodeAndSiblingsArray(node.child).map(toTree));
159151
if (renderedNodes.length === 0) {
160152
renderedNodes = [node.memoizedProps.children];
@@ -169,14 +161,14 @@ function toTree(vnode) {
169161
rendered: renderedNodes,
170162
};
171163
}
172-
case HostText: // 6
164+
case FiberTags.HostText:
173165
return node.memoizedProps;
174-
case FragmentType: // 10
175-
case Mode: // 11
176-
case ContextProviderType: // 13
177-
case ContextConsumerType: // 12
166+
case FiberTags.Fragment:
167+
case FiberTags.Mode:
168+
case FiberTags.ContextProvider:
169+
case FiberTags.ContextConsumer:
178170
return childrenToTree(node.child);
179-
case ForwardRefType: {
171+
case FiberTags.ForwardRef: {
180172
return {
181173
nodeType: 'function',
182174
type: node.type,
@@ -255,6 +247,10 @@ class ReactSixteenAdapter extends EnzymeAdapter {
255247

256248
createMountRenderer(options) {
257249
assertDomAvailable('mount');
250+
if (FiberTags === null) {
251+
// Requires DOM.
252+
FiberTags = detectFiberTags();
253+
}
258254
const { attachTo, hydrateIn } = options;
259255
const domNode = hydrateIn || attachTo || global.document.createElement('div');
260256
let instance = null;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
function getFiber(element) {
5+
const container = global.document.createElement('div');
6+
let inst = null;
7+
class Tester extends React.Component {
8+
render() {
9+
inst = this;
10+
return element;
11+
}
12+
}
13+
ReactDOM.render(React.createElement(Tester), container);
14+
return inst._reactInternalFiber.child;
15+
}
16+
17+
module.exports = function detectFiberTags() {
18+
const supportsMode = typeof React.StrictMode !== 'undefined';
19+
const supportsContext = typeof React.createContext !== 'undefined';
20+
const supportsForwardRef = typeof React.forwardRef !== 'undefined';
21+
22+
function Fn() {
23+
return null;
24+
}
25+
// eslint-disable-next-line react/prefer-stateless-function
26+
class Cls extends React.Component {
27+
render() {
28+
return null;
29+
}
30+
}
31+
let Ctx = null;
32+
let FwdRef = null;
33+
if (supportsContext) {
34+
Ctx = React.createContext();
35+
}
36+
if (supportsForwardRef) {
37+
// React will warn if we don't have both arguments.
38+
// eslint-disable-next-line no-unused-vars
39+
FwdRef = React.forwardRef((props, ref) => null);
40+
}
41+
42+
return {
43+
HostRoot: getFiber('test').return.return.tag, // Go two levels above to find the root
44+
ClassComponent: getFiber(React.createElement(Cls)).tag,
45+
Fragment: getFiber([['nested']]).tag,
46+
FunctionalComponent: getFiber(React.createElement(Fn)).tag,
47+
HostPortal: getFiber(ReactDOM.createPortal(null, global.document.createElement('div'))).tag,
48+
HostComponent: getFiber(React.createElement('span')).tag,
49+
HostText: getFiber('text').tag,
50+
Mode: supportsMode
51+
? getFiber(React.createElement(React.StrictMode)).tag
52+
: -1,
53+
ContextConsumer: supportsContext
54+
? getFiber(React.createElement(Ctx.Consumer, null, () => null)).tag
55+
: -1,
56+
ContextProvider: supportsContext
57+
? getFiber(React.createElement(Ctx.Provider, { value: null })).tag
58+
: -1,
59+
ForwardRef: supportsForwardRef
60+
? getFiber(React.createElement(FwdRef)).tag
61+
: -1,
62+
};
63+
};

0 commit comments

Comments
 (0)