Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify our kibana mocks
Browse files Browse the repository at this point in the history
* Simpler mock factory that returns an object instead of a thunk
  * We can use mockReturnValue instead of mockImplementation to
  accomplish the same
  * Allows us to replace createStartServices mock
* Uses unknown instead of any for mocks
rylnd committed Aug 13, 2020
1 parent e2ef219 commit d60f4d1
Showing 9 changed files with 40 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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,
},
Original file line number Diff line number Diff line change
@@ -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() },
},
};
Original file line number Diff line number Diff line change
@@ -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,
},
Original file line number Diff line number Diff line change
@@ -6,15 +6,15 @@

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');
const useKibanaMock = useKibana as jest.Mock;

describe('useLocalStorage', () => {
beforeEach(() => {
const services = { ...createUseKibanaMock()().services };
const services = createStartServicesMock();
useKibanaMock.mockImplementation(() => ({ services }));
services.storage.store.clear();
});
Original file line number Diff line number Diff line change
@@ -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());
60 changes: 17 additions & 43 deletions x-pack/plugins/security_solution/public/common/mock/kibana_react.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> = {
const mockUiSettings: Record<string, unknown> = {
[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<string, any> = {
[DEFAULT_DARK_MODE]: false,
};

export const createUseUiSettingMock = () => <T extends unknown = string>(
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 = () => <T extends unknown = string>(
export const createUseUiSetting$Mock = () => {
const useUiSettingMock = createUseUiSettingMock();

return <T extends unknown = string>(
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);
};
Original file line number Diff line number Diff line change
@@ -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(),
Original file line number Diff line number Diff line change
@@ -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(),
Original file line number Diff line number Diff line change
@@ -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,
},
}));

0 comments on commit d60f4d1

Please sign in to comment.