From 3b60966bdb72107c0b8bccf3b0310e7de53706eb Mon Sep 17 00:00:00 2001 From: immitsu Date: Sat, 13 Jan 2024 21:58:36 +0300 Subject: [PATCH 1/3] fix: shallow render with fragment at top lvl --- src/pretty.js | 12 ++++++++---- test/shallowRender.test.jsx | 28 +++++++++++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/pretty.js b/src/pretty.js index 70e9e5a8..8f73b509 100644 --- a/src/pretty.js +++ b/src/pretty.js @@ -103,10 +103,12 @@ function _renderToStringPretty( // components if (typeof nodeName === 'function') { isComponent = true; - if (opts.shallow && (inner || opts.renderRootComponent === false)) { - nodeName = getComponentName(nodeName); - } else if (nodeName === Fragment) { - const children = []; + + let isFragment = nodeName === Fragment, + isShallow = opts.shallow && (inner || opts.renderRootComponent === false); + + if (isFragment || (isShallow && isFragment)) { + let children = []; getChildren(children, vnode.props.children); return _renderToStringPretty( children, @@ -116,6 +118,8 @@ function _renderToStringPretty( isSvgMode, selectValue ); + } else if (isShallow) { + nodeName = getComponentName(nodeName); } else { let rendered; diff --git a/test/shallowRender.test.jsx b/test/shallowRender.test.jsx index 8b981f36..ca332445 100644 --- a/test/shallowRender.test.jsx +++ b/test/shallowRender.test.jsx @@ -52,12 +52,26 @@ describe('shallowRender()', () => { expect(Test).to.have.been.calledOnce; }); - it('should ignore Fragments', () => { - let rendered = shallowRender( - -
foo
-
- ); - expect(rendered).to.equal(`
foo
`); + describe('should ignore Fragments', () => { + it('passed directly', () => { + let rendered = shallowRender( + +
foo
+
+ ); + expect(rendered).to.equal(`
foo
`); + }); + + it('passed from FC', () => { + const Test = () => ( + +
foo
+
+ ); + + let rendered = shallowRender(); + + expect(rendered).to.equal(`
foo
`); + }); }); }); From dbc783ef579eb34046ae2e36c2d44d661e358db3 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 8 May 2024 13:19:33 -0500 Subject: [PATCH 2/3] refactor: Simplify fix Co-authored-by: Remko Klein <37383276+remko79@users.noreply.github.com> --- src/pretty.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pretty.js b/src/pretty.js index 8f73b509..59df0e4c 100644 --- a/src/pretty.js +++ b/src/pretty.js @@ -103,12 +103,14 @@ function _renderToStringPretty( // components if (typeof nodeName === 'function') { isComponent = true; - - let isFragment = nodeName === Fragment, - isShallow = opts.shallow && (inner || opts.renderRootComponent === false); - - if (isFragment || (isShallow && isFragment)) { - let children = []; + if ( + opts.shallow && + (inner || opts.renderRootComponent === false) && + nodeName !== Fragment + ) { + nodeName = getComponentName(nodeName); + } else if (nodeName === Fragment) { + const children = []; getChildren(children, vnode.props.children); return _renderToStringPretty( children, @@ -118,8 +120,6 @@ function _renderToStringPretty( isSvgMode, selectValue ); - } else if (isShallow) { - nodeName = getComponentName(nodeName); } else { let rendered; From 714c90e0f104f92645904a5bca9af5c406a60d75 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 8 May 2024 13:23:50 -0500 Subject: [PATCH 3/3] docs: Adding changeset --- .changeset/fast-rockets-admire.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fast-rockets-admire.md diff --git a/.changeset/fast-rockets-admire.md b/.changeset/fast-rockets-admire.md new file mode 100644 index 00000000..ebec71e7 --- /dev/null +++ b/.changeset/fast-rockets-admire.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +Fix for shallow rendering incorrectly transforming Fragments into other nodes