Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/to-have-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ describe('.toHaveStyle', () => {
})
})

test('correctly matches invalid properties', () => {
const {queryByTestId} = render(`
<div data-testid="element" style="fontSize: 8" />
`)
expect(queryByTestId('element')).not.toHaveStyle({
fontSize: 1,
})
})

test('Fails with an invalid unit', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="font-size: 12rem">Hello World</span>
Expand Down
22 changes: 18 additions & 4 deletions src/to-have-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ function getStyleDeclaration(document, css) {
return styles
}

function isInvalidStyleDeclaration(name, value, computedStyle) {
return (
name &&
!value &&
!computedStyle[name] &&
!computedStyle.getPropertyValue(name)
)
}
EduardoSimon marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +17 to +24
Copy link
Member

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.

Copy link
Author

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.


function isSubset(styles, computedStyle) {
return (
!!Object.keys(styles).length &&
Expand All @@ -22,11 +31,16 @@ function isSubset(styles, computedStyle) {
const spellingVariants = [prop]
if (!isCustomProperty) spellingVariants.push(prop.toLowerCase())

return spellingVariants.some(
name =>
return spellingVariants.some(name => {
if (isInvalidStyleDeclaration(name, value, computedStyle)) {
return false
}

return (
computedStyle[name] === value ||
computedStyle.getPropertyValue(name) === value,
)
computedStyle.getPropertyValue(name) === value
)
})
})
)
}
Expand Down