-
Notifications
You must be signed in to change notification settings - Fork 404
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
fix: toHaveStyle assertion with invalid style (#564) #582
base: main
Are you sure you want to change the base?
fix: toHaveStyle assertion with invalid style (#564) #582
Conversation
Given an invalid declaration such as `fontSize: 8`, due to the missing unit, the `toHaveStyle` matcher should not pass the following test: ``` render(<div data-testid="element" style={{ fontSize: 8 }} />) expect(screen.getByTestId('element')).toHaveStyle({ fontSize: 1 }) ``` This PR fixes testing-library#564 by adding a more restrictive guard in the matcher's logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
Although it may benefit from a comment on the new helper function as mentioned/requested below.
function isInvalidStyleDeclaration(name, value, computedStyle) { | ||
return ( | ||
name && | ||
!value && | ||
!computedStyle[name] && | ||
!computedStyle.getPropertyValue(name) | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not fully evident (to me at least) why this works. I'm not saying I doubt that it works, I'm saying that perhaps this function could use a jsdoc to explain what it does. Or at the very least can you share here the logic? Perhaps no comment is needed, and I'm just having trouble to understand it myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Actually getting it to work was a bit tricky, so a comment would not harm.
Co-authored-by: Ernesto García <[email protected]>
Hey @gnapse , would it be possible to add the Hacktoberfest label to the PR? |
What:
Given an invalid declaration such as
fontSize: 8
, due to the missing unit, thetoHaveStyle
matcher should not pass the following test:Why:
Refer to the original issue for more info: #564
How:
This PR fixes #564 by adding a more restrictive guard in the matcher's logic.
Checklist: