Skip to content

Commit 5497740

Browse files
authored
Strip static class names from jest snapshot results (#320)
1 parent f8e6f79 commit 5497740

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/styleSheetSerializer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const getClassNames = nodes =>
4444
}, new Set());
4545

4646
const filterClassNames = (classNames, hashes) => classNames.filter(className => hashes.includes(className));
47+
const filterUnreferencedClassNames = (classNames, hashes) => classNames.filter(className => className.startsWith('sc-') && !hashes.includes(className));
4748

4849
const includesClassNames = (classNames, selectors) =>
4950
classNames.some(className => selectors.some(selector => selector.includes(className)));
@@ -91,6 +92,10 @@ const replaceClassNames = (result, classNames, style) =>
9192
.filter(className => style.includes(className))
9293
.reduce((acc, className, index) => acc.replace(new RegExp(className, 'g'), `c${index++}`), result);
9394

95+
const stripUnreferencedClassNames = (result, classNames) =>
96+
classNames
97+
.reduce((acc, className) => acc.replace(new RegExp(`${className}\\s?`,'g'), ''), result);
98+
9499
const replaceHashes = (result, hashes) =>
95100
hashes.reduce(
96101
(acc, className) => acc.replace(new RegExp(`((class|className)="[^"]*?)${className}\\s?([^"]*")`, 'g'), '$1$3'),
@@ -113,13 +118,17 @@ module.exports = {
113118
const hashes = getHashes();
114119

115120
let classNames = [...getClassNames(nodes)];
121+
let unreferencedClassNames = classNames;
122+
116123
classNames = filterClassNames(classNames, hashes);
124+
unreferencedClassNames = filterUnreferencedClassNames(unreferencedClassNames, hashes);
117125

118126
const style = getStyle(classNames);
119127
const classNamesToReplace = getClassNamesFromSelectorsByHashes(classNames, hashes);
120128
const code = print(val);
121129

122130
let result = `${style}${style ? '\n\n' : ''}${code}`;
131+
result = stripUnreferencedClassNames(result, unreferencedClassNames);
123132
result = replaceClassNames(result, classNamesToReplace, style);
124133
result = replaceHashes(result, hashes);
125134

test/__snapshots__/styleSheetSerializer.spec.js.snap

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,64 @@ exports[`referring to other components: react-testing-library 1`] = `
647647
</a>
648648
`;
649649

650+
exports[`referring to other unreferenced components: mount 1`] = `
651+
.c0 {
652+
font-size: 1.5em;
653+
color: palevioletred;
654+
font-weight: bold;
655+
}
656+
657+
<div>
658+
<Styled(styled.a)>
659+
<a
660+
className="c0"
661+
>
662+
Styled, exciting Link
663+
</a>
664+
</Styled(styled.a)>
665+
</div>
666+
`;
667+
668+
exports[`referring to other unreferenced components: react-test-renderer 1`] = `
669+
.c0 {
670+
font-size: 1.5em;
671+
color: palevioletred;
672+
font-weight: bold;
673+
}
674+
675+
<div>
676+
<a
677+
className="c0"
678+
>
679+
Styled, exciting Link
680+
</a>
681+
</div>
682+
`;
683+
684+
exports[`referring to other unreferenced components: react-testing-library 1`] = `
685+
.c0 {
686+
font-size: 1.5em;
687+
color: palevioletred;
688+
font-weight: bold;
689+
}
690+
691+
<div>
692+
<a
693+
class="c0"
694+
>
695+
Styled, exciting Link
696+
</a>
697+
</div>
698+
`;
699+
700+
exports[`referring to other unreferenced components: shallow 1`] = `
701+
<div>
702+
<Styled(styled.a)>
703+
Styled, exciting Link
704+
</Styled(styled.a)>
705+
</div>
706+
`;
707+
650708
exports[`shallow with theme 1`] = `
651709
.c0 {
652710
color: mediumseagreen;

test/styleSheetSerializer.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,20 @@ it('referring to other components', () => {
250250
expect(mount(component)).toMatchSnapshot('mount');
251251
expect(render(component).container.firstChild).toMatchSnapshot('react-testing-library');
252252
});
253+
254+
it('referring to other unreferenced components', () => {
255+
const UnreferencedLink = styled.a`
256+
font-size: 1.5em;
257+
`
258+
259+
const ReferencedLink = styled(UnreferencedLink)`
260+
color: palevioletred;
261+
font-weight: bold;
262+
`
263+
264+
toMatchSnapshot(
265+
<div>
266+
<ReferencedLink>Styled, exciting Link</ReferencedLink>
267+
</div>
268+
);
269+
});

0 commit comments

Comments
 (0)