Skip to content

Commit

Permalink
Clean up our manual use of kibana mocks in tests
Browse files Browse the repository at this point in the history
* Since our useKibana mock returns a consistent mock, we can modify its
return value instead of re-mocking the entire thing
* Removes unnecessary uses of clearing/resetting mocks
  * If your mocks are configured at the beginning of each test this is
  usually unnecessary.
  * I left one case of clearAllMocks in all_cases/index.test since it
  defined several mock functions that were persistent across tests, and
  it was easier than moving their definitions to a beforeEach
* Removes some unnecessary overrides that seemed due to storage
previously not being mocked
  • Loading branch information
rylnd committed Aug 13, 2020
1 parent d60f4d1 commit 0e5dfe3
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { useGetCasesMockState } from '../../containers/mock';
import * as i18n from './translations';

import { useKibana } from '../../../common/lib/kibana';
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';
Expand All @@ -28,7 +27,7 @@ jest.mock('../../containers/use_delete_cases');
jest.mock('../../containers/use_get_cases');
jest.mock('../../containers/use_get_cases_status');

const useKibanaMock = useKibana as jest.Mock;
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;
const useDeleteCasesMock = useDeleteCases as jest.Mock;
const useGetCasesMock = useGetCases as jest.Mock;
const useGetCasesStatusMock = useGetCasesStatus as jest.Mock;
Expand Down Expand Up @@ -97,23 +96,16 @@ describe('AllCases', () => {
});
/* eslint-enable no-console */
beforeEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
navigateToApp = jest.fn();
const servicesMock = createStartServicesMock();
useKibanaMock.mockReturnValue({
services: {
...servicesMock,
application: {
navigateToApp,
},
},
});
useKibanaMock().services.application.navigateToApp = navigateToApp;
useUpdateCasesMock.mockReturnValue(defaultUpdateCases);
useGetCasesMock.mockReturnValue(defaultGetCases);
useDeleteCasesMock.mockReturnValue(defaultDeleteCases);
useGetCasesStatusMock.mockReturnValue(defaultCasesStatus);
moment.tz.setDefault('UTC');
});

it('should render AllCases', () => {
const wrapper = mount(
<TestProviders>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +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 { 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';

export const connectors: Connector[] = connectorsMock;

Expand Down Expand Up @@ -46,10 +43,3 @@ export const useConnectorsResponse: ReturnConnectors = {
connectors,
refetchConnectors: jest.fn(),
};

export const kibanaMockImplementationArgs = {
services: {
...createStartServicesMock(),
triggers_actions_ui: { actionTypeRegistry: actionTypeRegistryMock.create() },
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,40 @@ import {
ActionsConnectorsContextProvider,
ConnectorAddFlyout,
ConnectorEditFlyout,
TriggersAndActionsUIPublicPluginStart,
} from '../../../../../triggers_actions_ui/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { actionTypeRegistryMock } from '../../../../../triggers_actions_ui/public/application/action_type_registry.mock';

import { useKibana } from '../../../common/lib/kibana';
import { useConnectors } from '../../containers/configure/use_connectors';
import { useCaseConfigure } from '../../containers/configure/use_configure';
import { useGetUrlSearch } from '../../../common/components/navigation/use_get_url_search';

import {
connectors,
searchURL,
useCaseConfigureResponse,
useConnectorsResponse,
kibanaMockImplementationArgs,
} from './__mock__';
import { connectors, searchURL, useCaseConfigureResponse, useConnectorsResponse } from './__mock__';

jest.mock('../../../common/lib/kibana');
jest.mock('../../containers/configure/use_connectors');
jest.mock('../../containers/configure/use_configure');
jest.mock('../../../common/components/navigation/use_get_url_search');

const useKibanaMock = useKibana as jest.Mock;
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;
const useConnectorsMock = useConnectors as jest.Mock;
const useCaseConfigureMock = useCaseConfigure as jest.Mock;
const useGetUrlSearchMock = useGetUrlSearch as jest.Mock;

describe('ConfigureCases', () => {
beforeEach(() => {
useKibanaMock().services.triggers_actions_ui = ({
actionTypeRegistry: actionTypeRegistryMock.create(),
} as unknown) as TriggersAndActionsUIPublicPluginStart;
});

describe('rendering', () => {
let wrapper: ReactWrapper;
beforeEach(() => {
jest.resetAllMocks();
useCaseConfigureMock.mockImplementation(() => useCaseConfigureResponse);
useConnectorsMock.mockImplementation(() => ({ ...useConnectorsResponse, connectors: [] }));
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);

wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
Expand Down Expand Up @@ -84,8 +86,8 @@ describe('ConfigureCases', () => {

describe('Unhappy path', () => {
let wrapper: ReactWrapper;

beforeEach(() => {
jest.resetAllMocks();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
closureType: 'close-by-user',
Expand All @@ -98,7 +100,6 @@ describe('ConfigureCases', () => {
},
}));
useConnectorsMock.mockImplementation(() => ({ ...useConnectorsResponse, connectors: [] }));
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);
wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
});
Expand All @@ -122,7 +123,6 @@ describe('ConfigureCases', () => {
let wrapper: ReactWrapper;

beforeEach(() => {
jest.resetAllMocks();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
mapping: connectors[0].config.incidentConfiguration.mapping,
Expand All @@ -136,7 +136,6 @@ describe('ConfigureCases', () => {
},
}));
useConnectorsMock.mockImplementation(() => useConnectorsResponse);
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);

wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
Expand Down Expand Up @@ -211,9 +210,6 @@ describe('ConfigureCases', () => {
let wrapper: ReactWrapper;

beforeEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
jest.clearAllMocks();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
mapping: connectors[1].config.incidentConfiguration.mapping,
Expand All @@ -230,7 +226,6 @@ describe('ConfigureCases', () => {
...useConnectorsResponse,
loading: true,
}));
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);
wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
});
Expand Down Expand Up @@ -262,15 +257,13 @@ describe('ConfigureCases', () => {
let wrapper: ReactWrapper;

beforeEach(() => {
jest.resetAllMocks();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
connectorId: 'servicenow-1',
persistLoading: true,
}));

useConnectorsMock.mockImplementation(() => useConnectorsResponse);
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);
wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
});
Expand Down Expand Up @@ -305,15 +298,13 @@ describe('ConfigureCases', () => {
let wrapper: ReactWrapper;

beforeEach(() => {
jest.resetAllMocks();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
loading: true,
}));
useConnectorsMock.mockImplementation(() => ({
...useConnectorsResponse,
}));
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);
wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
});
Expand All @@ -329,10 +320,10 @@ describe('ConfigureCases', () => {

describe('connectors', () => {
let wrapper: ReactWrapper;
const persistCaseConfigure = jest.fn();
let persistCaseConfigure: jest.Mock;

beforeEach(() => {
jest.resetAllMocks();
persistCaseConfigure = jest.fn();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
mapping: connectors[0].config.incidentConfiguration.mapping,
Expand All @@ -347,7 +338,6 @@ describe('ConfigureCases', () => {
persistCaseConfigure,
}));
useConnectorsMock.mockImplementation(() => useConnectorsResponse);
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);

wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
Expand Down Expand Up @@ -396,10 +386,10 @@ describe('ConfigureCases', () => {

describe('closure options', () => {
let wrapper: ReactWrapper;
const persistCaseConfigure = jest.fn();
let persistCaseConfigure: jest.Mock;

beforeEach(() => {
jest.resetAllMocks();
persistCaseConfigure = jest.fn();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
mapping: connectors[0].config.incidentConfiguration.mapping,
Expand All @@ -414,7 +404,6 @@ describe('closure options', () => {
persistCaseConfigure,
}));
useConnectorsMock.mockImplementation(() => useConnectorsResponse);
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);

wrapper = mount(<ConfigureCases userCanCrud />, { wrappingComponent: TestProviders });
Expand All @@ -435,7 +424,6 @@ describe('closure options', () => {

describe('user interactions', () => {
beforeEach(() => {
jest.resetAllMocks();
useCaseConfigureMock.mockImplementation(() => ({
...useCaseConfigureResponse,
mapping: connectors[1].config.incidentConfiguration.mapping,
Expand All @@ -449,7 +437,6 @@ describe('user interactions', () => {
},
}));
useConnectorsMock.mockImplementation(() => useConnectorsResponse);
useKibanaMock.mockImplementation(() => kibanaMockImplementationArgs);
useGetUrlSearchMock.mockImplementation(() => searchURL);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,17 @@ import '../../../common/mock/match_media';
import { TimelineId } from '../../../../common/types/timeline';
import { useAllCasesModal, UseAllCasesModalProps, UseAllCasesModalReturnedValues } from '.';
import { TestProviders } from '../../../common/mock';
import { createStartServicesMock } from '../../../common/mock/kibana_react';

jest.mock('../../../common/lib/kibana');

const useKibanaMock = useKibana as jest.Mock;
const useKibanaMock = useKibana as jest.Mocked<typeof useKibana>;

describe('useAllCasesModal', () => {
const navigateToApp = jest.fn(() => Promise.resolve());
let navigateToApp: jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
const servicesMock = createStartServicesMock();
useKibanaMock.mockImplementation(() => ({
services: {
...servicesMock,
application: {
navigateToApp,
},
},
}));
navigateToApp = jest.fn();
useKibanaMock().services.application.navigateToApp = navigateToApp;
});

it('init', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { TestProviders, mockIndexPattern } from '../../mock';
import { createKibanaCoreStartMock } from '../../mock/kibana_core';
import { FilterManager, SearchBar } from '../../../../../../../src/plugins/data/public';
import { QueryBar, QueryBarComponentProps } from '.';
import { createKibanaContextProviderMock } from '../../mock/kibana_react';

const mockUiSettingsForFilterManager = createKibanaCoreStartMock().uiSettings;

Expand Down Expand Up @@ -187,13 +186,9 @@ describe('QueryBar ', () => {

describe('state', () => {
test('clears draftQuery when filterQueryDraft has been cleared', () => {
const KibanaWithStorageProvider = createKibanaContextProviderMock();

const Proxy = (props: QueryBarComponentProps) => (
<TestProviders>
<KibanaWithStorageProvider services={{ storage: { get: jest.fn() } }}>
<QueryBar {...props} />
</KibanaWithStorageProvider>
<QueryBar {...props} />
</TestProviders>
);

Expand Down Expand Up @@ -231,13 +226,9 @@ describe('QueryBar ', () => {

describe('#onQueryChange', () => {
test(' is the only reference that changed when filterQueryDraft props get updated', () => {
const KibanaWithStorageProvider = createKibanaContextProviderMock();

const Proxy = (props: QueryBarComponentProps) => (
<TestProviders>
<KibanaWithStorageProvider services={{ storage: { get: jest.fn() } }}>
<QueryBar {...props} />
</KibanaWithStorageProvider>
<QueryBar {...props} />
</TestProviders>
);

Expand Down Expand Up @@ -382,24 +373,9 @@ describe('QueryBar ', () => {

describe('SavedQueryManagementComponent state', () => {
test('popover should hidden when "Save current query" button was clicked', () => {
const KibanaWithStorageProvider = createKibanaContextProviderMock();

const Proxy = (props: QueryBarComponentProps) => (
<TestProviders>
<KibanaWithStorageProvider
services={{
data: {
query: {
savedQueries: {
findSavedQueries: jest.fn().mockResolvedValue({ total: 0, queries: [] }),
getAllSavedQueries: jest.fn().mockResolvedValue([]),
},
},
},
}}
>
<QueryBar {...props} />
</KibanaWithStorageProvider>
<QueryBar {...props} />
</TestProviders>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

import { renderHook, act } from '@testing-library/react-hooks';
import { useKibana } from '../../lib/kibana';
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 = createStartServicesMock();
useKibanaMock.mockImplementation(() => ({ services }));
services.storage.store.clear();
useKibana().services.storage.clear();
});

it('should return an empty array when there is no messages', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import { createKibanaCoreStartMock, createKibanaPluginsStartMock } from './kibana_core';
import { StartServices } from '../../types';
import { createSecuritySolutionStorageMock } from './mock_local_storage';
import { uiSettingsServiceMock } from '../../../../../../src/core/public/mocks';

const mockUiSettings: Record<string, unknown> = {
[DEFAULT_TIME_RANGE]: { from: 'now-15m', to: 'now', mode: 'quick' },
Expand Down
Loading

0 comments on commit 0e5dfe3

Please sign in to comment.