Skip to content

Commit

Permalink
[8.x] [Cases][Flaky Test] Memoize test provider (#195088) (#197008)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Cases][Flaky Test] Memoize test provider
(#195088)](#195088)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Julian
Gernun","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-21T08:55:45Z","message":"[Cases][Flaky
Test] Memoize test provider (#195088)\n\n## Summary\r\n\r\n- Memo as
much as possible in the common component that mocks providers\r\nfor the
component tests in cases.\r\n- Also fixes an issue with fake jest timers
in
`utility_bar.test`","sha":"498da89e272b01fa33bf31998d65de2254c9c56c","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","v9.0.0","backport:prev-minor"],"title":"[Cases][Flaky
Test] Memoize test provider
","number":195088,"url":"https://github.com/elastic/kibana/pull/195088","mergeCommit":{"message":"[Cases][Flaky
Test] Memoize test provider (#195088)\n\n## Summary\r\n\r\n- Memo as
much as possible in the common component that mocks providers\r\nfor the
component tests in cases.\r\n- Also fixes an issue with fake jest timers
in
`utility_bar.test`","sha":"498da89e272b01fa33bf31998d65de2254c9c56c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195088","number":195088,"mergeCommit":{"message":"[Cases][Flaky
Test] Memoize test provider (#195088)\n\n## Summary\r\n\r\n- Memo as
much as possible in the common component that mocks providers\r\nfor the
component tests in cases.\r\n- Also fixes an issue with fake jest timers
in
`utility_bar.test`","sha":"498da89e272b01fa33bf31998d65de2254c9c56c"}}]}]
BACKPORT-->

Co-authored-by: Julian Gernun <[email protected]>
  • Loading branch information
kibanamachine and jcger authored Oct 21, 2024
1 parent 2eff4fd commit 933cbc0
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://github.com/testing-library/user-event/issues/833#issuecomment-1171452841
user = userEvent.setup({
advanceTimers: jest.advanceTimersByTime,
Expand Down

0 comments on commit 933cbc0

Please sign in to comment.