Skip to content

Commit 35b151c

Browse files
committed
tweak
1 parent fb2f0c4 commit 35b151c

File tree

5 files changed

+77
-28
lines changed

5 files changed

+77
-28
lines changed

packages/jest/src/matchers.js

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ function toHaveStyleRule(
4444
value,
4545
options /* ?: { target?: string | RegExp, media?: string } */ = {}
4646
) {
47+
if (Array.isArray(received)) {
48+
throw new Error(
49+
'`toHaveStyleRule` expects to receive a single element but it received an array.'
50+
)
51+
}
4752
const { target, media } = options
4853
const classNames = getClassNamesFromNodes([received])
4954
const cssString = getStylesFromClassNames(classNames, getStyleElements())

packages/jest/src/utils.js

-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ function getClassNamesFromCheerio(selectors, node) {
100100
}
101101

102102
function getClassNamesFromDOMElement(selectors, node) {
103-
if (!isDOMElement(node)) {
104-
throw new Error('`toHaveStyleRule` expects to receive an element.')
105-
}
106103
return getClassNames(selectors, node.getAttribute('class'))
107104
}
108105

packages/jest/test/__snapshots__/printer.test.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ exports[`jest-emotion with DOM elements disabled replaces class names and insert
4040
}
4141
4242
<div
43-
class="emotion-0"
43+
className="emotion-0"
4444
>
4545
<svg
46-
class="emotion-1"
46+
className="emotion-1"
4747
/>
4848
</div>"
4949
`;
@@ -76,10 +76,10 @@ exports[`jest-emotion with dom elements replaces class names and inserts styles
7676
}
7777
7878
<div
79-
class="emotion-0"
79+
className="emotion-0"
8080
>
8181
<svg
82-
class="emotion-1"
82+
className="emotion-1"
8383
/>
8484
</div>"
8585
`;

packages/jest/test/matchers.test.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/** @jsx jsx */
22
import 'test-utils/setup-env'
33
import React from 'react'
4+
import { act } from 'react'
45
import { render } from '@testing-library/react'
6+
import * as testRenderer from 'react-test-renderer'
57
import { css, jsx } from '@emotion/react'
68
import styled from '@emotion/styled'
79
import { matchers } from '@emotion/jest'
@@ -292,11 +294,26 @@ describe('toHaveStyleRule', () => {
292294
expect(container.firstChild.firstChild).toHaveStyleRule('color', 'hotpink')
293295
})
294296

295-
test('should throw a friendly error when it receives non-element', () => {
297+
test('should throw a friendly error when it receives an array', async () => {
298+
const tree = (
299+
await act(() =>
300+
testRenderer.create(
301+
<>
302+
<div
303+
css={css`
304+
color: hotpink;
305+
`}
306+
/>
307+
{'Some text'}
308+
</>
309+
)
310+
)
311+
).toJSON()
312+
296313
expect(() =>
297-
expect([]).toHaveStyleRule('color', 'hotpink')
314+
expect(tree).toHaveStyleRule('color', 'hotpink')
298315
).toThrowErrorMatchingInlineSnapshot(
299-
`"\`toHaveStyleRule\` expects to receive an element."`
316+
`"\`toHaveStyleRule\` expects to receive a single element but it received an array."`
300317
)
301318
})
302319
})

packages/jest/test/printer.test.js

+48-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import React from 'react'
1+
/** @jsx jsx */
22
import 'test-utils/setup-env'
3+
import React from 'react'
4+
import { act } from 'react'
35
import prettyFormat from 'pretty-format'
4-
/** @jsx jsx */
56
import { css, jsx, CacheProvider } from '@emotion/react'
67
import createCache from '@emotion/cache'
78
import { createSerializer } from '@emotion/jest'
89
import { render } from '@testing-library/react'
10+
import * as testRenderer from 'react-test-renderer'
911
import { ignoreConsoleErrors } from 'test-utils'
1012

1113
let emotionPlugin = createSerializer()
@@ -21,14 +23,18 @@ describe('jest-emotion with dom elements', () => {
2123
width: 100%;
2224
`
2325

24-
test('replaces class names and inserts styles into React test component snapshots', () => {
25-
const { container } = render(
26-
<div css={divStyle}>
27-
<svg css={svgStyle} />
28-
</div>
29-
)
30-
31-
const output = prettyFormat(container.firstChild, {
26+
test('replaces class names and inserts styles into React test component snapshots', async () => {
27+
const tree = (
28+
await act(() =>
29+
testRenderer.create(
30+
<div css={divStyle}>
31+
<svg css={svgStyle} />
32+
</div>
33+
)
34+
)
35+
).toJSON()
36+
37+
const output = prettyFormat(tree, {
3238
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
3339
})
3440

@@ -62,21 +68,23 @@ describe('jest-emotion with DOM elements disabled', () => {
6268
width: 100%;
6369
`
6470

65-
test('replaces class names and inserts styles into React test component snapshots', () => {
66-
const { container } = render(
67-
<div css={divStyle}>
68-
<svg css={svgStyle} />
69-
</div>
71+
test('replaces class names and inserts styles into React test component snapshots', async () => {
72+
const tree = await act(() =>
73+
testRenderer.create(
74+
<div css={divStyle}>
75+
<svg css={svgStyle} />
76+
</div>
77+
)
7078
)
7179

72-
const output = prettyFormat(container.firstChild, {
80+
const output = prettyFormat(tree.toJSON(), {
7381
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
7482
})
7583

7684
expect(output).toMatchSnapshot()
7785
})
7886

79-
test('does not replace class names or insert styles into DOM element snapshots', () => {
87+
test('does not replace class names or insert styles into DOM element snapshots', async () => {
8088
const divRef = React.createRef()
8189
render(
8290
<div css={divStyle} ref={divRef}>
@@ -143,7 +151,29 @@ describe('jest-emotion with nested selectors', () => {
143151
}
144152
`
145153

146-
test('replaces class names and inserts styles into React test component snapshots', () => {
154+
test('replaces class names and inserts styles into React test component snapshots', async () => {
155+
const tree = (
156+
await act(() => testRenderer.create(<div css={divStyle} />))
157+
).toJSON()
158+
159+
const output = prettyFormat(tree, {
160+
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
161+
})
162+
163+
expect(output).toBe(`.emotion-0 {
164+
color: blue;
165+
}
166+
167+
header .emotion-0 {
168+
color: red;
169+
}
170+
171+
<div
172+
className="emotion-0"
173+
/>`)
174+
})
175+
176+
test('replaces class names and inserts styles into DOM element snapshots', () => {
147177
const { container } = render(<div css={divStyle} />)
148178

149179
const output = prettyFormat(container.firstChild, {

0 commit comments

Comments
 (0)