Skip to content

Commit 5bea350

Browse files
authored
fix(toHaveStyle): strictly match empty values (#276)
closes #272
1 parent 33c35c6 commit 5bea350

File tree

4 files changed

+52
-68
lines changed

4 files changed

+52
-68
lines changed

src/__tests__/to-have-style.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,55 @@ describe('.toHaveStyle', () => {
146146
)
147147
})
148148

149-
test('handles styles as object', () => {
150-
const {container} = render(`
151-
<div class="label" style="background-color: blue; height: 100%">
152-
Hello World
153-
</div>
154-
`)
149+
describe('object syntax', () => {
150+
test('handles styles as object', () => {
151+
const {container} = render(`
152+
<div class="label" style="background-color: blue; height: 100%">
153+
Hello World
154+
</div>
155+
`)
155156

156-
expect(container.querySelector('.label')).toHaveStyle({
157-
backgroundColor: 'blue',
158-
})
159-
expect(container.querySelector('.label')).toHaveStyle({
160-
backgroundColor: 'blue',
161-
height: '100%',
157+
expect(container.querySelector('.label')).toHaveStyle({
158+
backgroundColor: 'blue',
159+
})
160+
expect(container.querySelector('.label')).toHaveStyle({
161+
backgroundColor: 'blue',
162+
height: '100%',
163+
})
164+
expect(container.querySelector('.label')).not.toHaveStyle({
165+
backgroundColor: 'red',
166+
height: '100%',
167+
})
168+
expect(container.querySelector('.label')).not.toHaveStyle({
169+
whatever: 'anything',
170+
})
162171
})
163-
expect(container.querySelector('.label')).not.toHaveStyle({
164-
backgroundColor: 'red',
165-
height: '100%',
172+
173+
test('supports dash-cased property names', () => {
174+
const {container} = render(`
175+
<div class="label" style="background-color: blue; height: 100%">
176+
Hello World
177+
</div>
178+
`)
179+
expect(container.querySelector('.label')).toHaveStyle({
180+
'background-color': 'blue',
181+
})
166182
})
167-
expect(container.querySelector('.label')).not.toHaveStyle({
168-
whatever: 'anything',
183+
184+
test('requires strict empty properties matching', () => {
185+
const {container} = render(`
186+
<div class="label" style="width: 100%;height: 100%">
187+
Hello World
188+
</div>
189+
`)
190+
expect(container.querySelector('.label')).not.toHaveStyle({
191+
width: '100%',
192+
height: '',
193+
})
194+
expect(container.querySelector('.label')).not.toHaveStyle({
195+
width: '',
196+
height: '',
197+
})
169198
})
170199
})
171200
})

src/__tests__/utils.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
deprecate,
33
checkHtmlElement,
44
HtmlElementTypeError,
5-
parseJStoCSS,
65
toSentence,
76
} from '../utils'
87
import document from './helpers/document'
@@ -84,40 +83,6 @@ describe('checkHtmlElement', () => {
8483
})
8584
})
8685

87-
describe('parseJStoCSS', () => {
88-
describe('when all the styles are valid', () => {
89-
it('returns the JS parsed as CSS text', () => {
90-
expect(
91-
parseJStoCSS(document, {
92-
backgroundColor: 'blue',
93-
height: '100%',
94-
}),
95-
).toBe('background-color: blue; height: 100%;')
96-
})
97-
})
98-
99-
describe('when some style is invalid', () => {
100-
it('returns the JS parsed as CSS text without the invalid style', () => {
101-
expect(
102-
parseJStoCSS(document, {
103-
backgroundColor: 'blue',
104-
whatever: 'anything',
105-
}),
106-
).toBe('background-color: blue;')
107-
})
108-
})
109-
110-
describe('when all the styles are invalid', () => {
111-
it('returns an empty string', () => {
112-
expect(
113-
parseJStoCSS(document, {
114-
whatever: 'anything',
115-
}),
116-
).toBe('')
117-
})
118-
})
119-
})
120-
12186
describe('toSentence', () => {
12287
it('turns array into string of comma separated list with default last word connector', () => {
12388
expect(toSentence(['one', 'two', 'three'])).toBe('one, two and three')

src/to-have-style.js

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

66
function getStyleDeclaration(document, css) {
77
const styles = {}
@@ -21,7 +21,8 @@ function isSubset(styles, computedStyle) {
2121
!!Object.keys(styles).length &&
2222
Object.entries(styles).every(
2323
([prop, value]) =>
24-
computedStyle.getPropertyValue(prop.toLowerCase()) === value,
24+
computedStyle[prop] === value ||
25+
computedStyle[prop.toLowerCase()] === value,
2526
)
2627
)
2728
}
@@ -37,7 +38,7 @@ function printoutStyles(styles) {
3738
// received computed styles
3839
function expectedDiff(expected, computedStyles) {
3940
const received = Array.from(computedStyles)
40-
.filter(prop => expected[prop])
41+
.filter(prop => expected[prop] !== undefined)
4142
.reduce(
4243
(obj, prop) =>
4344
Object.assign(obj, {[prop]: computedStyles.getPropertyValue(prop)}),
@@ -51,14 +52,10 @@ function expectedDiff(expected, computedStyles) {
5152
return diffOutput.replace(`${chalk.red('+ Received')}\n`, '')
5253
}
5354

54-
function getCSStoParse(document, css) {
55-
return typeof css === 'object' ? parseJStoCSS(document, css) : css
56-
}
57-
5855
export function toHaveStyle(htmlElement, css) {
5956
checkHtmlElement(htmlElement, toHaveStyle, this)
60-
const cssToParse = getCSStoParse(htmlElement.ownerDocument, css)
61-
const parsedCSS = parseCSS(cssToParse, toHaveStyle, this)
57+
const parsedCSS =
58+
typeof css === 'object' ? css : parseCSS(css, toHaveStyle, this)
6259
const {getComputedStyle} = htmlElement.ownerDocument.defaultView
6360

6461
const expected = getStyleDeclaration(htmlElement.ownerDocument, parsedCSS)

src/utils.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,6 @@ function compareArraysAsSet(a, b) {
192192
return undefined
193193
}
194194

195-
function parseJStoCSS(document, css) {
196-
const sandboxElement = document.createElement('div')
197-
Object.assign(sandboxElement.style, css)
198-
return sandboxElement.style.cssText
199-
}
200-
201195
function toSentence(
202196
array,
203197
{wordConnector = ', ', lastWordConnector = ' and '} = {},
@@ -218,6 +212,5 @@ export {
218212
getTag,
219213
getSingleElementValue,
220214
compareArraysAsSet,
221-
parseJStoCSS,
222215
toSentence,
223216
}

0 commit comments

Comments
 (0)