Skip to content

Commit df984b7

Browse files
umbopepatocnasikas
authored andcommitted
[Cases] Avoid empty renders of CasesProvider
1 parent 494f57d commit df984b7

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

x-pack/plugins/cases/public/common/mock/test_providers.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ export const createAppMockRenderer = ({
191191
releasePhase,
192192
getFilesClient,
193193
}}
194+
queryClient={queryClient}
194195
>
195-
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
196+
{children}
196197
</CasesProvider>
197198
</MemoryRouter>
198199
</ThemeProvider>

x-pack/plugins/cases/public/components/cases_context/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import React, { useCallback, useMemo, useReducer } from 'react';
1212

1313
import type { ScopedFilesClient } from '@kbn/files-plugin/public';
1414
import { FilesContext } from '@kbn/shared-ux-file-context';
15+
16+
import type { QueryClient } from '@tanstack/react-query';
1517
import { QueryClientProvider } from '@tanstack/react-query';
1618
import type { CasesContextStoreAction } from './cases_context_reducer';
1719
import type {
@@ -63,7 +65,7 @@ export interface CasesContextProps
6365

6466
export const CasesContext = React.createContext<CasesContextValue | undefined>(undefined);
6567

66-
export const CasesProvider: React.FC<{ value: CasesContextProps }> = ({
68+
export const CasesProvider: React.FC<{ value: CasesContextProps; queryClient?: QueryClient }> = ({
6769
children,
6870
value: {
6971
externalReferenceAttachmentTypeRegistry,
@@ -75,6 +77,7 @@ export const CasesProvider: React.FC<{ value: CasesContextProps }> = ({
7577
releasePhase = 'ga',
7678
getFilesClient,
7779
},
80+
queryClient = casesQueryClient,
7881
}) => {
7982
const { appId, appTitle } = useApplication();
8083
const [state, dispatch] = useReducer(casesContextReducer, getInitialCasesContextState());

x-pack/plugins/cases/public/components/cases_context/use_application.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55
* 2.0.
66
*/
77
import useObservable from 'react-use/lib/useObservable';
8+
import type { BehaviorSubject, Observable } from 'rxjs';
89
import { useKibana } from '../../common/lib/kibana';
910

1011
interface UseApplicationReturn {
1112
appId: string | undefined;
1213
appTitle: string | undefined;
1314
}
1415

16+
/**
17+
* Checks if the observable is stateful and, in case, sets up `useObservable` with an initial value
18+
*/
19+
const useStatefulObservable = <T,>(observable: Observable<T>) => {
20+
const defaultValue = 'value' in observable ? (observable as BehaviorSubject<T>).value : undefined;
21+
return useObservable(observable, defaultValue);
22+
};
23+
1524
export const useApplication = (): UseApplicationReturn => {
1625
const { currentAppId$, applications$ } = useKibana().services.application;
1726
// retrieve the most recent value from the BehaviorSubject
18-
const appId = useObservable(currentAppId$);
19-
const applications = useObservable(applications$);
27+
const appId = useStatefulObservable(currentAppId$);
28+
const applications = useStatefulObservable(applications$);
2029

2130
const appTitle = appId
2231
? applications?.get(appId)?.category?.label ?? applications?.get(appId)?.title

x-pack/plugins/cases/public/components/create/description.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { FormTestComponent } from '../../common/test_utils';
1818
import type { FormSchema } from '@kbn/index-management-plugin/public/shared_imports';
1919

2020
// FLAKY: https://github.com/elastic/kibana/issues/175204
21-
describe.skip('Description', () => {
21+
describe('Description', () => {
2222
let appMockRender: AppMockRenderer;
2323
const onSubmit = jest.fn();
2424
const draftStorageKey = `cases.caseView.createCase.description.markdownEditor`;

0 commit comments

Comments
 (0)