From 36c6752497c21cf784418c33644f9d439f1817c5 Mon Sep 17 00:00:00 2001 From: sejas Date: Tue, 23 Jul 2024 18:19:28 +0100 Subject: [PATCH 1/2] Remove test timers related to code block --- .../tests/ai-clear-history-reminder.test.tsx | 1 + .../tests/assistant-code-block.test.tsx | 27 +++++++++---------- .../tests/content-tab-assistant.test.tsx | 4 +-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/components/tests/ai-clear-history-reminder.test.tsx b/src/components/tests/ai-clear-history-reminder.test.tsx index 849f42ca..31c0a252 100644 --- a/src/components/tests/ai-clear-history-reminder.test.tsx +++ b/src/components/tests/ai-clear-history-reminder.test.tsx @@ -14,6 +14,7 @@ describe( 'AIClearHistoryReminder', () => { window.HTMLElement.prototype.scrollIntoView = jest.fn(); clearInput = jest.fn(); jest.clearAllMocks(); + jest.useFakeTimers(); jest.setSystemTime( MOCKED_TIME ); } ); diff --git a/src/components/tests/assistant-code-block.test.tsx b/src/components/tests/assistant-code-block.test.tsx index 2e6e6951..2cee11e4 100644 --- a/src/components/tests/assistant-code-block.test.tsx +++ b/src/components/tests/assistant-code-block.test.tsx @@ -1,4 +1,4 @@ -import { render, screen, fireEvent, act } from '@testing-library/react'; +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import { getIpcApi } from '../../lib/get-ipc-api'; import createCodeComponent from '../assistant-code-block'; @@ -100,10 +100,9 @@ describe( 'createCodeComponent', () => { expect( screen.getByText( 'Running...' ) ).toBeVisible(); - // Run code execution measurement timer - await act( () => jest.runAllTimersAsync() ); - - expect( screen.queryByText( 'Running...' ) ).not.toBeInTheDocument(); + await waitFor( () => { + expect( screen.queryByText( 'Running...' ) ).not.toBeInTheDocument(); + } ); } ); it( 'should display the output of the successfully executed code', async () => { @@ -114,11 +113,10 @@ describe( 'createCodeComponent', () => { fireEvent.click( screen.getByText( 'Run' ) ); - // Run code execution measurement timer - await act( () => jest.runAllTimersAsync() ); - - expect( screen.getByText( 'Success' ) ).toBeVisible(); - expect( screen.getByText( 'Mock success' ) ).toBeVisible(); + await waitFor( () => { + expect( screen.getByText( 'Success' ) ).toBeVisible(); + expect( screen.getByText( 'Mock success' ) ).toBeVisible(); + } ); } ); it( 'should display the output of the failed code execution', async () => { @@ -129,11 +127,10 @@ describe( 'createCodeComponent', () => { fireEvent.click( screen.getByText( 'Run' ) ); - // Run code execution measurement timer - await act( () => jest.runAllTimersAsync() ); - - expect( screen.getByText( 'Error' ) ).toBeVisible(); - expect( screen.getByText( 'Mock error' ) ).toBeVisible(); + await waitFor( () => { + expect( screen.getByText( 'Error' ) ).toBeVisible(); + expect( screen.getByText( 'Mock error' ) ).toBeVisible(); + } ); } ); } ); diff --git a/src/components/tests/content-tab-assistant.test.tsx b/src/components/tests/content-tab-assistant.test.tsx index 769c3c1f..6485876a 100644 --- a/src/components/tests/content-tab-assistant.test.tsx +++ b/src/components/tests/content-tab-assistant.test.tsx @@ -110,7 +110,6 @@ describe( 'ContentTabAssistant', () => { const storageKey = 'ai_chat_messages'; localStorage.setItem( storageKey, JSON.stringify( { [ runningSite.id ]: initialMessages } ) ); render( ); - jest.advanceTimersByTime( MIMIC_CONVERSATION_DELAY + 1000 ); await waitFor( () => { expect( screen.getByText( 'Initial message 1' ) ).toBeVisible(); expect( screen.getByText( 'Initial message 2' ) ).toBeVisible(); @@ -214,7 +213,6 @@ describe( 'ContentTabAssistant', () => { act( () => { fireEvent.change( textInput, { target: { value: 'New message' } } ); fireEvent.keyDown( textInput, { key: 'Enter', code: 'Enter' } ); - jest.advanceTimersByTime( MIMIC_CONVERSATION_DELAY + 1000 ); } ); await waitFor( () => { expect( screen.getByText( 'New message' ) ).toBeVisible(); @@ -325,6 +323,7 @@ describe( 'ContentTabAssistant', () => { test( 'clears history via reminder when last message is two hours old', async () => { const MOCKED_TIME = 1718882159928; const TWO_HOURS_DIFF = 2 * 60 * 60 * 1000; + jest.useFakeTimers(); jest.setSystemTime( MOCKED_TIME ); const storageKey = 'ai_chat_messages'; @@ -369,6 +368,7 @@ describe( 'ContentTabAssistant', () => { }, { timeout: MIMIC_CONVERSATION_DELAY + 1000 } ); + jest.useRealTimers(); } ); test( 'renders notices by importance', async () => { From cdb3e17a6ea31e30a1a000a4190c8adf2add1dfa Mon Sep 17 00:00:00 2001 From: sejas Date: Wed, 24 Jul 2024 15:17:10 +0100 Subject: [PATCH 2/2] Go back to use runOnlyPendingTimersAsync on assistant code block --- .../tests/assistant-code-block.test.tsx | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/components/tests/assistant-code-block.test.tsx b/src/components/tests/assistant-code-block.test.tsx index 2cee11e4..20b11b09 100644 --- a/src/components/tests/assistant-code-block.test.tsx +++ b/src/components/tests/assistant-code-block.test.tsx @@ -1,4 +1,4 @@ -import { render, screen, fireEvent, waitFor } from '@testing-library/react'; +import { act, render, screen, fireEvent } from '@testing-library/react'; import { getIpcApi } from '../../lib/get-ipc-api'; import createCodeComponent from '../assistant-code-block'; @@ -89,6 +89,14 @@ describe( 'createCodeComponent', () => { } ); describe( 'when the "run" button is clicked', () => { + beforeEach( () => { + jest.useFakeTimers(); + } ); + + afterEach( () => { + jest.useRealTimers(); + } ); + it( 'should display an activity indicator while running code', async () => { ( getIpcApi as jest.Mock ).mockReturnValue( { executeWPCLiInline: jest.fn().mockResolvedValue( { stdout: 'Mock success', stderr: '' } ), @@ -100,9 +108,9 @@ describe( 'createCodeComponent', () => { expect( screen.getByText( 'Running...' ) ).toBeVisible(); - await waitFor( () => { - expect( screen.queryByText( 'Running...' ) ).not.toBeInTheDocument(); - } ); + await act( () => jest.runOnlyPendingTimersAsync() ); + + expect( screen.queryByText( 'Running...' ) ).not.toBeInTheDocument(); } ); it( 'should display the output of the successfully executed code', async () => { @@ -113,10 +121,10 @@ describe( 'createCodeComponent', () => { fireEvent.click( screen.getByText( 'Run' ) ); - await waitFor( () => { - expect( screen.getByText( 'Success' ) ).toBeVisible(); - expect( screen.getByText( 'Mock success' ) ).toBeVisible(); - } ); + await act( () => jest.runOnlyPendingTimersAsync() ); + + expect( screen.getByText( 'Success' ) ).toBeVisible(); + expect( screen.getByText( 'Mock success' ) ).toBeVisible(); } ); it( 'should display the output of the failed code execution', async () => { @@ -127,10 +135,10 @@ describe( 'createCodeComponent', () => { fireEvent.click( screen.getByText( 'Run' ) ); - await waitFor( () => { - expect( screen.getByText( 'Error' ) ).toBeVisible(); - expect( screen.getByText( 'Mock error' ) ).toBeVisible(); - } ); + await act( () => jest.runOnlyPendingTimersAsync() ); + + expect( screen.getByText( 'Error' ) ).toBeVisible(); + expect( screen.getByText( 'Mock error' ) ).toBeVisible(); } ); } );