Skip to content

Commit

Permalink
[enzyme-adapter-react-16] [fix] shallow: properly dive through `mem…
Browse files Browse the repository at this point in the history
…o` components

Fixes #2103
  • Loading branch information
ljharb committed May 6, 2019
1 parent f478970 commit d885c34
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Expand Up @@ -717,7 +717,8 @@ class ReactSixteenAdapter extends EnzymeAdapter {
// eslint-disable-next-line class-methods-use-this
nodeToElement(node) {
if (!node || typeof node !== 'object') return null;
return React.createElement(node.type, propsWithKeysAndRef(node));
const { type } = node;
return React.createElement(isMemo(type) ? type.type : type, propsWithKeysAndRef(node));
}

elementToNode(element) {
Expand Down
28 changes: 28 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -29,6 +29,7 @@ import {
useEffect,
useState,
Profiler,
memo,
} from './_helpers/react-compat';
import {
describeIf,
Expand Down Expand Up @@ -1592,6 +1593,33 @@ describe('shallow', () => {
underwater = wrapper.dive({ context: { foo: 'enzyme!' } });
expect(underwater.context()).to.deep.equal({ foo: 'enzyme!' });
});

describeIf(is('>= 16.6'), 'memo', () => {
const App = () => <div>Guest</div>;

const AppMemoized = memo && Object.assign(memo(App), { displayName: 'AppMemoized' });

const RendersApp = () => <App />;
const RendersAppMemoized = () => <AppMemoized />;

it('works without memoizing', () => {
const wrapper = shallow(<RendersApp />);
expect(wrapper.debug()).to.equal('<App />');
expect(wrapper.dive().debug()).to.equal(`<div>
Guest
</div>`);
expect(() => wrapper.dive().dive()).to.throw(TypeError);
});

it('works with memoizing', () => {
const wrapper = shallow(<RendersAppMemoized />);
expect(wrapper.debug()).to.equal('<App />');
expect(wrapper.dive().debug()).to.equal(`<div>
Guest
</div>`);
expect(() => wrapper.dive().dive()).to.throw(TypeError);
});
});
});

describeIf(is('>= 16.6'), 'Suspense & lazy', () => {
Expand Down

0 comments on commit d885c34

Please sign in to comment.