Wrong style rule used
See original GitHub issueI’m trying to test some styles on my component and despite all tries I can’t get the test to pass.
After invetigating a little bit, I found that the way you are getting the component rules is what makes the test to fail.
Current behavior: Check this codesandbox: https://codesandbox.io/s/bold-glitter-h8rkb
When testing a styled component, the jest-emotion matcher validates against the wrong style rule.
Expected behavior: The jest-emotion matcher validates against the correct style rule.
More information:
At the Checkbox test, I’m trying to validate that the hover state has the correct border, but as is, the matcher will validate the component style against the &:checked &:hover
style rule, because it is the last style rule that has the :hover
.
I debugged the matcher code locally and found the following:
Selectors for the component (and for the :hover
state)
[
'.css-ih6inj-StyledCheckbox:hover'
'.css-ih6inj-StyledCheckbox:checked:hover',
]
Declarations
[
{
type: 'declaration', // This one is for the first selector `:hover`
property: 'border-color',
value: '#7E7E7E',
position: Position { start: [Object], end: [Object], source: undefined }
},
{
type: 'declaration', // This is for the second selector `:hover:checked`
property: 'border-color',
value: '#9E9E9E',
position: Position { start: [Object], end: [Object], source: undefined }
}
]
The declaration that was used to validate the component style
{
type: 'declaration',
property: 'border-color',
value: '#9E9E9E',
position: Position { start: [Object], end: [Object], source: undefined }
}
Which is the last declaration for the :hover
state despite it is only applied when the component is :checked
(and the component being unchecked on the test)
If I change the order of the style declaration on the component from:
&:hover ...
&:checked &:hover ...
to
&:checked &:hover ...
&:hover ...
the test passes but then if I test the style for the :hover
when the input is :checked
it will fail because the matcher will compare against the last declared style for hover.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top GitHub Comments
I understand that a totally fine tuned matcher for every cases would be very complicated, but in this case we have a very limited matcher.
It would be nice to have at least a matcher that would recognize a rule with more than a pseudo-element and/or combinators targeting a specific element forgetting all the inheritance.
Anyway I think that this should be properly documented.
This solution does work but the typing that
toHaveStyleRule
for target only receives RegExp. To allow that I’ve created a PR #2457