-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
matchers.test.js
369 lines (319 loc) Β· 8.99 KB
/
matchers.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import 'test-utils/legacy-env'
import renderer from 'react-test-renderer'
/** @jsx jsx */
import * as enzyme from 'enzyme'
import { css, jsx } from '@emotion/core'
import styled from '@emotion/styled'
import { matchers } from 'jest-emotion'
const { toHaveStyleRule } = matchers
expect.extend(matchers)
describe('toHaveStyleRule', () => {
const divStyle = css`
color: red;
`
const svgStyle = css`
width: 100%;
`
it('matches styles on the top-most node passed in', () => {
const tree = renderer
.create(
<div css={divStyle}>
<svg css={svgStyle} />
</div>
)
.toJSON()
expect(tree).toHaveStyleRule('color', 'red')
expect(tree).not.toHaveStyleRule('width', '100%')
const svgNode = tree.children[0]
expect(svgNode).toHaveStyleRule('width', '100%')
expect(svgNode).not.toHaveStyleRule('color', 'red')
})
it('supports asymmetric matchers', () => {
const tree = renderer
.create(
<div css={divStyle}>
<svg css={svgStyle} />
</div>
)
.toJSON()
expect(tree).toHaveStyleRule('color', expect.anything())
expect(tree).not.toHaveStyleRule('padding', expect.anything())
const svgNode = tree.children[0]
expect(svgNode).toHaveStyleRule('width', expect.stringMatching(/.*%$/))
})
it('supports enzyme `mount` method', () => {
const Component = () => (
<div css={divStyle}>
<svg css={svgStyle} />
</div>
)
const wrapper = enzyme.mount(<Component />)
expect(wrapper).toHaveStyleRule('color', 'red')
expect(wrapper).not.toHaveStyleRule('width', '100%')
const svgNode = wrapper.find('svg')
expect(svgNode).toHaveStyleRule('width', '100%')
expect(svgNode).not.toHaveStyleRule('color', 'red')
})
it('supports enzyme `render` method', () => {
const Component = () => (
<div css={divStyle}>
<svg css={svgStyle} />
</div>
)
const wrapper = enzyme.render(<Component />)
expect(wrapper).toHaveStyleRule('color', 'red')
expect(wrapper).not.toHaveStyleRule('width', '100%')
const svgNode = wrapper.find('svg')
expect(svgNode).toHaveStyleRule('width', '100%')
expect(svgNode).not.toHaveStyleRule('color', 'red')
})
it('supports enzyme `shallow` method', () => {
const Component = () => (
<div css={divStyle}>
<svg css={svgStyle} />
</div>
)
const wrapper = enzyme.shallow(<Component />)
expect(wrapper).toHaveStyleRule('color', 'red')
expect(wrapper).not.toHaveStyleRule('width', '100%')
const svgNode = wrapper.childAt(0)
expect(svgNode).toHaveStyleRule('width', '100%')
expect(svgNode).not.toHaveStyleRule('color', 'red')
})
// i think this isn't working because of forwardRef
it.skip('supports styled components', () => {
const Div = styled('div')`
color: red;
`
const Svg = styled('svg')`
width: 100%;
`
;['mount', 'render', 'shallow'].forEach(method => {
const wrapper = enzyme[method](
<Div>
<Svg />
</Div>
)
expect(wrapper).toHaveStyleRule('color', 'red')
expect(wrapper).not.toHaveStyleRule('width', '100%')
const svgNode =
method === 'render' ? wrapper.find('svg') : wrapper.find(Svg)
expect(svgNode).toHaveStyleRule('width', '100%')
expect(svgNode).not.toHaveStyleRule('color', 'red')
})
})
it('fails if no styles are found', () => {
const tree = renderer.create(<div />).toJSON()
const result = toHaveStyleRule(tree, 'color', 'red')
expect(result.pass).toBe(false)
expect(result.message()).toBe('Property not found: color')
})
it('supports regex values', () => {
const tree = renderer.create(<div css={divStyle} />).toJSON()
expect(tree).toHaveStyleRule('color', /red/)
})
it.skip('returns a message explaining the failure', () => {
const tree = renderer.create(<div css={divStyle} />).toJSON()
// When expect(tree).toHaveStyleRule('color', 'blue') fails
const resultFail = toHaveStyleRule(tree, 'color', 'blue')
expect(resultFail.message()).toMatchSnapshot()
// When expect(tree).not.toHaveStyleRule('color', 'red')
const resultPass = toHaveStyleRule(tree, 'color', 'red')
expect(resultPass.message()).toMatchSnapshot()
})
it('matches styles on the focus, hover targets', () => {
const localDivStyle = css`
color: white;
&:hover {
color: yellow;
}
&:focus {
color: black;
}
`
const tree = renderer
.create(
<div css={localDivStyle}>
<svg css={svgStyle} />
</div>
)
.toJSON()
expect(tree).toHaveStyleRule('color', 'yellow', { target: ':hover' })
expect(tree).toHaveStyleRule('color', 'black', { target: ':focus' })
expect(tree).toHaveStyleRule('color', 'white')
})
it('matches styles on the nested component or html element', () => {
const Svg = styled('svg')`
width: 100%;
fill: blue;
`
const Div = styled('div')`
color: red;
${Svg} {
fill: green;
}
span {
color: yellow;
}
`
const tree = renderer
.create(
<Div>
<Svg />
<span>Test</span>
</Div>
)
.toJSON()
expect(tree).toHaveStyleRule('color', 'yellow', { target: 'span' })
expect(tree).toHaveStyleRule('color', 'red')
expect(tree).toHaveStyleRule('fill', 'green', { target: `${Svg}` })
})
it('matches target styles by regex', () => {
const localDivStyle = css`
a {
color: yellow;
}
a:hover {
color: black;
}
`
const tree = renderer
.create(
<div css={localDivStyle}>
<svg css={svgStyle} />
</div>
)
.toJSON()
expect(tree).toHaveStyleRule('color', 'yellow', { target: /a$/ })
})
it('matches proper style for css', () => {
const tree = renderer
.create(
<div
css={css`
color: green;
color: hotpink;
`}
/>
)
.toJSON()
expect(tree).not.toHaveStyleRule('color', 'green')
expect(tree).toHaveStyleRule('color', 'hotpink')
})
it('matches style of the media', () => {
const Svg = styled('svg')`
width: 100%;
`
const Div = styled('div')`
font-size: 30px;
@media (min-width: 420px) {
font-size: 50px;
}
@media (min-width: 920px) and (max-width: 1200px) {
font-size: 70px;
}
@media screen and (max-width: 1200px) {
font-size: 80px;
}
@media not all and (monochrome) {
font-size: 90px;
}
`
const tree = renderer
.create(
<Div>
<Svg />
</Div>
)
.toJSON()
expect(tree).toHaveStyleRule('font-size', '30px')
expect(tree).toHaveStyleRule('font-size', '50px', {
media: '(min-width: 420px)'
})
expect(tree).toHaveStyleRule('font-size', '70px', {
media: '(min-width: 920px) and (max-width: 1200px)'
})
expect(tree).toHaveStyleRule('font-size', '80px', {
media: 'screen and (max-width: 1200px)'
})
expect(tree).toHaveStyleRule('font-size', '90px', {
media: 'not all and (monochrome)'
})
})
it('matches styles with target and media options', () => {
const localDivStyle = css`
color: white;
@media (min-width: 420px) {
color: green;
&:hover {
color: yellow;
}
}
`
const tree = renderer
.create(
<div css={localDivStyle}>
<span>Test</span>
</div>
)
.toJSON()
expect(tree).toHaveStyleRule('color', 'yellow', {
target: ':hover',
media: '(min-width: 420px)'
})
expect(tree).toHaveStyleRule('color', 'green', {
media: '(min-width: 420px)'
})
expect(tree).toHaveStyleRule('color', 'white')
})
it('fails if option media invalid', () => {
const Div = styled('div')`
font-size: 30px;
@media (min-width: 420px) {
font-size: 50px;
}
`
const tree = renderer.create(<Div />).toJSON()
const result = toHaveStyleRule(tree, 'font-size', '50px', {
media: '(min-width-'
})
expect(result.pass).toBe(false)
expect(result.message()).toBe('Property not found: font-size')
})
it('matches styles for a component used as selector', () => {
const Bar = styled.div``
const Foo = styled.div`
color: red;
${Bar} {
color: hotpink;
}
`
const tree = renderer
.create(
<Foo>
<Bar />
</Foo>
)
.toJSON()
expect(tree.children[0]).toHaveStyleRule('color', 'hotpink')
})
it('takes specificity into account when matching styles (basic)', () => {
const Bar = styled.div`
color: yellow;
`
const Foo = styled.div`
color: red;
${Bar} {
color: hotpink;
}
`
const tree = renderer
.create(
<Foo>
<Bar />
</Foo>
)
.toJSON()
expect(tree.children[0]).toHaveStyleRule('color', 'hotpink')
})
})