diff --git a/x-pack/plugins/security_solution/public/cases/components/all_cases/index.test.tsx b/x-pack/plugins/security_solution/public/cases/components/all_cases/index.test.tsx index f5ed151ebac3c..45bc9eece812a 100644 --- a/x-pack/plugins/security_solution/public/cases/components/all_cases/index.test.tsx +++ b/x-pack/plugins/security_solution/public/cases/components/all_cases/index.test.tsx @@ -15,7 +15,7 @@ import { useGetCasesMockState } from '../../containers/mock'; import * as i18n from './translations'; import { useKibana } from '../../../common/lib/kibana'; -import { createUseKibanaMock } from '../../../common/mock/kibana_react'; +import { createStartServicesMock } from '../../../common/mock/kibana_react'; import { getEmptyTagValue } from '../../../common/components/empty_value'; import { useDeleteCases } from '../../containers/use_delete_cases'; import { useGetCases } from '../../containers/use_get_cases'; @@ -99,10 +99,10 @@ describe('AllCases', () => { beforeEach(() => { jest.resetAllMocks(); navigateToApp = jest.fn(); - const kibanaMock = createUseKibanaMock()(); + const servicesMock = createStartServicesMock(); useKibanaMock.mockReturnValue({ - ...kibanaMock, services: { + ...servicesMock, application: { navigateToApp, }, diff --git a/x-pack/plugins/security_solution/public/cases/components/configure_cases/__mock__/index.tsx b/x-pack/plugins/security_solution/public/cases/components/configure_cases/__mock__/index.tsx index 23c76953a6a0f..8cb53d65e9772 100644 --- a/x-pack/plugins/security_solution/public/cases/components/configure_cases/__mock__/index.tsx +++ b/x-pack/plugins/security_solution/public/cases/components/configure_cases/__mock__/index.tsx @@ -8,7 +8,7 @@ import { Connector } from '../../../containers/configure/types'; import { ReturnConnectors } from '../../../containers/configure/use_connectors'; import { connectorsMock } from '../../../containers/configure/mock'; import { ReturnUseCaseConfigure } from '../../../containers/configure/use_configure'; -import { createUseKibanaMock } from '../../../../common/mock/kibana_react'; +import { createStartServicesMock } from '../../../../common/mock/kibana_react'; export { mapping } from '../../../containers/configure/mock'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { actionTypeRegistryMock } from '../../../../../../triggers_actions_ui/public/application/action_type_registry.mock'; @@ -49,7 +49,7 @@ export const useConnectorsResponse: ReturnConnectors = { export const kibanaMockImplementationArgs = { services: { - ...createUseKibanaMock()().services, + ...createStartServicesMock(), triggers_actions_ui: { actionTypeRegistry: actionTypeRegistryMock.create() }, }, }; diff --git a/x-pack/plugins/security_solution/public/cases/components/use_all_cases_modal/index.test.tsx b/x-pack/plugins/security_solution/public/cases/components/use_all_cases_modal/index.test.tsx index b5bf68cbf6dc8..cfc3a065bdbc5 100644 --- a/x-pack/plugins/security_solution/public/cases/components/use_all_cases_modal/index.test.tsx +++ b/x-pack/plugins/security_solution/public/cases/components/use_all_cases_modal/index.test.tsx @@ -14,7 +14,7 @@ import '../../../common/mock/match_media'; import { TimelineId } from '../../../../common/types/timeline'; import { useAllCasesModal, UseAllCasesModalProps, UseAllCasesModalReturnedValues } from '.'; import { TestProviders } from '../../../common/mock'; -import { createUseKibanaMock } from '../../../common/mock/kibana_react'; +import { createStartServicesMock } from '../../../common/mock/kibana_react'; jest.mock('../../../common/lib/kibana'); @@ -25,10 +25,10 @@ describe('useAllCasesModal', () => { beforeEach(() => { jest.clearAllMocks(); - const kibanaMock = createUseKibanaMock()(); + const servicesMock = createStartServicesMock(); useKibanaMock.mockImplementation(() => ({ - ...kibanaMock, services: { + ...servicesMock, application: { navigateToApp, }, diff --git a/x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx b/x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx index 7085894e4a51c..79bf724469e65 100644 --- a/x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx +++ b/x-pack/plugins/security_solution/public/common/containers/local_storage/use_messages_storage.test.tsx @@ -6,7 +6,7 @@ import { renderHook, act } from '@testing-library/react-hooks'; import { useKibana } from '../../lib/kibana'; -import { createUseKibanaMock } from '../../mock/kibana_react'; +import { createStartServicesMock } from '../../mock/kibana_react'; import { useMessagesStorage, UseMessagesStorage } from './use_messages_storage'; jest.mock('../../lib/kibana'); @@ -14,7 +14,7 @@ const useKibanaMock = useKibana as jest.Mock; describe('useLocalStorage', () => { beforeEach(() => { - const services = { ...createUseKibanaMock()().services }; + const services = createStartServicesMock(); useKibanaMock.mockImplementation(() => ({ services })); services.storage.store.clear(); }); diff --git a/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts b/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts index 5f4285f2747ae..f932af4bbb247 100644 --- a/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts +++ b/x-pack/plugins/security_solution/public/common/lib/kibana/__mocks__/index.ts @@ -9,19 +9,21 @@ import { createKibanaContextProviderMock, createUseUiSettingMock, createUseUiSetting$Mock, - createUseKibanaMock, + createStartServicesMock, createWithKibanaMock, } from '../../../mock/kibana_react'; export const KibanaServices = { get: jest.fn(), getKibanaVersion: jest.fn(() => '8.0.0') }; -export const useKibana = jest.fn(createUseKibanaMock()); +export const useKibana = jest.fn().mockReturnValue({ services: createStartServicesMock() }); export const useUiSetting = jest.fn(createUseUiSettingMock()); export const useUiSetting$ = jest.fn(createUseUiSetting$Mock()); -export const useHttp = jest.fn(() => useKibana().services.http); +export const useHttp = jest.fn().mockReturnValue(createStartServicesMock().http); export const useTimeZone = jest.fn(); export const useDateFormat = jest.fn(); export const useBasePath = jest.fn(() => '/test/base/path'); -export const useToasts = jest.fn(() => notificationServiceMock.createStartContract().toasts); +export const useToasts = jest + .fn() + .mockReturnValue(notificationServiceMock.createStartContract().toasts); export const useCurrentUser = jest.fn(); export const withKibana = jest.fn(createWithKibanaMock()); export const KibanaContextProvider = jest.fn(createKibanaContextProviderMock()); diff --git a/x-pack/plugins/security_solution/public/common/mock/kibana_react.ts b/x-pack/plugins/security_solution/public/common/mock/kibana_react.ts index bdb8ca85b0d77..797e2a4eda69e 100644 --- a/x-pack/plugins/security_solution/public/common/mock/kibana_react.ts +++ b/x-pack/plugins/security_solution/public/common/mock/kibana_react.ts @@ -28,9 +28,9 @@ import { import { createKibanaCoreStartMock, createKibanaPluginsStartMock } from './kibana_core'; import { StartServices } from '../../types'; import { createSecuritySolutionStorageMock } from './mock_local_storage'; +import { uiSettingsServiceMock } from '../../../../../../src/core/public/mocks'; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export const mockUiSettings: Record = { +const mockUiSettings: Record = { [DEFAULT_TIME_RANGE]: { from: 'now-15m', to: 'now', mode: 'quick' }, [DEFAULT_REFRESH_RATE_INTERVAL]: { pause: false, value: 0 }, [DEFAULT_APP_TIME_RANGE]: { @@ -48,10 +48,7 @@ export const mockUiSettings: Record = { [DEFAULT_DARK_MODE]: false, }; -export const createUseUiSettingMock = () => ( - key: string, - defaultValue?: T -): T => { +export const createUseUiSettingMock = () => (key: string, defaultValue?: unknown): unknown => { const result = mockUiSettings[key]; if (typeof result != null) return result; @@ -66,61 +63,38 @@ export const createUseUiSettingMock = () => ( export const createUseUiSetting$Mock = () => { const useUiSettingMock = createUseUiSettingMock(); - return ( - key: string, - defaultValue?: T - ): [T, () => void] | undefined => [useUiSettingMock(key, defaultValue), jest.fn()]; + return (key: string, defaultValue?: unknown): [unknown, () => void] | undefined => [ + useUiSettingMock(key, defaultValue), + jest.fn(), + ]; }; -export const createKibanaObservable$Mock = createKibanaCoreStartMock; - -export const createUseKibanaMock = () => { +export const createStartServicesMock = (): StartServices => { const core = createKibanaCoreStartMock(); const plugins = createKibanaPluginsStartMock(); - const useUiSetting = createUseUiSettingMock(); + core.uiSettings.get.mockImplementation(createUseUiSettingMock()); const { storage } = createSecuritySolutionStorageMock(); - const services = { - ...core, - ...plugins, - uiSettings: { - ...core.uiSettings, - get: useUiSetting, - }, - storage, - }; - - return () => ({ services }); -}; - -export const createStartServices = () => { - const core = createKibanaCoreStartMock(); - const plugins = createKibanaPluginsStartMock(); - const services = ({ ...core, ...plugins, + storage, } as unknown) as StartServices; return services; }; export const createWithKibanaMock = () => { - const kibana = createUseKibanaMock()(); + const services = createStartServicesMock(); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return (Component: any) => (props: any) => { - return React.createElement(Component, { ...props, kibana }); + return (Component: unknown) => (props: unknown) => { + return React.createElement(Component as string, { ...(props as object), kibana: { services } }); }; }; export const createKibanaContextProviderMock = () => { - const kibana = createUseKibanaMock()(); - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return ({ services, ...rest }: any) => - React.createElement(KibanaContextProvider, { - ...rest, - services: { ...kibana.services, ...services }, - }); + const services = createStartServicesMock(); + + return ({ children }: { children: React.ReactNode }) => + React.createElement(KibanaContextProvider, { services }, children); }; diff --git a/x-pack/plugins/security_solution/public/common/mock/test_providers.tsx b/x-pack/plugins/security_solution/public/common/mock/test_providers.tsx index 297dc235a2a50..487cc90f6382d 100644 --- a/x-pack/plugins/security_solution/public/common/mock/test_providers.tsx +++ b/x-pack/plugins/security_solution/public/common/mock/test_providers.tsx @@ -19,7 +19,7 @@ import { ThemeProvider } from 'styled-components'; import { createStore, State } from '../store'; import { mockGlobalState } from './global_state'; -import { createKibanaContextProviderMock, createStartServices } from './kibana_react'; +import { createKibanaContextProviderMock, createStartServicesMock } from './kibana_react'; import { FieldHook, useForm } from '../../shared_imports'; import { SUB_PLUGINS_REDUCER } from './utils'; import { createSecuritySolutionStorageMock, localStorageMock } from './mock_local_storage'; @@ -38,7 +38,7 @@ export const apolloClient = new ApolloClient({ }); export const apolloClientObservable = new BehaviorSubject(apolloClient); -export const kibanaObservable = new BehaviorSubject(createStartServices()); +export const kibanaObservable = new BehaviorSubject(createStartServicesMock()); Object.defineProperty(window, 'localStorage', { value: localStorageMock(), diff --git a/x-pack/plugins/security_solution/public/overview/components/recent_cases/no_cases/index.test.tsx b/x-pack/plugins/security_solution/public/overview/components/recent_cases/no_cases/index.test.tsx index 99902a31975d0..1dfde3296b0c2 100644 --- a/x-pack/plugins/security_solution/public/overview/components/recent_cases/no_cases/index.test.tsx +++ b/x-pack/plugins/security_solution/public/overview/components/recent_cases/no_cases/index.test.tsx @@ -9,7 +9,7 @@ import { mount } from 'enzyme'; import { useKibana } from '../../../../common/lib/kibana'; import '../../../../common/mock/match_media'; -import { createUseKibanaMock, TestProviders } from '../../../../common/mock'; +import { createStartServicesMock, TestProviders } from '../../../../common/mock'; import { NoCases } from '.'; jest.mock('../../../../common/lib/kibana'); @@ -22,10 +22,10 @@ describe('RecentCases', () => { beforeEach(() => { jest.resetAllMocks(); navigateToApp = jest.fn(); - const kibanaMock = createUseKibanaMock()(); + const servicesMock = createStartServicesMock(); useKibanaMock.mockReturnValue({ - ...kibanaMock, services: { + ...servicesMock, application: { navigateToApp, getUrlForApp: jest.fn(), diff --git a/x-pack/plugins/security_solution/public/timelines/containers/local_storage/index.test.ts b/x-pack/plugins/security_solution/public/timelines/containers/local_storage/index.test.ts index e1bccbdff4889..95bd169336af3 100644 --- a/x-pack/plugins/security_solution/public/timelines/containers/local_storage/index.test.ts +++ b/x-pack/plugins/security_solution/public/timelines/containers/local_storage/index.test.ts @@ -15,7 +15,7 @@ import { import { TimelineId } from '../../../../common/types/timeline'; import { mockTimelineModel, createSecuritySolutionStorageMock } from '../../../common/mock'; import { useKibana } from '../../../common/lib/kibana'; -import { createUseKibanaMock } from '../../../common/mock/kibana_react'; +import { createStartServicesMock } from '../../../common/mock/kibana_react'; jest.mock('../../../common/lib/kibana'); @@ -28,7 +28,7 @@ describe('SiemLocalStorage', () => { jest.resetAllMocks(); useKibanaMock.mockImplementation(() => ({ services: { - ...createUseKibanaMock()().services, + ...createStartServicesMock(), storage, }, }));