Skip to content

Commit eb5b7a3

Browse files
committed
support lazy components
1 parent f046079 commit eb5b7a3

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
@@ -1285,6 +1285,55 @@ describeWithDOM('mount', () => {
12851285
});
12861286
});
12871287

1288+
describeIf(is('>= 16.6'), 'Suspense & lazy component', () => {
1289+
class Fallback extends React.Component {
1290+
render() {
1291+
return (
1292+
<div>Fallback</div>
1293+
);
1294+
}
1295+
}
1296+
1297+
it('finds fallback when given lazy class component', () => {
1298+
class Component extends React.Component {
1299+
// eslint-disable-next-line react/require-render-return
1300+
render() {
1301+
throw Promise.resolve();
1302+
}
1303+
}
1304+
1305+
const SuspenseComponent = () => (
1306+
<Suspense fallback={Fallback}>
1307+
<Component />
1308+
</Suspense>
1309+
);
1310+
1311+
const wrapper = mount(<SuspenseComponent />);
1312+
1313+
expect(wrapper.is(SuspenseComponent)).to.equal(true);
1314+
expect(wrapper.find(Component)).to.have.lengthOf(1);
1315+
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
1316+
});
1317+
1318+
it('finds fallback when given lazy functional component', () => {
1319+
function Component() {
1320+
throw Promise.resolve();
1321+
}
1322+
1323+
const SuspenseComponent = () => (
1324+
<Suspense fallback={Fallback}>
1325+
<Component />
1326+
</Suspense>
1327+
);
1328+
1329+
const wrapper = mount(<SuspenseComponent />);
1330+
1331+
expect(wrapper.is(SuspenseComponent)).to.equal(true);
1332+
expect(wrapper.find(Component)).to.have.lengthOf(1);
1333+
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
1334+
});
1335+
});
1336+
12881337
describe('.mount()', () => {
12891338
it('calls componentWillUnmount()', () => {
12901339
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)