Skip to content

Commit

Permalink
[Cases][Flaky Test] Memoize test provider (elastic#195088)
Browse files Browse the repository at this point in the history
## 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`
  • Loading branch information
jcger authored Oct 21, 2024
1 parent ad91668 commit 498da89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
46 changes: 21 additions & 25 deletions x-pack/plugins/cases/public/common/mock/test_providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,20 @@ const TestProvidersComponent: React.FC<TestProviderProps> = ({
});

const getFilesClient = mockGetFilesClient();
const casesProviderValue = {
externalReferenceAttachmentTypeRegistry,
persistableStateAttachmentTypeRegistry,
features,
owner,
permissions,
getFilesClient,
};

return (
<KibanaRenderContextProvider i18n={coreStart.i18n} theme={coreStart.theme}>
<KibanaContextProvider services={services}>
<MemoryRouter>
<CasesProvider
value={{
externalReferenceAttachmentTypeRegistry,
persistableStateAttachmentTypeRegistry,
features,
owner,
permissions,
getFilesClient,
}}
queryClient={queryClient}
>
<CasesProvider value={casesProviderValue} queryClient={queryClient}>
<FilesContext client={createMockFilesClient()}>{children}</FilesContext>
</CasesProvider>
</MemoryRouter>
Expand Down Expand Up @@ -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 }) => (
<KibanaRenderContextProvider i18n={coreStart.i18n} theme={coreStart.theme}>
<KibanaContextProvider services={services}>
<MemoryRouter>
<CasesProvider
value={{
externalReferenceAttachmentTypeRegistry,
persistableStateAttachmentTypeRegistry,
features,
owner,
permissions,
releasePhase,
getFilesClient,
}}
queryClient={queryClient}
>
<CasesProvider value={casesProviderValue} queryClient={queryClient}>
{children}
</CasesProvider>
</MemoryRouter>
Expand All @@ -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,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:/testing-library/user-event/issues/833#issuecomment-1171452841
user = userEvent.setup({
advanceTimers: jest.advanceTimersByTime,
Expand Down

0 comments on commit 498da89

Please sign in to comment.