@@ -2,7 +2,7 @@ import userEvent from '@testing-library/user-event';
2
2
import { screen , render , waitFor , fireEvent } from '@testing-library/react' ;
3
3
import JsonView from '../' ;
4
4
import React from 'react' ;
5
- import { act } from 'react-test-renderer ' ;
5
+ import { act } from 'react' ;
6
6
7
7
const avatar = 'https://i.imgur.com/MK3eW3As.jpg' ;
8
8
const example = {
@@ -48,13 +48,15 @@ it('render <JsonView />, copy String test case', async () => {
48
48
expect ( copied . style ) . toHaveProperty ( 'margin-left' , '5px' ) ;
49
49
expect ( copied . getAttribute ( 'fill' ) ) . toEqual ( 'var(--w-rjv-copied-color, currentColor)' ) ;
50
50
expect ( copied . tagName ) . toEqual ( 'svg' ) ;
51
- await user . click ( copied ) ;
52
- act ( ( ) => {
53
- expect ( copied . getAttribute ( 'fill' ) ) . toEqual ( 'var(--w-rjv-copied-success-color, #28a745)' ) ;
51
+ await act ( async ( ) => {
52
+ await user . click ( copied ) ;
54
53
} ) ;
54
+ expect ( copied . getAttribute ( 'fill' ) ) . toEqual ( 'var(--w-rjv-copied-success-color, #28a745)' ) ;
55
55
// Assertions
56
56
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( JSON . stringify ( example , null , 2 ) ) ;
57
- await user . unhover ( container . lastElementChild ! ) ;
57
+ await act ( async ( ) => {
58
+ await user . unhover ( container . lastElementChild ! ) ;
59
+ } ) ;
58
60
const countInfo = screen . getByTestId ( 'countInfo' ) ;
59
61
expect ( countInfo . nextElementSibling ) . toBeNull ( ) ;
60
62
await waitFor ( ( ) => {
@@ -82,7 +84,9 @@ it('render <JsonView />, copy Number test case', async () => {
82
84
fireEvent . mouseEnter ( lineDom ) ;
83
85
const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
84
86
expect ( copied . tagName ) . toEqual ( 'svg' ) ;
85
- await user . click ( copied ) ;
87
+ await waitFor ( async ( ) => {
88
+ await user . click ( copied ) ;
89
+ } ) ;
86
90
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( '123' ) ;
87
91
jest . restoreAllMocks ( ) ;
88
92
} ) ;
@@ -112,7 +116,9 @@ it('render <JsonView.Copied />, copy Number test case', async () => {
112
116
const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
113
117
expect ( copied . tagName ) . toEqual ( 'SPAN' ) ;
114
118
expect ( copied . innerHTML ) . toEqual ( 'xx' ) ;
115
- await user . click ( copied ) ;
119
+ await waitFor ( async ( ) => {
120
+ await user . click ( copied ) ;
121
+ } ) ;
116
122
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( '123' ) ;
117
123
jest . restoreAllMocks ( ) ;
118
124
} ) ;
@@ -134,7 +140,9 @@ it('render <JsonView.Copied />, copy NaN test case', async () => {
134
140
const lineDom = quote . parentElement ?. parentElement ! ;
135
141
fireEvent . mouseEnter ( lineDom ) ;
136
142
const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
137
- await user . click ( copied ) ;
143
+ await waitFor ( async ( ) => {
144
+ await user . click ( copied ) ;
145
+ } ) ;
138
146
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( 'NaN' ) ;
139
147
jest . restoreAllMocks ( ) ;
140
148
} ) ;
@@ -156,7 +164,9 @@ it('render <JsonView.Copied />, copy Infinity test case', async () => {
156
164
const lineDom = quote . parentElement ?. parentElement ! ;
157
165
fireEvent . mouseEnter ( lineDom ) ;
158
166
const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
159
- await user . click ( copied ) ;
167
+ await waitFor ( async ( ) => {
168
+ await user . click ( copied ) ;
169
+ } ) ;
160
170
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( 'Infinity' ) ;
161
171
jest . restoreAllMocks ( ) ;
162
172
} ) ;
@@ -179,7 +189,9 @@ it('render <JsonView.Copied />, copy BigInt test case', async () => {
179
189
const lineDom = quote . parentElement ?. parentElement ! ;
180
190
fireEvent . mouseEnter ( lineDom ) ;
181
191
const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
182
- await user . click ( copied ) ;
192
+ await waitFor ( async ( ) => {
193
+ await user . click ( copied ) ;
194
+ } ) ;
183
195
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( '1000n' ) ;
184
196
fireEvent . mouseLeave ( lineDom ) ;
185
197
const bigint = screen . getAllByTestId ( 'bigint' ) [ 1 ] ;
@@ -202,10 +214,14 @@ it('render <JsonView.Copied />, copy Object with BigInt test case', async () =>
202
214
expect ( container . firstElementChild ) . toBeInstanceOf ( Element ) ;
203
215
fireEvent . mouseEnter ( container . lastElementChild ! ) ;
204
216
const copied = screen . getByTestId ( 'copied' ) ;
205
- await user . click ( copied ) ;
217
+ await waitFor ( async ( ) => {
218
+ await user . click ( copied ) ;
219
+ } ) ;
206
220
// Assertions
207
221
expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( JSON . stringify ( exampleWithBigIntAnswer , null , 2 ) ) ;
208
- await user . unhover ( container . lastElementChild ! ) ;
222
+ await waitFor ( async ( ) => {
223
+ await user . unhover ( container . lastElementChild ! ) ;
224
+ } ) ;
209
225
// Restore the original implementation
210
226
jest . restoreAllMocks ( ) ;
211
227
} ) ;
0 commit comments