Skip to content

Commit

Permalink
Fix an issue that broke serialization (#2642)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-davis-sonarsource committed Mar 23, 2023
1 parent 314a5fb commit fd87787
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/jest-serialization-failure.md
@@ -0,0 +1,5 @@
---
'@emotion/jest': patch
---

Fix an issue that broke serialization. Nodes in a render prop aren't writable, making the `delete node.props.className` instruction throw.
5 changes: 4 additions & 1 deletion packages/jest/src/create-serializer.js
Expand Up @@ -172,7 +172,10 @@ function clean(node: any, classNames: string[]) {
const { className } = node.props
if (!className) {
// if it's empty, remove it
delete node.props.className
const descriptor = Object.getOwnPropertyDescriptor(node, 'props')
if (descriptor && descriptor.writable) {
delete node.props.className
}
} else {
const hasKnownClass = hasIntersection(className.split(' '), classNames)
if (hasKnownClass) {
Expand Down
26 changes: 26 additions & 0 deletions packages/jest/test/__snapshots__/react-enzyme.test.js.snap
Expand Up @@ -440,6 +440,24 @@ exports[`enzyme mount with prop containing css element with other props 1`] = `
</Greeting>
`;

exports[`enzyme mount with prop containing empty classname 1`] = `
<Thing
renderProp={
<span
className=""
>
Hi
</span>
}
>
<div>
<span>
Hi
</span>
</div>
</Thing>
`;

exports[`enzyme mount with prop containing regular element 1`] = `
<Test
element={
Expand Down Expand Up @@ -796,6 +814,14 @@ exports[`enzyme shallow with prop containing css element with other props 1`] =
</div>
`;

exports[`enzyme shallow with prop containing empty classname 1`] = `
<div>
<span>
Hi
</span>
</div>
`;

exports[`enzyme shallow with prop containing regular element 1`] = `
<button>
Foo
Expand Down
8 changes: 8 additions & 0 deletions packages/jest/test/react-enzyme.test.js
Expand Up @@ -187,6 +187,14 @@ const cases = {
)
}
},
'with prop containing empty classname': {
render() {
const Thing = ({ renderProp }) => {
return <div>{renderProp}</div>
}
return <Thing renderProp={<span className="">Hi</span>} />
}
},
'with array of styles as css prop': {
render() {
const style1 = css`
Expand Down

0 comments on commit fd87787

Please sign in to comment.