From 4149a21983fa0bdad8e5c60059add34016756d0c Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 2 Apr 2019 23:31:40 -0700 Subject: [PATCH] [enzyme-adapter-react-16] [fix] properly render memoized SFCs Fixes #2038 --- .../src/ReactSixteenAdapter.js | 9 +++++-- .../test/ReactWrapper-spec.jsx | 24 ++++++++++--------- .../test/shared/methods/find.jsx | 3 +-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js index cbe6750fc..10282550b 100644 --- a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js +++ b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js @@ -177,7 +177,11 @@ function toTree(vnode) { instance: node.stateNode, rendered: childrenToTree(node.child.child), }; - case FiberTags.MemoSFC: + case FiberTags.MemoSFC: { + let renderedNodes = flatten(nodeAndSiblingsArray(node.child).map(toTree)); + if (renderedNodes.length === 0) { + renderedNodes = [node.memoizedProps.children]; + } return { nodeType: 'function', type: node.elementType.type, @@ -185,8 +189,9 @@ function toTree(vnode) { key: ensureKeyOrUndefined(node.key), ref: node.ref, instance: null, - rendered: childrenToTree(node.child.child), + rendered: renderedNodes, }; + } case FiberTags.HostComponent: { let renderedNodes = flatten(nodeAndSiblingsArray(node.child).map(toTree)); if (renderedNodes.length === 0) { diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index a29f0742d..e4042bff0 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -601,18 +601,20 @@ describeWithDOM('mount', () => { const wrapper = mount(); expect(wrapper.debug()).to.equal(` - -
- - Hello - +
+ +
+ + Hello + +
+
+
+ bar +
+
+ qux
- -
- bar -
-
- qux
`); expect(wrapper.find('InnerComp')).to.have.lengthOf(1); diff --git a/packages/enzyme-test-suite/test/shared/methods/find.jsx b/packages/enzyme-test-suite/test/shared/methods/find.jsx index be3d252b5..2d301f6a3 100644 --- a/packages/enzyme-test-suite/test/shared/methods/find.jsx +++ b/packages/enzyme-test-suite/test/shared/methods/find.jsx @@ -857,8 +857,7 @@ export default function describeFind({ }); describeIf(is('>= 16.6'), 'React.memo', () => { - // FIXME: fix bug for mount - itIf(!isMount, 'works with an SFC', () => { + it('works with an SFC', () => { const InnerComp = () =>
Hello
; const InnerFoo = ({ foo }) => (