You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the altStyle attribute is true, it will apply some styles to both nested th and td elements, some styles only apply to nested th and others only apply to nested td.
When testing this to ensure all the style rules are set, it is very repetitive to check for multiple rules on the same component. When using only jest-styled-components, we'd be looking at this:
describe('Table Component',()=>{it('Adds the expected rules when property is true',()=>{constprops={property: true};consttree=create(<Table{ ...props}/>).toJSON();expect(tree).toHaveStyleRule('font-family',expect.stringContaining('apple-system'),{modifier: 'th'});expect(tree).toHaveStyleRule('font-family',expect.stringContaining('apple-system'),{modifier: 'td'});expect(tree).toHaveStyleRule('line-height','normal',{modifier: 'th'});expect(tree).toHaveStyleRule('line-height','normal',{modifier: 'td'});expect(tree).toHaveStyleRule('height','12px',{modifier: 'th'});expect(tree).toHaveStyleRule('height','12px',{modifier: 'td'});expect(tree).toHaveStyleRule('font-size','14px',{modifier: 'th'});expect(tree).toHaveStyleRule('font-size','13px',{modifier: 'td'});expect(tree).toHaveStyleRule('font-weight','500',{modifier: 'th'});expect(tree).toHaveStyleRule('font-weight','400',{modifier: 'td'});});});
Now, if we add in jest-chain it gets slightly less repetitive, but still a lot of duplication. Like so:
describe('Table Component',()=>{it('Adds the expected rules when property is true',()=>{constprops={property: true};consttree=create(<Table{ ...props}/>).toJSON();expect(tree).toHaveStyleRule('font-family',expect.stringContaining('apple-system'),{modifier: 'th'}).toHaveStyleRule('font-family',expect.stringContaining('apple-system'),{modifier: 'td'}).toHaveStyleRule('line-height','normal',{modifier: 'th'}).toHaveStyleRule('line-height','normal',{modifier: 'td'}).toHaveStyleRule('height','12px',{modifier: 'th'}).toHaveStyleRule('height','12px',{modifier: 'td'}).toHaveStyleRule('font-size','14px',{modifier: 'th'}).toHaveStyleRule('font-size','13px',{modifier: 'td'}).toHaveStyleRule('font-weight','500',{modifier: 'th'}).toHaveStyleRule('font-weight','400',{modifier: 'td'});});});
What I'm proposing
It would be great if we could add a matcher, very similar to the current toHaveStyleRule, but named something along the lines of toHaveStyleRules. Here is the proposed TypeScript API (alongside the current one):
As you can see in the above code, it is almost the same as toHaveStyleRule, but matching multiple rules on the one element/modifier/etc.
It would look something like this:
describe('Table Component',()=>{it('Adds the expected rules when property is true',()=>{constprops={property: true};consttree=create(<Table{ ...props}/>).toJSON();constcommonRules=[['font-family',expect.stringContaining('apple-system')],['line-height','normal'],['height','12px']];expect(tree).toHaveStyleRules([
...commonRules,['font-size','14px'],['font-weight','500']],{modifier: 'th'});expect(tree).toHaveStyleRules([
...commonRules,['font-size','13px'],['font-weight','400']],{modifier: 'td'});});});
This would allow us to allow code re-use more easily and reduce repetition.
I'm happy to create a PR for this change as it should be fairly simple and could almost certainly use the existing toHaveStyleRule function.
The text was updated successfully, but these errors were encountered:
Hi!
I'd love it if we can add another matcher to jest-styled-components to support checking multiple style rules on the one element/modifier settings.
The Problem
Lets say I have the following component created with Styled Components:
When the
altStyle
attribute is true, it will apply some styles to both nestedth
andtd
elements, some styles only apply to nestedth
and others only apply to nestedtd
.When testing this to ensure all the style rules are set, it is very repetitive to check for multiple rules on the same component. When using only
jest-styled-components
, we'd be looking at this:Now, if we add in
jest-chain
it gets slightly less repetitive, but still a lot of duplication. Like so:What I'm proposing
It would be great if we could add a matcher, very similar to the current
toHaveStyleRule
, but named something along the lines oftoHaveStyleRules
. Here is the proposed TypeScript API (alongside the current one):As you can see in the above code, it is almost the same as
toHaveStyleRule
, but matching multiple rules on the one element/modifier/etc.It would look something like this:
This would allow us to allow code re-use more easily and reduce repetition.
I'm happy to create a PR for this change as it should be fairly simple and could almost certainly use the existing
toHaveStyleRule
function.The text was updated successfully, but these errors were encountered: