From 498da89e272b01fa33bf31998d65de2254c9c56c Mon Sep 17 00:00:00 2001 From: Julian Gernun <17549662+jcger@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:55:45 +0200 Subject: [PATCH] [Cases][Flaky Test] Memoize test provider (#195088) ## Summary - Memo as much as possible in the common component that mocks providers for the component tests in cases. - Also fixes an issue with fake jest timers in `utility_bar.test` --- .../public/common/mock/test_providers.tsx | 46 +++++++++---------- .../components/all_cases/utility_bar.test.tsx | 10 ++++ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/x-pack/plugins/cases/public/common/mock/test_providers.tsx b/x-pack/plugins/cases/public/common/mock/test_providers.tsx index aef1024cc8afc..257ac4b1f8293 100644 --- a/x-pack/plugins/cases/public/common/mock/test_providers.tsx +++ b/x-pack/plugins/cases/public/common/mock/test_providers.tsx @@ -89,22 +89,20 @@ const TestProvidersComponent: React.FC = ({ }); const getFilesClient = mockGetFilesClient(); + const casesProviderValue = { + externalReferenceAttachmentTypeRegistry, + persistableStateAttachmentTypeRegistry, + features, + owner, + permissions, + getFilesClient, + }; return ( - + {children} @@ -170,23 +168,20 @@ export const createAppMockRenderer = ({ }); const getFilesClient = mockGetFilesClient(); - + const casesProviderValue = { + externalReferenceAttachmentTypeRegistry, + persistableStateAttachmentTypeRegistry, + features, + owner, + permissions, + releasePhase, + getFilesClient, + }; const AppWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => ( - + {children} @@ -195,10 +190,11 @@ export const createAppMockRenderer = ({ ); AppWrapper.displayName = 'AppWrapper'; + const memoizedAppWrapper = React.memo(AppWrapper); const render: UiRender = (ui, options) => { return reactRender(ui, { - wrapper: AppWrapper, + wrapper: memoizedAppWrapper, ...options, }); }; diff --git a/x-pack/plugins/cases/public/components/all_cases/utility_bar.test.tsx b/x-pack/plugins/cases/public/components/all_cases/utility_bar.test.tsx index bd72a460c8c71..63d8cd2e5faab 100644 --- a/x-pack/plugins/cases/public/components/all_cases/utility_bar.test.tsx +++ b/x-pack/plugins/cases/public/components/all_cases/utility_bar.test.tsx @@ -41,7 +41,17 @@ describe('Severity form field', () => { showClearFiltersButton: false, }; + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + sessionStorage.removeItem(localStorageKey); + }); + beforeEach(() => { + jest.useFakeTimers(); // Workaround for timeout via https://github.com/testing-library/user-event/issues/833#issuecomment-1171452841 user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime,