Skip to content

Commit 4e38622

Browse files
[pigment] Improve testing of fixtures (#41389)
Co-authored-by: siriwatknp <siriwatkunaporn@gmail.com>
1 parent 62b6ad3 commit 4e38622

21 files changed

Lines changed: 306 additions & 115 deletions

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/packages/pigment-react/processors/
2424
/packages/pigment-react/exports/
2525
/packages/pigment-react/theme/
26-
/packages/pigment-react/tests/fixtures/
26+
/packages/pigment-react/tests/**/fixtures
2727
/packages/pigment-nextjs-plugin/loader.js
2828
# Ignore fixtures
2929
/packages-internal/scripts/typescript-to-proptypes/test/*/*

packages/pigment-react/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"copy-license": "node ../../scripts/pigment-license.mjs",
2727
"build": "tsup",
2828
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/pigment-react/**/*.test.{js,ts,tsx}'",
29+
"test:update": "cd ../../ && cross-env NODE_ENV=test UPDATE_FIXTURES=true mocha 'packages/pigment-react/**/*.test.{js,ts,tsx}'",
2930
"test:ci": "cd ../../ && cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=lcov --report-dir=./coverage/pigment-react mocha 'packages/pigment-react/**/*.test.{js,ts,tsx}'",
3031
"typecheck": "tsc --noEmit -p ."
3132
},
@@ -53,12 +54,14 @@
5354
"@types/babel__core": "^7.20.5",
5455
"@types/babel__helper-module-imports": "^7.18.3",
5556
"@types/babel__helper-plugin-utils": "^7.10.3",
57+
"@types/chai": "^4.3.12",
5658
"@types/cssesc": "^3.0.2",
5759
"@types/lodash": "^4.14.202",
5860
"@types/node": "^18.19.21",
5961
"@types/react": "^18.2.55",
6062
"@types/stylis": "^4.2.5",
6163
"chai": "^4.4.1",
64+
"prettier": "^3.2.5",
6265
"react": "^18.2.0"
6366
},
6467
"peerDependencies": {
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1-
# Adding new fixtures
1+
# Pigment CSS testing
22

3-
Create a new file name with `[name].input.js` and add `styled`, `css` or other zero-runtime calls into the file. Also add equivalent `[name].output.js` and `[name].output.css` and run the test. After the new test fails, get the results from the received output and add it to the equivalent js and css files. This is equivalent to snapshot testing and will make sure any change in internal css generation logic does not fail any other existing tests.
3+
## Folder structure
4+
5+
- `tests` is the root folder for all tests
6+
- `fixtures` contains all the fixtures for the tests
7+
- `*.input.js` are the input files created manually
8+
- `*.output.*` are the expected output files created by running the tests
9+
- `*.test.js` are the test files that run the fixtures
10+
11+
## Running tests
12+
13+
At the root project terminal:
14+
15+
```bash
16+
pnpm nx run @pigment-css/react:test
17+
```
18+
19+
To update the output fixtures:
20+
21+
```bash
22+
pnpm nx run @pigment-css/react:test:update
23+
```
24+
25+
## Adding new tests
26+
27+
Each folder inside `tests` is a Pigment CSS feature. To add a new test, create a new folder with the feature name and add a new test file with the `.test.js` extension. Inside the test file, import the fixtures and run the tests.
28+
29+
## Adding new fixtures
30+
31+
Create a new file name with `[name].input.js` and add `styled`, `css` or other Pigment CSS calls into the file.
32+
33+
The first time you run the tests, the output files will be created automatically. Then check the output files to make sure they are correct.
34+
35+
For any implementation changes after that, if you want to update the output files, run the tests with the `test:update` script.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import path from 'node:path';
2+
import { runTransformation, expect } from '../testUtils';
3+
4+
const theme = {
5+
palette: {
6+
primary: {
7+
main: 'red',
8+
},
9+
},
10+
size: {
11+
font: {
12+
h1: '3rem',
13+
},
14+
},
15+
components: {
16+
MuiSlider: {
17+
styleOverrides: {
18+
rail: {
19+
fontSize: '3rem',
20+
},
21+
},
22+
},
23+
},
24+
};
25+
26+
describe('Pigment CSS - css', () => {
27+
it('basics', async () => {
28+
const { output, fixture } = await runTransformation(
29+
path.join(__dirname, 'fixtures/css.input.js'),
30+
{
31+
themeArgs: {
32+
theme,
33+
},
34+
},
35+
);
36+
37+
expect(output.js).to.equal(fixture.js);
38+
expect(output.css).to.equal(fixture.css);
39+
});
40+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { css } from '@pigment-css/react';
2+
3+
const cls1 = css`
4+
color: ${({ theme }) => theme.palette.primary.main};
5+
font-size: ${({ theme }) => theme.size.font.h1};
6+
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.c1wr0t7p {
2+
color: red;
3+
font-size: 3rem;
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const cls1 = 'c1wr0t7p';

packages/pigment-react/tests/fixtures/styled.output.css

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/pigment-react/tests/fixtures/styled.output.js

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { keyframes } from '@pigment-css/react';
2+
3+
const rotateKeyframe = keyframes({
4+
from: {
5+
transform: 'rotate(360deg)',
6+
},
7+
to: {
8+
transform: 'rotate(0deg)',
9+
},
10+
});

0 commit comments

Comments
 (0)