Skip to content

Commit 19894b8

Browse files
committed
Add basic test setup
1 parent e0f02c2 commit 19894b8

9 files changed

+5016
-9
lines changed

Diff for: babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env', '@babel/preset-react'],
3+
};

Diff for: jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
testEnvironment: 'jsdom',
3+
moduleFileExtensions: ['js'],
4+
testMatch: ['**/test/**/*.js'],
5+
};

Diff for: package.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Jest snapshot serializer for JSS",
55
"main": "src/jss-snapshot-serializer.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest"
88
},
99
"repository": {
1010
"type": "git",
@@ -22,5 +22,16 @@
2222
"bugs": {
2323
"url": "https://github.com/cssinjs/jss-snapshot-serializer/issues"
2424
},
25-
"homepage": "https://github.com/cssinjs/jss-snapshot-serializer#readme"
25+
"homepage": "https://github.com/cssinjs/jss-snapshot-serializer#readme",
26+
"devDependencies": {
27+
"@babel/preset-env": "^7.13.9",
28+
"@babel/preset-react": "^7.12.13",
29+
"@testing-library/react": "^11.2.5",
30+
"babel-jest": "^26.6.3",
31+
"jest": "^26.6.3",
32+
"react": "^17.0.1",
33+
"react-dom": "^17.0.1",
34+
"react-jss": "^10.5.1",
35+
"styled-jss": "^2.2.3"
36+
}
2637
}

Diff for: src/jss-snapshot-serializer.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ const collectElements = (element, elements = []) => {
1010
elements.push(element);
1111

1212
if (element.children) {
13-
element.children.forEach(child => collectElements(child, elements));
13+
element.children.forEach((child) => collectElements(child, elements));
1414
}
1515

1616
return elements;
1717
};
1818

19-
const markElements = nodes => nodes.forEach((element) => {
20-
element[MARKER] = true;
21-
});
19+
const markElements = (nodes) =>
20+
nodes.forEach((element) => {
21+
element[MARKER] = true;
22+
});
2223

2324
const replaceJssClassNames = (elements) => {
2425
elements.forEach((element) => {
@@ -40,7 +41,10 @@ const replaceJssClassNames = (elements) => {
4041
if (!jssClassNameRegexp.test(className)) return className;
4142

4243
const lastDashPosition = className.lastIndexOf('-');
43-
const secondLastDashPosition = className.lastIndexOf('-', lastDashPosition - 1);
44+
const secondLastDashPosition = className.lastIndexOf(
45+
'-',
46+
lastDashPosition - 1
47+
);
4448

4549
return className.substring(0, secondLastDashPosition);
4650
});
@@ -49,11 +53,14 @@ const replaceJssClassNames = (elements) => {
4953
});
5054
};
5155

52-
5356
module.exports = {
5457
test(value) {
5558
// apply the serializer only to react elements that we haven't marked(processed) before
56-
return value && !value[MARKER] && value.$$typeof === Symbol.for('react.test.json');
59+
return (
60+
value &&
61+
!value[MARKER] &&
62+
value.$$typeof === Symbol.for('react.test.json')
63+
);
5764
},
5865

5966
print(value, serialize) {

Diff for: test/__snapshots__/react-jss.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`react-jss removes non-deterministic class names 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="redBox-0-2-1"
7+
/>
8+
</DocumentFragment>
9+
`;

Diff for: test/__snapshots__/styled-jss.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`styled-jss removes non-deterministic class names 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="div-1-0-1-1"
7+
/>
8+
</DocumentFragment>
9+
`;

Diff for: test/react-jss.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { createUseStyles } from 'react-jss';
3+
import { render } from '@testing-library/react';
4+
import serializer from '../src/jss-snapshot-serializer';
5+
6+
expect.addSnapshotSerializer(serializer);
7+
8+
const useStyles = createUseStyles({
9+
redBox: {
10+
width: 100,
11+
height: 100,
12+
backgroundColor: 'red',
13+
},
14+
});
15+
16+
const RedBox = () => {
17+
const classes = useStyles();
18+
return <div className={classes.redBox} />;
19+
};
20+
21+
describe('react-jss', () => {
22+
it('removes non-deterministic class names', () => {
23+
const redBox = render(<RedBox />).asFragment();
24+
expect(redBox).toMatchSnapshot();
25+
});
26+
});

Diff for: test/styled-jss.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import styled from 'styled-jss';
3+
import { render } from '@testing-library/react';
4+
import serializer from '../src/jss-snapshot-serializer';
5+
6+
expect.addSnapshotSerializer(serializer);
7+
8+
const RedBox = styled('div')({
9+
width: 100,
10+
height: 100,
11+
backgroundColor: 'red',
12+
});
13+
14+
describe('styled-jss', () => {
15+
it('removes non-deterministic class names', () => {
16+
const redBox = render(<RedBox />).asFragment();
17+
expect(redBox).toMatchSnapshot();
18+
});
19+
});

0 commit comments

Comments
 (0)