Skip to content

Commit d048768

Browse files
CarlaTeognapse
authored andcommitted
fix: toHaveStyle not to return true when shouldn't (#81)
1 parent 83d419f commit d048768

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@
273273
"name": "Jean Chung",
274274
"avatar_url": "https://avatars0.githubusercontent.com/u/10778036?v=4",
275275
"profile": "https://github.com/jeanchung",
276+
"contributions": [
277+
"code",
278+
"test"
279+
],
280+
{
281+
"login": "CarlaTeo",
282+
"name": "CarlaTeo",
283+
"avatar_url": "https://avatars3.githubusercontent.com/u/9220147?v=4",
284+
"profile": "https://github.com/CarlaTeo",
276285
"contributions": [
277286
"code",
278287
"test"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
2222
[![MIT License][license-badge]][license]
2323

24-
[![All Contributors](https://img.shields.io/badge/all_contributors-27-orange.svg?style=flat-square)](#contributors-)
24+
[![All Contributors](https://img.shields.io/badge/all_contributors-28-orange.svg?style=flat-square)](#contributors-)
2525
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
2626

2727
[![Watch on GitHub][github-watch-badge]][github-watch]
@@ -1075,6 +1075,7 @@ Thanks goes to these people ([emoji key][emojis]):
10751075
</tr>
10761076
<tr>
10771077
<td align="center"><a href="http://jagascript.com"><img src="https://avatars0.githubusercontent.com/u/4562878?v=4" width="100px;" alt="Jaga Santagostino"/><br /><sub><b>Jaga Santagostino</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/issues?q=author%3Akandros" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/jest-dom/commits?author=kandros" title="Tests">⚠️</a> <a href="https://github.com/testing-library/jest-dom/commits?author=kandros" title="Documentation">📖</a></td>
1078+
<td align="center"><a href="https://github.com/CarlaTeo"><img src="https://avatars2.githubusercontent.com/u/9220147?s=460&v=4" width="100px;" alt="Carla Teodoro"/><br /><sub><b>Carla Teodoro</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=CarlaTeo" title="Code">💻</a> <a href="https://github.com/testing-library/jest-dom/commits?author=CarlaTeo" title="Tests">⚠️</a></td>
10781079
</tr>
10791080
</table>
10801081

src/__tests__/to-have-style.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('.toHaveStyle', () => {
1515
background-color: black;
1616
color: white;
1717
float: left;
18+
transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275);
1819
}
1920
`
2021
document.body.appendChild(style)
@@ -30,6 +31,11 @@ describe('.toHaveStyle', () => {
3031
background-color: blue;
3132
color: white;
3233
`)
34+
35+
expect(container.querySelector('.label')).toHaveStyle(
36+
'transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275)',
37+
)
38+
3339
expect(container.querySelector('.label')).toHaveStyle(
3440
'background-color:blue;color:white',
3541
)
@@ -53,6 +59,7 @@ describe('.toHaveStyle', () => {
5359
background-color: black;
5460
color: white;
5561
float: left;
62+
transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275);
5663
}
5764
`
5865
document.body.appendChild(style)
@@ -63,16 +70,24 @@ describe('.toHaveStyle', () => {
6370
'font-weight: bold',
6471
),
6572
).toThrowError()
73+
6674
expect(() =>
6775
expect(container.querySelector('.label')).not.toHaveStyle('color: white'),
6876
).toThrowError()
6977

78+
expect(() =>
79+
expect(container.querySelector('.label')).toHaveStyle(
80+
'transition: all 0.7s ease, width 1.0s cubic-bezier(3, 4, 5, 6);',
81+
),
82+
).toThrowError()
83+
7084
// Make sure the test fails if the css syntax is not valid
7185
expect(() =>
7286
expect(container.querySelector('.label')).not.toHaveStyle(
7387
'font-weight bold',
7488
),
7589
).toThrowError()
90+
7691
expect(() =>
7792
expect(container.querySelector('.label')).toHaveStyle('color white'),
7893
).toThrowError()
@@ -96,4 +111,27 @@ describe('.toHaveStyle', () => {
96111
`)
97112
expect(queryByTestId('color-example')).toHaveStyle('border: 1px solid #fff')
98113
})
114+
115+
test('handles different color declaration formats', () => {
116+
const {queryByTestId} = render(`
117+
<span data-testid="color-example" style="color: rgba(0, 0, 0, 1); background-color: #000000">Hello World</span>
118+
`)
119+
120+
expect(queryByTestId('color-example')).toHaveStyle('color: #000000')
121+
expect(queryByTestId('color-example')).toHaveStyle(
122+
'background-color: rgba(0, 0, 0, 1)',
123+
)
124+
})
125+
126+
test('handles nonexistent styles', () => {
127+
const {container} = render(`
128+
<div class="label" style="background-color: blue; height: 100%">
129+
Hello World
130+
</div>
131+
`)
132+
133+
expect(container.querySelector('.label')).not.toHaveStyle(
134+
'whatever: anything',
135+
)
136+
})
99137
})

src/to-have-style.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import {matcherHint} from 'jest-matcher-utils'
22
import jestDiff from 'jest-diff'
33
import chalk from 'chalk'
4-
import {checkHtmlElement, checkValidCSS} from './utils'
4+
import {checkHtmlElement, parseCSS} from './utils'
55

66
function getStyleDeclaration(document, css) {
7+
const styles = {}
8+
9+
// The next block is necessary to normalize colors
710
const copy = document.createElement('div')
8-
copy.style = css
9-
const styles = copy.style
11+
Object.keys(css).forEach(property => {
12+
copy.style[property] = css[property]
13+
styles[property] = copy.style[property]
14+
})
1015

11-
return Array.from(styles).reduce(
12-
(acc, name) => ({...acc, [name]: styles[name]}),
13-
{},
14-
)
16+
return styles
1517
}
1618

1719
function isSubset(styles, computedStyle) {
@@ -47,10 +49,10 @@ function expectedDiff(expected, computedStyles) {
4749

4850
export function toHaveStyle(htmlElement, css) {
4951
checkHtmlElement(htmlElement, toHaveStyle, this)
50-
checkValidCSS(css, toHaveStyle, this)
52+
const parsedCSS = parseCSS(css, toHaveStyle, this)
5153
const {getComputedStyle} = htmlElement.ownerDocument.defaultView
5254

53-
const expected = getStyleDeclaration(htmlElement.ownerDocument, css)
55+
const expected = getStyleDeclaration(htmlElement.ownerDocument, parsedCSS)
5456
const received = getComputedStyle(htmlElement)
5557

5658
return {

src/utils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class InvalidCSSError extends Error {
7979
}
8080
}
8181

82-
function checkValidCSS(css, ...args) {
82+
function parseCSS(css, ...args) {
8383
const ast = parse(`selector { ${css} }`, {silent: true}).stylesheet
8484

8585
if (ast.parsingErrors && ast.parsingErrors.length > 0) {
@@ -93,6 +93,14 @@ function checkValidCSS(css, ...args) {
9393
...args,
9494
)
9595
}
96+
97+
const parsedRules = ast.rules[0].declarations
98+
.filter(d => d.type === 'declaration')
99+
.reduce(
100+
(obj, {property, value}) => Object.assign(obj, {[property]: value}),
101+
{},
102+
)
103+
return parsedRules
96104
}
97105

98106
function display(value) {
@@ -187,7 +195,7 @@ function compareArraysAsSet(a, b) {
187195
export {
188196
HtmlElementTypeError,
189197
checkHtmlElement,
190-
checkValidCSS,
198+
parseCSS,
191199
deprecate,
192200
getMessage,
193201
matches,

0 commit comments

Comments
 (0)