Skip to content

Commit

Permalink
Strip static class names from jest snapshot results (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
blnoonan committed Jul 15, 2020
1 parent f8e6f79 commit 5497740
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/styleSheetSerializer.js
Expand Up @@ -44,6 +44,7 @@ const getClassNames = nodes =>
}, new Set());

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

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

const stripUnreferencedClassNames = (result, classNames) =>
classNames
.reduce((acc, className) => acc.replace(new RegExp(`${className}\\s?`,'g'), ''), result);

const replaceHashes = (result, hashes) =>
hashes.reduce(
(acc, className) => acc.replace(new RegExp(`((class|className)="[^"]*?)${className}\\s?([^"]*")`, 'g'), '$1$3'),
Expand All @@ -113,13 +118,17 @@ module.exports = {
const hashes = getHashes();

let classNames = [...getClassNames(nodes)];
let unreferencedClassNames = classNames;

classNames = filterClassNames(classNames, hashes);
unreferencedClassNames = filterUnreferencedClassNames(unreferencedClassNames, hashes);

const style = getStyle(classNames);
const classNamesToReplace = getClassNamesFromSelectorsByHashes(classNames, hashes);
const code = print(val);

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

Expand Down
58 changes: 58 additions & 0 deletions test/__snapshots__/styleSheetSerializer.spec.js.snap
Expand Up @@ -647,6 +647,64 @@ exports[`referring to other components: react-testing-library 1`] = `
</a>
`;

exports[`referring to other unreferenced components: mount 1`] = `
.c0 {
font-size: 1.5em;
color: palevioletred;
font-weight: bold;
}
<div>
<Styled(styled.a)>
<a
className="c0"
>
Styled, exciting Link
</a>
</Styled(styled.a)>
</div>
`;

exports[`referring to other unreferenced components: react-test-renderer 1`] = `
.c0 {
font-size: 1.5em;
color: palevioletred;
font-weight: bold;
}
<div>
<a
className="c0"
>
Styled, exciting Link
</a>
</div>
`;

exports[`referring to other unreferenced components: react-testing-library 1`] = `
.c0 {
font-size: 1.5em;
color: palevioletred;
font-weight: bold;
}
<div>
<a
class="c0"
>
Styled, exciting Link
</a>
</div>
`;

exports[`referring to other unreferenced components: shallow 1`] = `
<div>
<Styled(styled.a)>
Styled, exciting Link
</Styled(styled.a)>
</div>
`;

exports[`shallow with theme 1`] = `
.c0 {
color: mediumseagreen;
Expand Down
17 changes: 17 additions & 0 deletions test/styleSheetSerializer.spec.js
Expand Up @@ -250,3 +250,20 @@ it('referring to other components', () => {
expect(mount(component)).toMatchSnapshot('mount');
expect(render(component).container.firstChild).toMatchSnapshot('react-testing-library');
});

it('referring to other unreferenced components', () => {
const UnreferencedLink = styled.a`
font-size: 1.5em;
`

const ReferencedLink = styled(UnreferencedLink)`
color: palevioletred;
font-weight: bold;
`

toMatchSnapshot(
<div>
<ReferencedLink>Styled, exciting Link</ReferencedLink>
</div>
);
});

0 comments on commit 5497740

Please sign in to comment.