Skip to content

Commit

Permalink
fix: Changed toHaveStyle to use getPropertyValue instead of accessing…
Browse files Browse the repository at this point in the history
… 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 <gnapse@gmail.com>
  • Loading branch information
luanraithz and gnapse committed Aug 11, 2020
1 parent 9b0510b commit 92176e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/__tests__/to-have-style.js
Expand Up @@ -103,6 +103,9 @@ describe('.toHaveStyle', () => {
expect(container.querySelector('.label')).toHaveStyle('color white'),
).toThrowError()

expect(() =>
expect(container.querySelector('.label')).toHaveStyle('--color: black'),
).toThrowError()
document.body.removeChild(style)
document.body.removeChild(container)
})
Expand All @@ -116,6 +119,33 @@ describe('.toHaveStyle', () => {
)
})

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

test('handles global custom properties', () => {
const style = document.createElement('style')
style.innerHTML = `
div {
--color: blue;
}
`

const {container} = render(`
<div>
Hello world
</div>
`)

document.body.appendChild(style)
document.body.appendChild(container)

expect(container).toHaveStyle(`--color: blue`)
})

test('properly normalizes colors for border', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="border: 1px solid #fff">Hello World</span>
Expand Down Expand Up @@ -170,6 +200,24 @@ describe('.toHaveStyle', () => {
})
})

test('Uses px as the default unit', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="font-size: 12px">Hello World</span>
`)
expect(queryByTestId('color-example')).toHaveStyle({
fontSize: 12
})
})

test('Fails with an invalid unit', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="font-size: 12rem">Hello World</span>
`)
expect(() => {
expect(queryByTestId('color-example')).toHaveStyle({ fontSize: '12px' })
}).toThrowError()
})

test('supports dash-cased property names', () => {
const {container} = render(`
<div class="label" style="background-color: blue; height: 100%">
Expand Down
2 changes: 1 addition & 1 deletion src/to-have-style.js
Expand Up @@ -22,7 +22,7 @@ function isSubset(styles, computedStyle) {
Object.entries(styles).every(
([prop, value]) =>
computedStyle[prop] === value ||
computedStyle[prop.toLowerCase()] === value,
computedStyle.getPropertyValue(prop.toLowerCase()) === value,
)
)
}
Expand Down

0 comments on commit 92176e1

Please sign in to comment.