Skip to content

Commit

Permalink
Merge pull request #331 from immitsu/issue-262
Browse files Browse the repository at this point in the history
fix: shallow render with fragment at top lvl
  • Loading branch information
rschristian authored May 8, 2024
2 parents 0faec39 + 714c90e commit e2c8fa1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-rockets-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix for shallow rendering incorrectly transforming Fragments into other nodes
6 changes: 5 additions & 1 deletion src/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ function _renderToStringPretty(
// components
if (typeof nodeName === 'function') {
isComponent = true;
if (opts.shallow && (inner || opts.renderRootComponent === false)) {
if (
opts.shallow &&
(inner || opts.renderRootComponent === false) &&
nodeName !== Fragment
) {
nodeName = getComponentName(nodeName);
} else if (nodeName === Fragment) {
const children = [];
Expand Down
28 changes: 21 additions & 7 deletions test/shallowRender.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,26 @@ describe('shallowRender()', () => {
expect(Test).to.have.been.calledOnce;
});

it('should ignore Fragments', () => {
let rendered = shallowRender(
<Fragment>
<div>foo</div>
</Fragment>
);
expect(rendered).to.equal(`<div>foo</div>`);
describe('should ignore Fragments', () => {
it('passed directly', () => {
let rendered = shallowRender(
<Fragment>
<div>foo</div>
</Fragment>
);
expect(rendered).to.equal(`<div>foo</div>`);
});

it('passed from FC', () => {
const Test = () => (
<Fragment>
<div>foo</div>
</Fragment>
);

let rendered = shallowRender(<Test />);

expect(rendered).to.equal(`<div>foo</div>`);
});
});
});

0 comments on commit e2c8fa1

Please sign in to comment.