Skip to content

Commit 400e9b5

Browse files
cincodenadafritz-cEll Bradshaw
authored
Fix diff on unchanged (#419)
* create failing tests for indent config compliance * use new serializer plugin API * pass indentation config to css.stringify Helps the library comply with Jest's serializer's indent configuration, which in turn fixes #355 * add comments to serialize util in tests * Manual updates post-rebase - Update test for new class format - Upgrade pretty-format to reduce indirect deps Co-authored-by: fritz-c <[email protected]> Co-authored-by: Ell Bradshaw <[email protected]>
1 parent 804ffa9 commit 400e9b5

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"preact": "^10.0.0-beta.2",
4646
"preact-render-to-json": "^3.6.6",
4747
"prettier": "^2.3.2",
48+
"pretty-format": "^27.5.1",
4849
"react": "^17.0.2",
4950
"react-dom": "^17.0.2",
5051
"react-is": "^17.0.2",

src/styleSheetSerializer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ const getAtRules = (ast, filter) =>
7070
return acc.concat(atRule);
7171
}, []);
7272

73-
const getStyle = (classNames) => {
73+
const getStyle = (classNames, config = {}) => {
7474
const ast = getCSS();
7575
const filter = filterRules(classNames);
7676
const rules = ast.stylesheet.rules.filter(filter);
7777
const atRules = getAtRules(ast, filter);
7878

7979
ast.stylesheet.rules = rules.concat(atRules);
8080

81-
return css.stringify(ast);
81+
return css.stringify(ast, { indent: config.indent });
8282
};
8383

8484
const getClassNamesFromSelectorsByHashes = (classNames, hashes) => {
@@ -137,7 +137,7 @@ module.exports = {
137137
);
138138
},
139139

140-
print(val, print) {
140+
serialize(val, config, indentation, depth, refs, printer) {
141141
const nodes = getNodes(val);
142142
nodes.forEach(cache.add, cache);
143143

@@ -149,9 +149,9 @@ module.exports = {
149149
classNames = filterClassNames(classNames, hashes);
150150
unreferencedClassNames = filterUnreferencedClassNames(unreferencedClassNames, hashes);
151151

152-
const style = getStyle(classNames);
152+
const style = getStyle(classNames, config);
153153
const classNamesToReplace = getClassNamesFromSelectorsByHashes(classNames, hashes);
154-
const code = print(val);
154+
const code = printer(val, config, indentation, depth, refs);
155155

156156
let result = serializerOptions.addStyles ? `${style}${style ? '\n\n' : ''}${code}` : code;
157157
result = stripUnreferencedClassNames(result, unreferencedClassNames);

test/styleSheetSerializer.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { mount, shallow } from 'enzyme';
33
import React from 'react';
44
import renderer from 'react-test-renderer';
55
import styled, { ThemeContext, ThemeProvider } from 'styled-components';
6+
import prettyFormat, { plugins } from 'pretty-format';
67
import {setStyleSheetSerializerOptions} from '../serializer';
8+
import styleSheetSerializer from '../src/styleSheetSerializer';
79

810
const toMatchSnapshot = (component) => {
911
expect(renderer.create(component).toJSON()).toMatchSnapshot('react-test-renderer');
@@ -19,6 +21,31 @@ const shallowWithTheme = (tree, theme) => {
1921
return shallow(tree);
2022
};
2123

24+
/**
25+
* Serializes a value similarly to jest-snapshot
26+
*
27+
* Imitates the serialization logic inside jest-snapshot,
28+
* so we can pass options, including indent, to it.
29+
*
30+
* @see https://github.com/facebook/jest/blob/615084195ae1ae61ddd56162c62bbdda17587569/packages/jest-snapshot/src/utils.ts#L155-L163
31+
* @see https://github.com/facebook/jest/blob/615084195ae1ae61ddd56162c62bbdda17587569/packages/jest-snapshot/src/plugins.ts#L24-L32
32+
*/
33+
const serialize = (val, indent = 2) =>
34+
prettyFormat(val, {
35+
escapeRegex: true,
36+
indent,
37+
plugins: [
38+
styleSheetSerializer,
39+
plugins.ReactTestComponent,
40+
plugins.ReactElement,
41+
plugins.DOMElement,
42+
plugins.DOMCollection,
43+
plugins.Immutable,
44+
plugins.AsymmetricMatcher,
45+
],
46+
printFunctionName: false,
47+
});
48+
2249
it('null', () => {
2350
expect(null).toMatchSnapshot();
2451
});
@@ -346,3 +373,35 @@ it('allows to set a css classNameFormatter', () => {
346373
</div>
347374
);
348375
});
376+
377+
it('responds to indent configuration', () => {
378+
const Link = styled.a`
379+
color: blue;
380+
`;
381+
382+
const mounted = renderer.create(<Link>Styled, exciting Link</Link>);
383+
384+
expect(serialize(mounted)).toMatchInlineSnapshot(`
385+
".styledComponent0 {
386+
color: blue;
387+
}
388+
389+
<a
390+
className=\\"styledComponent0\\"
391+
>
392+
Styled, exciting Link
393+
</a>"
394+
`);
395+
396+
expect(serialize(mounted, 0)).toMatchInlineSnapshot(`
397+
".styledComponent0 {
398+
color: blue;
399+
}
400+
401+
<a
402+
className=\\"styledComponent0\\"
403+
>
404+
Styled, exciting Link
405+
</a>"
406+
`);
407+
});

typings/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, NewPlugin } from 'pretty-format'
1+
import { NewPlugin } from 'pretty-format'
22
import { css } from 'styled-components'
33

44
declare global {
@@ -27,6 +27,6 @@ export interface StyledComponentsSerializerOptions {
2727
classNameFormatter?: (index: number) => string
2828
}
2929

30-
export declare const styleSheetSerializer: Exclude<Plugin, NewPlugin> & {
30+
export declare const styleSheetSerializer: NewPlugin & {
3131
setStyleSheetSerializerOptions: (options?: StyledComponentsSerializerOptions) => void
32-
};
32+
};

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6200,6 +6200,15 @@ pretty-format@^27.0.2, pretty-format@^27.3.1:
62006200
ansi-styles "^5.0.0"
62016201
react-is "^17.0.1"
62026202

6203+
pretty-format@^27.5.1:
6204+
version "27.5.1"
6205+
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.5.1.tgz#2181879fdea51a7a5851fb39d920faa63f01d88e"
6206+
integrity sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ==
6207+
dependencies:
6208+
ansi-regex "^5.0.1"
6209+
ansi-styles "^5.0.0"
6210+
react-is "^17.0.1"
6211+
62036212
process-nextick-args@~2.0.0:
62046213
version "2.0.1"
62056214
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"

0 commit comments

Comments
 (0)