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: support uppercase custom props in toHaveStyle #552

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/__tests__/to-have-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('.toHaveStyle', () => {
background-color: black;
color: white;
float: left;
--var-name: 0px;
transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275);
}
`
Expand All @@ -92,6 +93,11 @@ describe('.toHaveStyle', () => {
),
).toThrowError()

// Custom property names are case sensitive
expect(() =>
expect(container.querySelector('.label')).toHaveStyle('--VAR-NAME: 0px;'),
).toThrowError()

// Make sure the test fails if the css syntax is not valid
expect(() =>
expect(container.querySelector('.label')).not.toHaveStyle(
Expand Down Expand Up @@ -119,11 +125,11 @@ describe('.toHaveStyle', () => {
)
})

test('handles inline custom properties', () => {
test('handles inline custom properties (with uppercase letters)', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="--color: blue">Hello World</span>
<span data-testid="color-example" style="--accentColor: blue">Hello World</span>
`)
expect(queryByTestId('color-example')).toHaveStyle('--color: blue')
expect(queryByTestId('color-example')).toHaveStyle('--accentColor: blue')
})

test('handles global custom properties', () => {
Expand Down Expand Up @@ -205,7 +211,7 @@ describe('.toHaveStyle', () => {
<span data-testid="color-example" style="font-size: 12px">Hello World</span>
`)
expect(queryByTestId('color-example')).toHaveStyle({
fontSize: 12
fontSize: 12,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

})
})

Expand All @@ -214,7 +220,7 @@ describe('.toHaveStyle', () => {
<span data-testid="color-example" style="font-size: 12rem">Hello World</span>
`)
expect(() => {
expect(queryByTestId('color-example')).toHaveStyle({ fontSize: '12px' })
expect(queryByTestId('color-example')).toHaveStyle({fontSize: '12px'})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one came from the husky pre-commit hook formatting the whole file. By the way running npm run format would have changed a bunch of other files, too.

}).toThrowError()
})

Expand Down
16 changes: 11 additions & 5 deletions src/to-have-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ function getStyleDeclaration(document, css) {
function isSubset(styles, computedStyle) {
return (
!!Object.keys(styles).length &&
Object.entries(styles).every(
([prop, value]) =>
computedStyle[prop] === value ||
computedStyle.getPropertyValue(prop.toLowerCase()) === value,
)
Object.entries(styles).every(([prop, value]) => {
const isCustomProperty = prop.startsWith('--')
const spellingVariants = [prop]
if (!isCustomProperty) spellingVariants.push(prop.toLowerCase())

return spellingVariants.some(
name =>
computedStyle[name] === value ||
computedStyle.getPropertyValue(name) === value,
)
})
)
}

Expand Down