Skip to content

Commit ce90865

Browse files
Fix an issue that broke serialization (#2642)
1 parent acb72a4 commit ce90865

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/jest': patch
3+
---
4+
5+
Fix an issue that broke serialization. Nodes in a render prop aren't writable, making the `delete node.props.className` instruction throw.

packages/jest/src/create-serializer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ function clean(node: any, classNames: string[]) {
172172
const { className } = node.props
173173
if (!className) {
174174
// if it's empty, remove it
175-
delete node.props.className
175+
const descriptor = Object.getOwnPropertyDescriptor(node, 'props')
176+
if (descriptor && descriptor.writable) {
177+
delete node.props.className
178+
}
176179
} else {
177180
const hasKnownClass = hasIntersection(className.split(' '), classNames)
178181
if (hasKnownClass) {

packages/jest/test/__snapshots__/react-enzyme.test.js.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,24 @@ exports[`enzyme mount with prop containing css element with other props 1`] = `
440440
</Greeting>
441441
`;
442442

443+
exports[`enzyme mount with prop containing empty classname 1`] = `
444+
<Thing
445+
renderProp={
446+
<span
447+
className=""
448+
>
449+
Hi
450+
</span>
451+
}
452+
>
453+
<div>
454+
<span>
455+
Hi
456+
</span>
457+
</div>
458+
</Thing>
459+
`;
460+
443461
exports[`enzyme mount with prop containing regular element 1`] = `
444462
<Test
445463
element={
@@ -796,6 +814,14 @@ exports[`enzyme shallow with prop containing css element with other props 1`] =
796814
</div>
797815
`;
798816

817+
exports[`enzyme shallow with prop containing empty classname 1`] = `
818+
<div>
819+
<span>
820+
Hi
821+
</span>
822+
</div>
823+
`;
824+
799825
exports[`enzyme shallow with prop containing regular element 1`] = `
800826
<button>
801827
Foo

packages/jest/test/react-enzyme.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ const cases = {
187187
)
188188
}
189189
},
190+
'with prop containing empty classname': {
191+
render() {
192+
const Thing = ({ renderProp }) => {
193+
return <div>{renderProp}</div>
194+
}
195+
return <Thing renderProp={<span className="">Hi</span>} />
196+
}
197+
},
190198
'with array of styles as css prop': {
191199
render() {
192200
const style1 = css`

0 commit comments

Comments
 (0)