1
- import React from 'react'
1
+ /** @jsx jsx */
2
2
import 'test-utils/setup-env'
3
+ import React from 'react'
4
+ import { act } from 'react'
3
5
import prettyFormat from 'pretty-format'
4
- /** @jsx jsx */
5
6
import { css , jsx , CacheProvider } from '@emotion/react'
6
7
import createCache from '@emotion/cache'
7
8
import { createSerializer } from '@emotion/jest'
8
9
import { render } from '@testing-library/react'
10
+ import * as testRenderer from 'react-test-renderer'
9
11
import { ignoreConsoleErrors } from 'test-utils'
10
12
11
13
let emotionPlugin = createSerializer ( )
@@ -21,14 +23,18 @@ describe('jest-emotion with dom elements', () => {
21
23
width : 100% ;
22
24
`
23
25
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 , {
32
38
plugins : [ emotionPlugin , ReactElement , ReactTestComponent , DOMElement ]
33
39
} )
34
40
@@ -62,21 +68,23 @@ describe('jest-emotion with DOM elements disabled', () => {
62
68
width : 100% ;
63
69
`
64
70
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
+ )
70
78
)
71
79
72
- const output = prettyFormat ( container . firstChild , {
80
+ const output = prettyFormat ( tree . toJSON ( ) , {
73
81
plugins : [ emotionPlugin , ReactElement , ReactTestComponent , DOMElement ]
74
82
} )
75
83
76
84
expect ( output ) . toMatchSnapshot ( )
77
85
} )
78
86
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 ( ) => {
80
88
const divRef = React . createRef ( )
81
89
render (
82
90
< div css = { divStyle } ref = { divRef } >
@@ -143,7 +151,29 @@ describe('jest-emotion with nested selectors', () => {
143
151
}
144
152
`
145
153
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' , ( ) => {
147
177
const { container } = render ( < div css = { divStyle } /> )
148
178
149
179
const output = prettyFormat ( container . firstChild , {
0 commit comments