Skip to content

Commit b7b7c6a

Browse files
authoredNov 30, 2023
fix: support uppercase custom props in toHaveStyle (#552)
1 parent 4ae0231 commit b7b7c6a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed
 

‎src/__tests__/to-have-style.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ describe('.toHaveStyle', () => {
7070
background-color: black;
7171
color: white;
7272
float: left;
73+
--var-name: 0px;
7374
transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275);
7475
}
7576
`
@@ -92,6 +93,11 @@ describe('.toHaveStyle', () => {
9293
),
9394
).toThrowError()
9495

96+
// Custom property names are case sensitive
97+
expect(() =>
98+
expect(container.querySelector('.label')).toHaveStyle('--VAR-NAME: 0px;'),
99+
).toThrowError()
100+
95101
// Make sure the test fails if the css syntax is not valid
96102
expect(() =>
97103
expect(container.querySelector('.label')).not.toHaveStyle(
@@ -119,11 +125,11 @@ describe('.toHaveStyle', () => {
119125
)
120126
})
121127

122-
test('handles inline custom properties', () => {
128+
test('handles inline custom properties (with uppercase letters)', () => {
123129
const {queryByTestId} = render(`
124-
<span data-testid="color-example" style="--color: blue">Hello World</span>
130+
<span data-testid="color-example" style="--accentColor: blue">Hello World</span>
125131
`)
126-
expect(queryByTestId('color-example')).toHaveStyle('--color: blue')
132+
expect(queryByTestId('color-example')).toHaveStyle('--accentColor: blue')
127133
})
128134

129135
test('handles global custom properties', () => {
@@ -205,7 +211,7 @@ describe('.toHaveStyle', () => {
205211
<span data-testid="color-example" style="font-size: 12px">Hello World</span>
206212
`)
207213
expect(queryByTestId('color-example')).toHaveStyle({
208-
fontSize: 12
214+
fontSize: 12,
209215
})
210216
})
211217

@@ -214,7 +220,7 @@ describe('.toHaveStyle', () => {
214220
<span data-testid="color-example" style="font-size: 12rem">Hello World</span>
215221
`)
216222
expect(() => {
217-
expect(queryByTestId('color-example')).toHaveStyle({ fontSize: '12px' })
223+
expect(queryByTestId('color-example')).toHaveStyle({fontSize: '12px'})
218224
}).toThrowError()
219225
})
220226

‎src/to-have-style.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ function getStyleDeclaration(document, css) {
1717
function isSubset(styles, computedStyle) {
1818
return (
1919
!!Object.keys(styles).length &&
20-
Object.entries(styles).every(
21-
([prop, value]) =>
22-
computedStyle[prop] === value ||
23-
computedStyle.getPropertyValue(prop.toLowerCase()) === value,
24-
)
20+
Object.entries(styles).every(([prop, value]) => {
21+
const isCustomProperty = prop.startsWith('--')
22+
const spellingVariants = [prop]
23+
if (!isCustomProperty) spellingVariants.push(prop.toLowerCase())
24+
25+
return spellingVariants.some(
26+
name =>
27+
computedStyle[name] === value ||
28+
computedStyle.getPropertyValue(name) === value,
29+
)
30+
})
2531
)
2632
}
2733

0 commit comments

Comments
 (0)
Please sign in to comment.