Skip to content

Commit 86d0ebf

Browse files
authored
Fix issue #317 (#319)
After the first render the node are marked with KEY property and calling rerender function of react-testing-library the test function will return false preventing the print to work as expected
1 parent 2340376 commit 86d0ebf

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/styleSheetSerializer.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const css = require('css');
22
const { getCSS, getHashes } = require('./utils');
33

4-
const KEY = '__jest-styled-components__';
5-
4+
let cache = new WeakSet()
65
const getNodes = (node, nodes = []) => {
76
if (typeof node === 'object') {
87
nodes.push(node);
@@ -15,8 +14,6 @@ const getNodes = (node, nodes = []) => {
1514
return nodes;
1615
};
1716

18-
const markNodes = (nodes) => nodes.forEach((node) => (node[KEY] = true));
19-
2017
const getClassNamesFromDOM = (node) => Array.from(node.classList);
2118
const getClassNamesFromProps = (node) => {
2219
const classNameProp = node.props && (node.props.class || node.props.className);
@@ -121,11 +118,11 @@ module.exports = {
121118

122119
/**
123120
* Configure jest-styled-components/serializer
124-
*
125-
* @param {{ addStyles?: boolean, classNameFormatter?: (index: number) => string }} options
121+
*
122+
* @param {{ addStyles?: boolean, classNameFormatter?: (index: number) => string }} options
126123
*/
127124
setStyleSheetSerializerOptions(options = {}) {
128-
serializerOptions = {
125+
serializerOptions = {
129126
...serializerOptionDefaults,
130127
...options
131128
};
@@ -134,14 +131,14 @@ module.exports = {
134131
test(val) {
135132
return (
136133
val &&
137-
!val[KEY] &&
134+
!cache.has(val) &&
138135
(val.$$typeof === Symbol.for('react.test.json') || (global.Element && val instanceof global.Element))
139136
);
140137
},
141138

142139
print(val, print) {
143140
const nodes = getNodes(val);
144-
markNodes(nodes);
141+
nodes.forEach(cache.add, cache);
145142

146143
const hashes = getHashes();
147144

@@ -159,7 +156,7 @@ module.exports = {
159156
result = stripUnreferencedClassNames(result, unreferencedClassNames);
160157
result = replaceClassNames(result, classNamesToReplace, style, serializerOptions.classNameFormatter);
161158
result = replaceHashes(result, hashes);
162-
159+
nodes.forEach(cache.delete, cache);
163160
return result;
164161
},
165162
};

test/__snapshots__/styleSheetSerializer.spec.js.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,32 @@ exports[`shallow with theme 1`] = `
896896
</button>
897897
`;
898898

899+
exports[`should match the same snapshot rerendering the same element: first-render 1`] = `
900+
.c0 {
901+
color: palevioletred;
902+
font-weight: bold;
903+
}
904+
905+
<div>
906+
<div
907+
class="c0"
908+
/>
909+
</div>
910+
`;
911+
912+
exports[`should match the same snapshot rerendering the same element: second-render 1`] = `
913+
.c0 {
914+
color: palevioletred;
915+
font-weight: bold;
916+
}
917+
918+
<div>
919+
<div
920+
class="c0"
921+
/>
922+
</div>
923+
`;
924+
899925
exports[`strips unused styles: mount 1`] = `
900926
.c2 {
901927
color: blue;

test/styleSheetSerializer.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ it('referring to other components', () => {
234234
const TextWithConditionalFormatting = styled.span`
235235
${Container} & {
236236
color: yellow;
237-
background-color: ${(props) => (props.error ? 'red' : 'green')};
237+
background-color: ${props => (props.error ? 'red' : 'green')};
238238
}
239239
`;
240240

@@ -301,6 +301,19 @@ it('referring to other unreferenced components', () => {
301301
);
302302
});
303303

304+
it('should match the same snapshot rerendering the same element', () => {
305+
const StyledDiv = styled.div`
306+
color: palevioletred;
307+
font-weight: bold;
308+
`;
309+
310+
const {rerender, container} = render(<StyledDiv />);
311+
312+
expect(container).toMatchSnapshot('first-render');
313+
rerender(<StyledDiv />);
314+
expect(container).toMatchSnapshot('second-render');
315+
});
316+
304317
it('allows to disable css snapshotting', () => {
305318
setStyleSheetSerializerOptions({ addStyles: false })
306319
const A = styled.div`

0 commit comments

Comments
 (0)