Skip to content

Commit 92176e1

Browse files
luanraithzgnapse
andauthored
fix: Changed toHaveStyle to use getPropertyValue instead of accessing the property directly (#285)
* Changed toHaveStyle to use getPropertyValue instead of accessing the property directly * Using px instead of rem * changed test description * Update src/__tests__/to-have-style.js * Apply suggestions from code review Co-authored-by: Ernesto García <[email protected]>
1 parent 9b0510b commit 92176e1

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/__tests__/to-have-style.js

+48
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ describe('.toHaveStyle', () => {
103103
expect(container.querySelector('.label')).toHaveStyle('color white'),
104104
).toThrowError()
105105

106+
expect(() =>
107+
expect(container.querySelector('.label')).toHaveStyle('--color: black'),
108+
).toThrowError()
106109
document.body.removeChild(style)
107110
document.body.removeChild(container)
108111
})
@@ -116,6 +119,33 @@ describe('.toHaveStyle', () => {
116119
)
117120
})
118121

122+
test('handles inline custom properties', () => {
123+
const {queryByTestId} = render(`
124+
<span data-testid="color-example" style="--color: blue">Hello World</span>
125+
`)
126+
expect(queryByTestId('color-example')).toHaveStyle('--color: blue')
127+
})
128+
129+
test('handles global custom properties', () => {
130+
const style = document.createElement('style')
131+
style.innerHTML = `
132+
div {
133+
--color: blue;
134+
}
135+
`
136+
137+
const {container} = render(`
138+
<div>
139+
Hello world
140+
</div>
141+
`)
142+
143+
document.body.appendChild(style)
144+
document.body.appendChild(container)
145+
146+
expect(container).toHaveStyle(`--color: blue`)
147+
})
148+
119149
test('properly normalizes colors for border', () => {
120150
const {queryByTestId} = render(`
121151
<span data-testid="color-example" style="border: 1px solid #fff">Hello World</span>
@@ -170,6 +200,24 @@ describe('.toHaveStyle', () => {
170200
})
171201
})
172202

203+
test('Uses px as the default unit', () => {
204+
const {queryByTestId} = render(`
205+
<span data-testid="color-example" style="font-size: 12px">Hello World</span>
206+
`)
207+
expect(queryByTestId('color-example')).toHaveStyle({
208+
fontSize: 12
209+
})
210+
})
211+
212+
test('Fails with an invalid unit', () => {
213+
const {queryByTestId} = render(`
214+
<span data-testid="color-example" style="font-size: 12rem">Hello World</span>
215+
`)
216+
expect(() => {
217+
expect(queryByTestId('color-example')).toHaveStyle({ fontSize: '12px' })
218+
}).toThrowError()
219+
})
220+
173221
test('supports dash-cased property names', () => {
174222
const {container} = render(`
175223
<div class="label" style="background-color: blue; height: 100%">

src/to-have-style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function isSubset(styles, computedStyle) {
2222
Object.entries(styles).every(
2323
([prop, value]) =>
2424
computedStyle[prop] === value ||
25-
computedStyle[prop.toLowerCase()] === value,
25+
computedStyle.getPropertyValue(prop.toLowerCase()) === value,
2626
)
2727
)
2828
}

0 commit comments

Comments
 (0)