-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[pigment-css] Improve testing of fixtures #41389
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
Changes from all commits
84471c5
67841f3
8fa9b12
b8221dd
3bb4638
ea3bf39
bc4c37c
ab5fe55
5ca792a
540871b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,35 @@ | ||
| # Adding new fixtures | ||
| # Pigment CSS testing | ||
|
|
||
| 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. | ||
| ## Folder structure | ||
|
|
||
| - `tests` is the root folder for all tests | ||
| - `fixtures` contains all the fixtures for the tests | ||
| - `*.input.js` are the input files created manually | ||
| - `*.output.*` are the expected output files created by running the tests | ||
| - `*.test.js` are the test files that run the fixtures | ||
|
|
||
| ## Running tests | ||
|
|
||
| At the root project terminal: | ||
|
|
||
| ```bash | ||
| pnpm nx run @pigment-css/react:test | ||
| ``` | ||
|
|
||
| To update the output fixtures: | ||
|
|
||
| ```bash | ||
| pnpm nx run @pigment-css/react:test:update | ||
| ``` | ||
|
|
||
| ## Adding new tests | ||
|
|
||
| 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. | ||
|
|
||
| ## Adding new fixtures | ||
|
|
||
| Create a new file name with `[name].input.js` and add `styled`, `css` or other Pigment CSS calls into the file. | ||
|
|
||
| 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. | ||
|
|
||
| For any implementation changes after that, if you want to update the output files, run the tests with the `test:update` script. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import path from 'node:path'; | ||
| import { runTransformation, expect } from '../testUtils'; | ||
|
|
||
| const theme = { | ||
| palette: { | ||
| primary: { | ||
| main: 'red', | ||
| }, | ||
| }, | ||
| size: { | ||
| font: { | ||
| h1: '3rem', | ||
| }, | ||
| }, | ||
| components: { | ||
| MuiSlider: { | ||
| styleOverrides: { | ||
| rail: { | ||
| fontSize: '3rem', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| describe('Pigment CSS - css', () => { | ||
| it('basics', async () => { | ||
| const { output, fixture } = await runTransformation( | ||
| path.join(__dirname, 'fixtures/css.input.js'), | ||
| { | ||
| themeArgs: { | ||
| theme, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| expect(output.js).to.equal(fixture.js); | ||
| expect(output.css).to.equal(fixture.css); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { css } from '@pigment-css/react'; | ||
|
|
||
| const cls1 = css` | ||
| color: ${({ theme }) => theme.palette.primary.main}; | ||
| font-size: ${({ theme }) => theme.size.font.h1}; | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .c1wr0t7p { | ||
| color: red; | ||
| font-size: 3rem; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| const cls1 = 'c1wr0t7p'; |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { keyframes } from '@pigment-css/react'; | ||
|
|
||
| const rotateKeyframe = keyframes({ | ||
| from: { | ||
| transform: 'rotate(360deg)', | ||
| }, | ||
| to: { | ||
| transform: 'rotate(0deg)', | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| @keyframes r14c1bqo { | ||
| from { | ||
| transform: rotate(360deg); | ||
| } | ||
| to { | ||
| transform: rotate(0deg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| const rotateKeyframe = 'r14c1bqo'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import path from 'node:path'; | ||
| import { runTransformation, expect } from '../testUtils'; | ||
|
|
||
| describe('Pigment CSS - keyframes', () => { | ||
| it('basics', async () => { | ||
| const { output, fixture } = await runTransformation( | ||
| path.join(__dirname, 'fixtures/keyframes.input.js'), | ||
| ); | ||
|
|
||
| expect(output.js).to.equal(fixture.js); | ||
| expect(output.css).to.equal(fixture.css); | ||
| }); | ||
| }); |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| @keyframes r1419f2q { | ||
| from { | ||
| transform: rotate(360deg); | ||
| } | ||
| to { | ||
| transform: rotate(0deg); | ||
| } | ||
| } | ||
| .c1vtarpi { | ||
| color: red; | ||
| animation: r1419f2q 2s ease-out 0s infinite; | ||
| } | ||
| .s1sjy0ja { | ||
| display: none; | ||
| position: absolute; | ||
| border-radius: inherit; | ||
| background-color: currentColor; | ||
| opacity: 0.38; | ||
| font-size: 3rem; | ||
| } | ||
| .s1sjy0ja-1 { | ||
| font-size: 3rem; | ||
| } | ||
| .s6hrafw { | ||
| display: block; | ||
| opacity: 0.38; | ||
| font-size: 3rem; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { styled as _styled3 } from '@pigment-css/react'; | ||
| import { styled as _styled2 } from '@pigment-css/react'; | ||
| import { styled as _styled } from '@pigment-css/react'; | ||
| import _theme from '@pigment-css/react/theme'; | ||
| const Component = /*#__PURE__*/ _styled('div')({ | ||
| classes: ['c1vtarpi'], | ||
| }); | ||
| const SliderRail = /*#__PURE__*/ _styled2('span', { | ||
| name: 'MuiSlider', | ||
| slot: 'Rail', | ||
| })({ | ||
| classes: ['s1sjy0ja', 's1sjy0ja-1'], | ||
| }); | ||
| const SliderRail2 = /*#__PURE__*/ _styled3('span')({ | ||
| classes: ['s6hrafw'], | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import path from 'node:path'; | ||
| import { runTransformation, expect } from '../testUtils'; | ||
|
|
||
| const theme = { | ||
| palette: { | ||
| primary: { | ||
| main: 'red', | ||
| }, | ||
| }, | ||
| size: { | ||
| font: { | ||
| h1: '3rem', | ||
| }, | ||
| }, | ||
| components: { | ||
| MuiSlider: { | ||
| styleOverrides: { | ||
| rail: { | ||
| fontSize: '3rem', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| describe('Pigment CSS - styled', () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the test files are repetitive in code. Only thing that is changing is the file path.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment above that'll handle duplication while still keeping the tests independent.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd argue this. The previous implementation does not let me use the test infra APIs like This made me think of The wrong abstraction. The agree that there are duplications but the tests should also be flexible to add/remove/alter as well. cc @mnajdova for 3rd opinion. |
||
| it('basics', async () => { | ||
| const { output, fixture } = await runTransformation( | ||
| path.join(__dirname, 'fixtures/styled.input.js'), | ||
| { | ||
| themeArgs: { | ||
| theme, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| expect(output.js).to.equal(fixture.js); | ||
| expect(output.css).to.equal(fixture.css); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.