Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip static class names from jest snapshot results #320

Merged
merged 1 commit into from Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>
);
});