Skip to content

Commit

Permalink
Mock UI Settings values in our TestProvider
Browse files Browse the repository at this point in the history
When using our TestProvider components, we were previously relying on
platform's UISettings mocks instead of our own, more comprehensive ones.
This worked for the most part, and when we needed real settings we would
mock the UI Settings client manually.

When we removed some app code that defaulted UI Settings values when the
client did not return a value, tests that used TestProviders but also
relied on those defaults broke. This adds that behavior back,
and obviates the need for manual calls to jest.mock except when we're a)
not using TestProviders but b) overriding the platform mocks.

Also removes some of those unneeded uses.
  • Loading branch information
rylnd committed Jan 7, 2020
1 parent ae70785 commit 1a8a8eb
Show file tree
Hide file tree
Showing 15 changed files with 7 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { mockBrowserFields } from '../../containers/source/mock';
import { defaultHeaders } from '../../mock/header';
import { useMountAppended } from '../../utils/use_mount_appended';

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

describe('EventFieldsBrowser', () => {
const mount = useMountAppended();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { mockBrowserFields } from '../../containers/source/mock';
import { eventsDefaultModel } from './default_model';
import { useMountAppended } from '../../utils/use_mount_appended';

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

const mockUseFetchIndexPatterns: jest.Mock = useFetchIndexPatterns as jest.Mock;
jest.mock('../../containers/detection_engine/rules/fetch_index_patterns');
mockUseFetchIndexPatterns.mockImplementation(() => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { useFetchIndexPatterns } from '../../containers/detection_engine/rules/f
import { mockBrowserFields } from '../../containers/source/mock';
import { eventsDefaultModel } from './default_model';

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

const mockUseFetchIndexPatterns: jest.Mock = useFetchIndexPatterns as jest.Mock;
jest.mock('../../containers/detection_engine/rules/fetch_index_patterns');
mockUseFetchIndexPatterns.mockImplementation(() => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const testFlyoutHeight = 980;
const testWidth = 640;
const usersViewing = ['elastic'];

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

describe('Pane', () => {
test('renders correctly against snapshot', () => {
const EmptyComponent = shallow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { TestProviders } from '../../../../mock';

import { FirstLastSeenHost, FirstLastSeenHostType } from '.';

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

describe('FirstLastSeen Component', () => {
const firstSeen = 'Apr 8, 2019 @ 16:09:40.692';
const lastSeen = 'Apr 8, 2019 @ 18:35:45.064';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { FilterManager, SearchBar } from '../../../../../../../src/plugins/data/
import { QueryBar, QueryBarComponentProps } from '.';
import { createKibanaContextProviderMock } from '../../mock/kibana_react';

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

const mockUiSettingsForFilterManager = createKibanaCoreStartMock().uiSettings;

describe('QueryBar ', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { useMountAppended } from '../../../../utils/use_mount_appended';
import { plainColumnRenderer } from './plain_column_renderer';
import { getValues, deleteItemIdx, findItem } from './helpers';

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

describe('plain_column_renderer', () => {
const mount = useMountAppended();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ import {
} from './generic_row_renderer';
import * as i18n from './translations';

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

describe('GenericRowRenderer', () => {
const mount = useMountAppended();

Expand Down
8 changes: 7 additions & 1 deletion x-pack/legacy/plugins/siem/public/mock/kibana_react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DEFAULT_TO,
DEFAULT_INTERVAL_PAUSE,
DEFAULT_INTERVAL_VALUE,
DEFAULT_BYTES_FORMAT,
} from '../../common/constants';
import { defaultIndexPattern } from '../../default_index_pattern';
import { createKibanaCoreStartMock, createKibanaPluginsStartMock } from './kibana_core';
Expand All @@ -37,6 +38,7 @@ export const mockUiSettings: Record<string, any> = {
value: DEFAULT_INTERVAL_VALUE,
},
[DEFAULT_INDEX_KEY]: defaultIndexPattern,
[DEFAULT_BYTES_FORMAT]: '0,0.[0]b',
[DEFAULT_DATE_FORMAT_TZ]: 'UTC',
[DEFAULT_DATE_FORMAT]: 'MMM D, YYYY @ HH:mm:ss.SSS',
[DEFAULT_DARK_MODE]: false,
Expand Down Expand Up @@ -83,11 +85,15 @@ export const createWithKibanaMock = () => {

export const createKibanaContextProviderMock = () => {
const kibana = createUseKibanaMock()();
const uiSettings = {
...kibana.services.uiSettings,
get: createUseUiSettingMock(),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ({ services, ...rest }: any) =>
React.createElement(KibanaContextProvider, {
...rest,
services: { ...kibana.services, ...services },
services: { ...kibana.services, uiSettings, ...services },
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import toJson from 'enzyme-to-json';
import * as React from 'react';

import { RuleSwitchComponent } from './index';
jest.mock('../../../../../lib/kibana');

describe('RuleSwitch', () => {
test('renders correctly against snapshot', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { hostDetailsPagePath } from '../types';
import { type } from './utils';
import { useMountAppended } from '../../../utils/use_mount_appended';

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

jest.mock('../../../containers/source', () => ({
indicesExistOrDataTemporarilyUnavailable: () => true,
WithSource: ({
Expand Down
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/siem/public/pages/hosts/hosts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { SiemNavigation } from '../../components/navigation';
import { HostsComponentProps } from './types';
import { Hosts } from './hosts';

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

// Test will fail because we will to need to mock some core services to make the test work
// For now let's forget about SiemSearchBar and QueryBar
jest.mock('../../components/search_bar', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const pop: Action = 'POP';

type GlobalWithFetch = NodeJS.Global & { fetch: jest.Mock };

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

// Test will fail because we will to need to mock some core services to make the test work
// For now let's forget about SiemSearchBar and QueryBar
jest.mock('../../../components/search_bar', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { mocksSource } from '../../containers/source/mock';
import { TestProviders } from '../../mock';
import { Network } from './network';

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

// Test will fail because we will to need to mock some core services to make the test work
// For now let's forget about SiemSearchBar and QueryBar
jest.mock('../../components/search_bar', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { TestProviders } from '../../mock';
import { mocksSource } from '../../containers/source/mock';
import { Overview } from './index';

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

let localSource: Array<{
request: {};
result: {
Expand Down

0 comments on commit 1a8a8eb

Please sign in to comment.