@@ -2,7 +2,7 @@ import userEvent from '@testing-library/user-event';
22import { screen , render , waitFor , fireEvent } from '@testing-library/react' ;
33import JsonView from '../' ;
44import React from 'react' ;
5- import { act } from 'react-test-renderer ' ;
5+ import { act } from 'react' ;
66
77const avatar = 'https://i.imgur.com/MK3eW3As.jpg' ;
88const example = {
@@ -48,13 +48,15 @@ it('render <JsonView />, copy String test case', async () => {
4848 expect ( copied . style ) . toHaveProperty ( 'margin-left' , '5px' ) ;
4949 expect ( copied . getAttribute ( 'fill' ) ) . toEqual ( 'var(--w-rjv-copied-color, currentColor)' ) ;
5050 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 ) ;
5453 } ) ;
54+ expect ( copied . getAttribute ( 'fill' ) ) . toEqual ( 'var(--w-rjv-copied-success-color, #28a745)' ) ;
5555 // Assertions
5656 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+ } ) ;
5860 const countInfo = screen . getByTestId ( 'countInfo' ) ;
5961 expect ( countInfo . nextElementSibling ) . toBeNull ( ) ;
6062 await waitFor ( ( ) => {
@@ -82,7 +84,9 @@ it('render <JsonView />, copy Number test case', async () => {
8284 fireEvent . mouseEnter ( lineDom ) ;
8385 const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
8486 expect ( copied . tagName ) . toEqual ( 'svg' ) ;
85- await user . click ( copied ) ;
87+ await waitFor ( async ( ) => {
88+ await user . click ( copied ) ;
89+ } ) ;
8690 expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( '123' ) ;
8791 jest . restoreAllMocks ( ) ;
8892} ) ;
@@ -112,7 +116,9 @@ it('render <JsonView.Copied />, copy Number test case', async () => {
112116 const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
113117 expect ( copied . tagName ) . toEqual ( 'SPAN' ) ;
114118 expect ( copied . innerHTML ) . toEqual ( 'xx' ) ;
115- await user . click ( copied ) ;
119+ await waitFor ( async ( ) => {
120+ await user . click ( copied ) ;
121+ } ) ;
116122 expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( '123' ) ;
117123 jest . restoreAllMocks ( ) ;
118124} ) ;
@@ -134,7 +140,9 @@ it('render <JsonView.Copied />, copy NaN test case', async () => {
134140 const lineDom = quote . parentElement ?. parentElement ! ;
135141 fireEvent . mouseEnter ( lineDom ) ;
136142 const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
137- await user . click ( copied ) ;
143+ await waitFor ( async ( ) => {
144+ await user . click ( copied ) ;
145+ } ) ;
138146 expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( 'NaN' ) ;
139147 jest . restoreAllMocks ( ) ;
140148} ) ;
@@ -156,7 +164,9 @@ it('render <JsonView.Copied />, copy Infinity test case', async () => {
156164 const lineDom = quote . parentElement ?. parentElement ! ;
157165 fireEvent . mouseEnter ( lineDom ) ;
158166 const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
159- await user . click ( copied ) ;
167+ await waitFor ( async ( ) => {
168+ await user . click ( copied ) ;
169+ } ) ;
160170 expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( 'Infinity' ) ;
161171 jest . restoreAllMocks ( ) ;
162172} ) ;
@@ -179,7 +189,9 @@ it('render <JsonView.Copied />, copy BigInt test case', async () => {
179189 const lineDom = quote . parentElement ?. parentElement ! ;
180190 fireEvent . mouseEnter ( lineDom ) ;
181191 const copied = screen . getAllByTestId ( 'copied' ) [ 1 ] ;
182- await user . click ( copied ) ;
192+ await waitFor ( async ( ) => {
193+ await user . click ( copied ) ;
194+ } ) ;
183195 expect ( navigator . clipboard . writeText ) . toHaveBeenCalledWith ( '1000n' ) ;
184196 fireEvent . mouseLeave ( lineDom ) ;
185197 const bigint = screen . getAllByTestId ( 'bigint' ) [ 1 ] ;
@@ -202,10 +214,14 @@ it('render <JsonView.Copied />, copy Object with BigInt test case', async () =>
202214 expect ( container . firstElementChild ) . toBeInstanceOf ( Element ) ;
203215 fireEvent . mouseEnter ( container . lastElementChild ! ) ;
204216 const copied = screen . getByTestId ( 'copied' ) ;
205- await user . click ( copied ) ;
217+ await waitFor ( async ( ) => {
218+ await user . click ( copied ) ;
219+ } ) ;
206220 // Assertions
207221 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+ } ) ;
209225 // Restore the original implementation
210226 jest . restoreAllMocks ( ) ;
211227} ) ;
0 commit comments