1- import { render , screen , fireEvent , waitFor } from '@testing-library/react' ;
1+ import { act , render , screen , fireEvent } from '@testing-library/react' ;
22import { getIpcApi } from '../../lib/get-ipc-api' ;
33import createCodeComponent from '../assistant-code-block' ;
44
@@ -89,6 +89,14 @@ describe( 'createCodeComponent', () => {
8989 } ) ;
9090
9191 describe ( 'when the "run" button is clicked' , ( ) => {
92+ beforeEach ( ( ) => {
93+ jest . useFakeTimers ( ) ;
94+ } ) ;
95+
96+ afterEach ( ( ) => {
97+ jest . useRealTimers ( ) ;
98+ } ) ;
99+
92100 it ( 'should display an activity indicator while running code' , async ( ) => {
93101 ( getIpcApi as jest . Mock ) . mockReturnValue ( {
94102 executeWPCLiInline : jest . fn ( ) . mockResolvedValue ( { stdout : 'Mock success' , stderr : '' } ) ,
@@ -100,9 +108,9 @@ describe( 'createCodeComponent', () => {
100108
101109 expect ( screen . getByText ( 'Running...' ) ) . toBeVisible ( ) ;
102110
103- await waitFor ( ( ) => {
104- expect ( screen . queryByText ( 'Running...' ) ) . not . toBeInTheDocument ( ) ;
105- } ) ;
111+ await act ( ( ) => jest . runOnlyPendingTimersAsync ( ) ) ;
112+
113+ expect ( screen . queryByText ( 'Running...' ) ) . not . toBeInTheDocument ( ) ;
106114 } ) ;
107115
108116 it ( 'should display the output of the successfully executed code' , async ( ) => {
@@ -113,10 +121,10 @@ describe( 'createCodeComponent', () => {
113121
114122 fireEvent . click ( screen . getByText ( 'Run' ) ) ;
115123
116- await waitFor ( ( ) => {
117- expect ( screen . getByText ( 'Success' ) ) . toBeVisible ( ) ;
118- expect ( screen . getByText ( 'Mock success ' ) ) . toBeVisible ( ) ;
119- } ) ;
124+ await act ( ( ) => jest . runOnlyPendingTimersAsync ( ) ) ;
125+
126+ expect ( screen . getByText ( 'Success ' ) ) . toBeVisible ( ) ;
127+ expect ( screen . getByText ( 'Mock success' ) ) . toBeVisible ( ) ;
120128 } ) ;
121129
122130 it ( 'should display the output of the failed code execution' , async ( ) => {
@@ -127,10 +135,10 @@ describe( 'createCodeComponent', () => {
127135
128136 fireEvent . click ( screen . getByText ( 'Run' ) ) ;
129137
130- await waitFor ( ( ) => {
131- expect ( screen . getByText ( 'Error' ) ) . toBeVisible ( ) ;
132- expect ( screen . getByText ( 'Mock error ' ) ) . toBeVisible ( ) ;
133- } ) ;
138+ await act ( ( ) => jest . runOnlyPendingTimersAsync ( ) ) ;
139+
140+ expect ( screen . getByText ( 'Error ' ) ) . toBeVisible ( ) ;
141+ expect ( screen . getByText ( 'Mock error' ) ) . toBeVisible ( ) ;
134142 } ) ;
135143 } ) ;
136144
0 commit comments