Skip to content

Commit e2c8fa1

Browse files
authored
Merge pull request #331 from immitsu/issue-262
fix: shallow render with fragment at top lvl
2 parents 0faec39 + 714c90e commit e2c8fa1

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

.changeset/fast-rockets-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'preact-render-to-string': patch
3+
---
4+
5+
Fix for shallow rendering incorrectly transforming Fragments into other nodes

src/pretty.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ function _renderToStringPretty(
103103
// components
104104
if (typeof nodeName === 'function') {
105105
isComponent = true;
106-
if (opts.shallow && (inner || opts.renderRootComponent === false)) {
106+
if (
107+
opts.shallow &&
108+
(inner || opts.renderRootComponent === false) &&
109+
nodeName !== Fragment
110+
) {
107111
nodeName = getComponentName(nodeName);
108112
} else if (nodeName === Fragment) {
109113
const children = [];

test/shallowRender.test.jsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,26 @@ describe('shallowRender()', () => {
5252
expect(Test).to.have.been.calledOnce;
5353
});
5454

55-
it('should ignore Fragments', () => {
56-
let rendered = shallowRender(
57-
<Fragment>
58-
<div>foo</div>
59-
</Fragment>
60-
);
61-
expect(rendered).to.equal(`<div>foo</div>`);
55+
describe('should ignore Fragments', () => {
56+
it('passed directly', () => {
57+
let rendered = shallowRender(
58+
<Fragment>
59+
<div>foo</div>
60+
</Fragment>
61+
);
62+
expect(rendered).to.equal(`<div>foo</div>`);
63+
});
64+
65+
it('passed from FC', () => {
66+
const Test = () => (
67+
<Fragment>
68+
<div>foo</div>
69+
</Fragment>
70+
);
71+
72+
let rendered = shallowRender(<Test />);
73+
74+
expect(rendered).to.equal(`<div>foo</div>`);
75+
});
6276
});
6377
});

0 commit comments

Comments
 (0)