Open jkettmann opened 11 months ago
Removing the whitespace from the ast rule as well should be a simple and backwards compatible change:
const getAtRules = (ast, options) => {
return Object.keys(options)
.map((option) =>
ast.stylesheet.rules
.filter((rule) => rule.type === option && rule[option].replace(/:\s/g, ":") === options[option].replace(/:\s/g, ":"))
.map((rule) => rule.rules)
.reduce((acc, rules) => acc.concat(rules), [])
)
.reduce((acc, rules) => acc.concat(rules), []);
};
If someone else encounters the same problem before this issue is closed a simple workaround is to add a second whitespace as in this example:
describe('toHaveStyleRule media test', () => {
test('has red color on larger devices', async () => {
render(<MyComponent />);
const styledDiv = screen.getByTestId('styled-div');
expect(styledDiv).toHaveStyleRule('color', 'red', {
media: '(min-width: 400px)',
});
});
});
Note the two whitespaces in min-width: 400px
If someone else encounters the same problem before this issue is closed a simple workaround is to add a second whitespace as in this example:
describe('toHaveStyleRule media test', () => { test('has red color on larger devices', async () => { render(<MyComponent />); const styledDiv = screen.getByTestId('styled-div'); expect(styledDiv).toHaveStyleRule('color', 'red', { media: '(min-width: 400px)', }); }); });
Note the two whitespaces in
min-width: 400px
You saved my life, I'm about 2 days trying to fix that issue. Thank you so much.
@hemerson-git I'm glad this was helpful 🙂
Is there a plan to fix this in the package? In my case, media query strings are stored in a theme with a single whitespace after the colon, so this is a pain to manipulate the string every time I run a test like this.
Same problem here.
If someone else encounters the same problem before this issue is closed a simple workaround is to add a second whitespace as in this example:
describe('toHaveStyleRule media test', () => { test('has red color on larger devices', async () => { render(<MyComponent />); const styledDiv = screen.getByTestId('styled-div'); expect(styledDiv).toHaveStyleRule('color', 'red', { media: '(min-width: 400px)', }); }); });
Note the two whitespaces in
min-width: 400px
This helped so much broh! Thank you!
We recently updated styled-components to v6 and something caused some of our tests to fail. I was able to reproduce the issue in this repository (you can also run it here on Replit). Here's the example test:
This is the error I'm getting:
The problem seems to be that this check in toHaveStyleRule
rule[option] === options[option].replace(/:\s/g, ":")
is not working anymore.Before
rule.option
didn't have a whitespace. I'm not sure what changed but now it looks like this:I'm happy to create a PR but as I said, I'm not sure what the original cause of the whitespace not being removed is.
Edit: To run the tests in the Replit simply run
npm run test
.