Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support lazy components #2521

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function toTree(vnode) {
};
}
case FiberTags.ClassComponent:
case FiberTags.ClassComponentLazy:
return {
nodeType: 'class',
type: node.type,
Expand All @@ -228,6 +229,7 @@ function toTree(vnode) {
rendered: childrenToTree(node.child),
};
case FiberTags.FunctionalComponent:
case FiberTags.FunctionalComponentLazy:
return {
nodeType: 'function',
type: node.type,
Expand Down
12 changes: 12 additions & 0 deletions packages/enzyme-adapter-react-16/src/detectFiberTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ module.exports = function detectFiberTags() {
function Fn() {
return null;
}
function LazyFn() {
throw Promise.resolve();
}
// eslint-disable-next-line react/prefer-stateless-function
class Cls extends React.Component {
render() {
return null;
}
}
// eslint-disable-next-line react/prefer-stateless-function
class LazyCls extends React.Component {
// eslint-disable-next-line react/require-render-return
render() {
throw Promise.resolve();
}
}
let Ctx = null;
let FwdRef = null;
let LazyComponent = null;
Expand All @@ -75,8 +85,10 @@ module.exports = function detectFiberTags() {
return {
HostRoot: getFiber('test').return.return.tag, // Go two levels above to find the root
ClassComponent: getFiber(React.createElement(Cls)).tag,
ClassComponentLazy: supportsSuspense ? getLazyFiber(LazyCls).tag : -1,
Fragment: getFiber([['nested']]).tag,
FunctionalComponent: getFiber(React.createElement(Fn)).tag,
FunctionalComponentLazy: supportsSuspense ? getLazyFiber(LazyFn).tag : -1,
MemoSFC: supportsMemo
? getFiber(React.createElement(React.memo(Fn))).tag
: -1,
Expand Down
49 changes: 49 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,55 @@ describeWithDOM('mount', () => {
});
});

describeIf(is('>= 16.6'), 'Suspense & lazy component', () => {
class Fallback extends React.Component {
render() {
return (
<div>Fallback</div>
);
}
}

it('finds fallback when given lazy class component', () => {
class Component extends React.Component {
// eslint-disable-next-line react/require-render-return
render() {
throw Promise.resolve();
}
}

const SuspenseComponent = () => (
<Suspense fallback={Fallback}>
<Component />
</Suspense>
);

const wrapper = mount(<SuspenseComponent />);

expect(wrapper.is(SuspenseComponent)).to.equal(true);
expect(wrapper.find(Component)).to.have.lengthOf(1);
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
});

it('finds fallback when given lazy functional component', () => {
function Component() {
throw Promise.resolve();
}

const SuspenseComponent = () => (
<Suspense fallback={Fallback}>
<Component />
</Suspense>
);

const wrapper = mount(<SuspenseComponent />);

expect(wrapper.is(SuspenseComponent)).to.equal(true);
expect(wrapper.find(Component)).to.have.lengthOf(1);
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
});
});

describe('.mount()', () => {
it('calls componentWillUnmount()', () => {
const willMount = sinon.spy();
Expand Down
49 changes: 49 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,55 @@ describe('shallow', () => {
});
});

describeIf(is('>= 16.6'), 'Suspense & lazy component', () => {
class Fallback extends React.Component {
render() {
return (
<div>Fallback</div>
);
}
}

it('finds fallback when given lazy class component', () => {
class Component extends React.Component {
// eslint-disable-next-line react/require-render-return
render() {
throw Promise.resolve();
}
}

const SuspenseComponent = () => (
<Suspense fallback={Fallback}>
<Component />
</Suspense>
);

const wrapper = shallow(<SuspenseComponent />, { suspenseFallback: true });

expect(wrapper.is(Suspense)).to.equal(true);
expect(wrapper.find(Component)).to.have.lengthOf(1);
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
});

it('finds fallback when given lazy functional component', () => {
function Component() {
throw Promise.resolve();
}

const SuspenseComponent = () => (
<Suspense fallback={Fallback}>
<Component />
</Suspense>
);

const wrapper = shallow(<SuspenseComponent />, { suspenseFallback: true });

expect(wrapper.is(Suspense)).to.equal(true);
expect(wrapper.find(Component)).to.have.lengthOf(1);
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
});
});

describe('lifecycle methods', () => {
describe('disableLifecycleMethods option', () => {
describe('validation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default function describeIsEmptyRender({
expect(elements.isEmptyRender()).to.equal(!isShallow);
});

it('works on a memoized functional component', () => {
itIf(!!memo, 'works on a memoized functional component', () => {
const Component = memo(() => null);
const wrapper = Wrap(<Component />);
expect(wrapper.debug()).to.equal(isShallow ? '' : '<Memo() />');
Expand Down