From 6979ee6a43e965527e6dabe310e8d85f106010b5 Mon Sep 17 00:00:00 2001 From: Eyo Okon Eyo <eyo.eyo@elastic.co> Date: Wed, 6 Nov 2024 17:40:30 +0100 Subject: [PATCH] fix console manager test --- .../console_manager/console_manager.tsx | 10 ++---- .../console_manager.test.tsx | 35 ++++++++++++++----- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/console_manager.tsx b/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/console_manager.tsx index deb6967da2c27..7d85f5c11575c 100644 --- a/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/console_manager.tsx +++ b/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/console_manager.tsx @@ -113,13 +113,9 @@ export const ConsoleManager = memo<ConsoleManagerProps>(({ storage = {}, childre validateIdOrThrow(id); setConsoleStorage((prevState) => { - return { - ...prevState, - [id]: { - ...prevState[id], - isOpen: false, - }, - }; + const newState = { ...prevState }; + newState[id].isOpen = false; + return newState; }); }, [validateIdOrThrow] // << IMPORTANT: this callback should have only immutable dependencies diff --git a/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/integration_tests/console_manager.test.tsx b/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/integration_tests/console_manager.test.tsx index 9797005cde7d8..06f95aa0b0269 100644 --- a/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/integration_tests/console_manager.test.tsx +++ b/x-pack/plugins/security_solution/public/management/components/console/components/console_manager/integration_tests/console_manager.test.tsx @@ -103,20 +103,27 @@ describe('When using ConsoleManager', () => { ); }); - it('should hide a console by `id`', () => { + it('should hide a console by `id`', async () => { renderHook(); const { id: consoleId } = registerNewConsole(); + + let consoleClient: ReturnType<ConsoleManagerClient['getOne']>; + + act(() => { + consoleClient = renderResult.result.current.getOne(consoleId); + }); + act(() => { renderResult.result.current.show(consoleId); }); - expect(renderResult.result.current.getOne(consoleId)!.isVisible()).toBe(true); + await waitFor(() => expect(consoleClient!.isVisible()).toBe(true)); act(() => { renderResult.result.current.hide(consoleId); }); - expect(renderResult.result.current.getOne(consoleId)!.isVisible()).toBe(false); + await waitFor(() => expect(consoleClient!.isVisible()).toBe(false)); }); it('should throw if attempting to hide a console with invalid `id`', () => { @@ -163,7 +170,9 @@ describe('When using ConsoleManager', () => { beforeEach(() => { renderHook(); ({ id: consoleId } = registerNewConsole()); - registeredConsole = renderResult.result.current.getOne(consoleId)!; + act(() => { + registeredConsole = renderResult.result.current.getOne(consoleId)!; + }); }); it('should have the expected interface', () => { @@ -178,20 +187,30 @@ describe('When using ConsoleManager', () => { }); it('should display the console when `.show()` is called', async () => { - registeredConsole.show(); + act(() => { + registeredConsole.show(); + }); await waitFor(() => expect(registeredConsole.isVisible()).toBe(true)); }); it('should hide the console when `.hide()` is called', async () => { - registeredConsole.show(); + act(() => { + registeredConsole.show(); + }); + await waitFor(() => expect(registeredConsole.isVisible()).toBe(true)); - registeredConsole.hide(); + act(() => { + registeredConsole.hide(); + }); + await waitFor(() => expect(registeredConsole.isVisible()).toBe(false)); }); it('should un-register the console when `.terminate() is called', async () => { - registeredConsole.terminate(); + act(() => { + registeredConsole.terminate(); + }); await waitFor(() => expect(renderResult.result.current.getOne(consoleId)).toBeUndefined()); }); });