Skip to content

Commit c661cb9

Browse files
artolaljharb
authored andcommitted
[enzyme-adapter-react-16] [fix] support lazy components
1 parent 41bf606 commit c661cb9

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ function toTree(vnode) {
218218
};
219219
}
220220
case FiberTags.ClassComponent:
221+
case FiberTags.ClassComponentLazy:
221222
return {
222223
nodeType: 'class',
223224
type: node.type,
@@ -228,6 +229,7 @@ function toTree(vnode) {
228229
rendered: childrenToTree(node.child),
229230
};
230231
case FiberTags.FunctionalComponent:
232+
case FiberTags.FunctionalComponentLazy:
231233
return {
232234
nodeType: 'function',
233235
type: node.type,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,22 @@ module.exports = function detectFiberTags() {
5151
function Fn() {
5252
return null;
5353
}
54+
function LazyFn() {
55+
throw Promise.resolve();
56+
}
5457
// eslint-disable-next-line react/prefer-stateless-function
5558
class Cls extends React.Component {
5659
render() {
5760
return null;
5861
}
5962
}
63+
// eslint-disable-next-line react/prefer-stateless-function
64+
class LazyCls extends React.Component {
65+
// eslint-disable-next-line react/require-render-return
66+
render() {
67+
throw Promise.resolve();
68+
}
69+
}
6070
let Ctx = null;
6171
let FwdRef = null;
6272
let LazyComponent = null;
@@ -75,8 +85,10 @@ module.exports = function detectFiberTags() {
7585
return {
7686
HostRoot: getFiber('test').return.return.tag, // Go two levels above to find the root
7787
ClassComponent: getFiber(React.createElement(Cls)).tag,
88+
ClassComponentLazy: supportsSuspense ? getLazyFiber(LazyCls).tag : -1,
7889
Fragment: getFiber([['nested']]).tag,
7990
FunctionalComponent: getFiber(React.createElement(Fn)).tag,
91+
FunctionalComponentLazy: supportsSuspense ? getLazyFiber(LazyFn).tag : -1,
8092
MemoSFC: supportsMemo
8193
? getFiber(React.createElement(React.memo(Fn))).tag
8294
: -1,

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,55 @@ describeWithDOM('mount', () => {
13211321
});
13221322
});
13231323

1324+
describeIf(is('>= 16.6'), 'Suspense & lazy component', () => {
1325+
class Fallback extends React.Component {
1326+
render() {
1327+
return (
1328+
<div>Fallback</div>
1329+
);
1330+
}
1331+
}
1332+
1333+
it('finds fallback when given lazy class component', () => {
1334+
class Component extends React.Component {
1335+
// eslint-disable-next-line react/require-render-return
1336+
render() {
1337+
throw Promise.resolve();
1338+
}
1339+
}
1340+
1341+
const SuspenseComponent = () => (
1342+
<Suspense fallback={Fallback}>
1343+
<Component />
1344+
</Suspense>
1345+
);
1346+
1347+
const wrapper = mount(<SuspenseComponent />);
1348+
1349+
expect(wrapper.is(SuspenseComponent)).to.equal(true);
1350+
expect(wrapper.find(Component)).to.have.lengthOf(1);
1351+
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
1352+
});
1353+
1354+
it('finds fallback when given lazy functional component', () => {
1355+
function Component() {
1356+
throw Promise.resolve();
1357+
}
1358+
1359+
const SuspenseComponent = () => (
1360+
<Suspense fallback={Fallback}>
1361+
<Component />
1362+
</Suspense>
1363+
);
1364+
1365+
const wrapper = mount(<SuspenseComponent />);
1366+
1367+
expect(wrapper.is(SuspenseComponent)).to.equal(true);
1368+
expect(wrapper.find(Component)).to.have.lengthOf(1);
1369+
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
1370+
});
1371+
});
1372+
13241373
describe('.mount()', () => {
13251374
it('calls componentWillUnmount()', () => {
13261375
const willMount = sinon.spy();

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,55 @@ describe('shallow', () => {
20772077
});
20782078
});
20792079

2080+
describeIf(is('>= 16.6'), 'Suspense & lazy component', () => {
2081+
class Fallback extends React.Component {
2082+
render() {
2083+
return (
2084+
<div>Fallback</div>
2085+
);
2086+
}
2087+
}
2088+
2089+
it('finds fallback when given lazy class component', () => {
2090+
class Component extends React.Component {
2091+
// eslint-disable-next-line react/require-render-return
2092+
render() {
2093+
throw Promise.resolve();
2094+
}
2095+
}
2096+
2097+
const SuspenseComponent = () => (
2098+
<Suspense fallback={Fallback}>
2099+
<Component />
2100+
</Suspense>
2101+
);
2102+
2103+
const wrapper = shallow(<SuspenseComponent />, { suspenseFallback: true });
2104+
2105+
expect(wrapper.is(Suspense)).to.equal(true);
2106+
expect(wrapper.find(Component)).to.have.lengthOf(1);
2107+
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
2108+
});
2109+
2110+
it('finds fallback when given lazy functional component', () => {
2111+
function Component() {
2112+
throw Promise.resolve();
2113+
}
2114+
2115+
const SuspenseComponent = () => (
2116+
<Suspense fallback={Fallback}>
2117+
<Component />
2118+
</Suspense>
2119+
);
2120+
2121+
const wrapper = shallow(<SuspenseComponent />, { suspenseFallback: true });
2122+
2123+
expect(wrapper.is(Suspense)).to.equal(true);
2124+
expect(wrapper.find(Component)).to.have.lengthOf(1);
2125+
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
2126+
});
2127+
});
2128+
20802129
describe('lifecycle methods', () => {
20812130
describe('disableLifecycleMethods option', () => {
20822131
describe('validation', () => {

0 commit comments

Comments
 (0)