Skip to content

Commit

Permalink
[SecuritySolution] Remove usage of the kibana observable (#175962)
Browse files Browse the repository at this point in the history
## Summary

After merging #175678, we have no
more need for the kibana observable. It can now be replaced by an
instance of kibana (aka `CoreStart`).

This change mostly impacts tests that use `createStore` in
`security_solution`.

In a second step, this PR cleans up test-related imports between
`security_solution` and the `timelines` plugin
(523080c).

This work is part of #175427.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
janmonschke authored Feb 2, 2024
1 parent 1985dd0 commit 5c00ddf
Show file tree
Hide file tree
Showing 80 changed files with 681 additions and 1,646 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
*/

import { createFilterManagerMock } from '@kbn/data-plugin/public/query/filter_manager/filter_manager.mock';
import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
} from '../../../common/mock';
import { createStore } from '../../../common/store';
import { createMockStore, mockGlobalState } from '../../../common/mock';
import { createFilterInCellActionFactory } from './filter_in';
import type { SecurityCellActionExecutionContext } from '../../types';
import { createStartServicesMock } from '../../../common/lib/kibana/kibana_react.mock';
Expand Down Expand Up @@ -45,8 +39,7 @@ jest.mock('@kbn/ui-actions-plugin/public', () => ({
addFilterOut: () => {},
}));

const { storage } = createSecuritySolutionStorageMock();
const mockStore = createStore(mockState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore(mockState);

describe('createFilterInCellActionFactory', () => {
const createFilterInCellAction = createFilterInCellActionFactory({ store: mockStore, services });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
*/

import { createFilterManagerMock } from '@kbn/data-plugin/public/query/filter_manager/filter_manager.mock';
import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
} from '../../../common/mock';
import { createStore } from '../../../common/store';
import { createMockStore, mockGlobalState } from '../../../common/mock';
import { createFilterOutCellActionFactory } from './filter_out';
import type { SecurityCellActionExecutionContext } from '../../types';
import { createStartServicesMock } from '../../../common/lib/kibana/kibana_react.mock';
Expand All @@ -39,8 +33,7 @@ const mockState = {
},
};

const { storage } = createSecuritySolutionStorageMock();
const mockStore = createStore(mockState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore(mockState);

describe('createFilterOutCellActionFactory', () => {
const filterOutActionFactory = createFilterOutCellActionFactory({ store: mockStore, services });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* 2.0.
*/

import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
} from '../../../common/mock';
import { createStore } from '../../../common/store';
import { createMockStore } from '../../../common/mock';
import { createFilterInDiscoverCellActionFactory } from './filter_in';
import type { SecurityCellActionExecutionContext } from '../../types';
import { createStartServicesMock } from '../../../common/lib/kibana/kibana_react.mock';
Expand All @@ -30,8 +24,7 @@ jest.mock('@kbn/ui-actions-plugin/public', () => ({
addFilterOut: () => {},
}));

const { storage } = createSecuritySolutionStorageMock();
const mockStore = createStore(mockGlobalState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore();

describe('createFilterInDiscoverCellActionFactory', () => {
const createFilterInCellAction = createFilterInDiscoverCellActionFactory({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* 2.0.
*/

import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
} from '../../../common/mock';
import { createStore } from '../../../common/store';
import { createMockStore } from '../../../common/mock';
import { createFilterOutDiscoverCellActionFactory } from './filter_out';
import type { SecurityCellActionExecutionContext } from '../../types';
import { createStartServicesMock } from '../../../common/lib/kibana/kibana_react.mock';
Expand All @@ -30,8 +24,7 @@ jest.mock('@kbn/ui-actions-plugin/public', () => ({
addFilterOut: () => {},
}));

const { storage } = createSecuritySolutionStorageMock();
const mockStore = createStore(mockGlobalState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore();

describe('createFilterOutDiscoverCellActionFactory', () => {
const createFilterOutCellAction = createFilterOutDiscoverCellActionFactory({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ import {
THREAT_INTELLIGENCE_PATH,
} from '../../../../common/constants';
import {
createSecuritySolutionStorageMock,
createMockStore,
mockGlobalState,
mockIndexPattern,
SUB_PLUGINS_REDUCER,
TestProviders,
} from '../../../common/mock';
import { TimelineId } from '../../../../common/types/timeline';
import { createStore } from '../../../common/store';
import { kibanaObservable } from '@kbn/timelines-plugin/public/mock';
import { sourcererPaths } from '../../../common/containers/sourcerer';

jest.mock('react-router-dom', () => {
Expand Down Expand Up @@ -61,8 +58,7 @@ describe('global header', () => {
},
},
};
const { storage } = createSecuritySolutionStorageMock();
const store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const store = createMockStore(state);

beforeEach(() => {
useVariationMock.mockReset();
Expand Down Expand Up @@ -159,7 +155,7 @@ describe('global header', () => {
},
},
};
const mockStore = createStore(mockstate, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore(mockstate);

(useLocation as jest.Mock).mockReturnValue({ pathname: sourcererPaths[2] });

Expand Down
27 changes: 6 additions & 21 deletions x-pack/plugins/security_solution/public/app/home/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { HomePage } from '.';
import type { SavedQuery } from '@kbn/data-plugin/public';
import { FilterManager } from '@kbn/data-plugin/public';

import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
TestProviders,
} from '../../common/mock';
import { createMockStore, mockGlobalState, TestProviders } from '../../common/mock';
import { inputsActions } from '../../common/store/inputs';
import {
setSearchBarFilter,
Expand All @@ -26,7 +20,6 @@ import {
} from '../../common/store/inputs/actions';
import { coreMock } from '@kbn/core/public/mocks';
import type { Filter } from '@kbn/es-query';
import { createStore } from '../../common/store';
import type { TimeRange, UrlInputsModel } from '../../common/store/inputs/model';
import { SecurityPageName } from '../types';
import type { TimelineUrl } from '../../timelines/store/model';
Expand Down Expand Up @@ -346,7 +339,6 @@ describe('HomePage', () => {
const state = null;
mockUseInitializeUrlParam(URL_PARAM_KEY.filters, state);
const spySetAppFilters = jest.spyOn(mockedFilterManager, 'setAppFilters');
const { storage } = createSecuritySolutionStorageMock();

const mockstate = {
...mockGlobalState,
Expand All @@ -359,7 +351,7 @@ describe('HomePage', () => {
},
};

const mockStore = createStore(mockstate, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore(mockstate);

render(
<TestProviders store={mockStore}>
Expand Down Expand Up @@ -512,8 +504,7 @@ describe('HomePage', () => {
},
};

const { storage } = createSecuritySolutionStorageMock();
const mockStore = createStore(mockstate, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore(mockstate);

const TestComponent = () => (
<TestProviders store={mockStore}>
Expand Down Expand Up @@ -569,8 +560,7 @@ describe('HomePage', () => {
},
};

const { storage } = createSecuritySolutionStorageMock();
const mockStore = createStore(mockstate, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const mockStore = createMockStore(mockstate);

const TestComponent = () => (
<TestProviders store={mockStore}>
Expand Down Expand Up @@ -629,16 +619,13 @@ describe('HomePage', () => {
});

it('it keeps timeline visibility and selected tab state in URL', async () => {
const { storage } = createSecuritySolutionStorageMock();
const store = createStore(mockGlobalState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);

mockUseInitializeUrlParam(URL_PARAM_KEY.timeline, {
id: 'testSavedTimelineId',
isOpen: false,
});

const TestComponent = () => (
<TestProviders store={store}>
<TestProviders>
<HomePage>
<span />
</HomePage>
Expand All @@ -660,8 +647,6 @@ describe('HomePage', () => {
});

it('it updates URL when timeline store changes', async () => {
const { storage } = createSecuritySolutionStorageMock();
const store = createStore(mockGlobalState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const savedObjectId = 'testTimelineId';

mockUseInitializeUrlParam(URL_PARAM_KEY.timeline, {
Expand All @@ -670,7 +655,7 @@ describe('HomePage', () => {
});

const TestComponent = () => (
<TestProviders store={store}>
<TestProviders>
<HomePage>
<span />
</HomePage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
import { renderHook } from '@testing-library/react-hooks';
import React from 'react';

import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
TestProviders,
} from '../../../../mock';
import type { State } from '../../../../store';
import { createStore } from '../../../../store';
import { TestProviders } from '../../../../mock';
import * as i18n from './translations';
import { useChartSettingsPopoverConfiguration } from '.';

Expand All @@ -30,11 +22,8 @@ describe('useChartSettingsPopoverConfiguration', () => {
const onResetStackByFields = jest.fn();
const queryId = 'abcd';

const state: State = mockGlobalState;
const { storage } = createSecuritySolutionStorageMock();
const store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const wrapper = ({ children }: { children: React.ReactNode }) => (
<TestProviders store={store}>{children}</TestProviders>
<TestProviders>{children}</TestProviders>
);

beforeEach(() => jest.clearAllMocks());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ import { discoverPluginMock } from '@kbn/discover-plugin/public/mocks';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import type { SavedSearch } from '@kbn/saved-search-plugin/common';
import { renderHook } from '@testing-library/react-hooks';
import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
TestProviders,
} from '../../mock';
import { createMockStore, mockGlobalState, TestProviders } from '../../mock';
import { useDiscoverInTimelineActions } from './use_discover_in_timeline_actions';
import type { Filter } from '@kbn/es-query';
import { createStartServicesMock } from '../../lib/kibana/kibana_react.mock';
import { useKibana } from '../../lib/kibana';
import type { State } from '../../store';
import { createStore } from '../../store';
import { TimelineId } from '../../../../common/types';
import * as timelineActions from '../../../timelines/store/actions';
import type { ComponentType, FC, PropsWithChildren } from 'react';
Expand Down Expand Up @@ -67,10 +60,8 @@ jest.mock('./use_discover_in_timeline_actions', () => {
return actual;
});

const { storage } = createSecuritySolutionStorageMock();

const getTestProviderWithCustomState = (state: State = mockState) => {
const store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const store = createMockStore(state);

const MockTestProvider: FC<PropsWithChildren<{}>> = ({ children }) => (
<TestProviders store={store}> {children}</TestProviders>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,15 @@ import { shallow } from 'enzyme';
import React from 'react';
import { Provider } from 'react-redux';

import {
mockGlobalState,
SUB_PLUGINS_REDUCER,
kibanaObservable,
createSecuritySolutionStorageMock,
} from '../../mock';
import { createStore } from '../../store/store';
import { createMockStore } from '../../mock';

import { ErrorToastDispatcher } from '.';
import type { State } from '../../store/types';

describe('Error Toast Dispatcher', () => {
const state: State = mockGlobalState;
const { storage } = createSecuritySolutionStorageMock();
let store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage);

beforeEach(() => {
store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
});

describe('rendering', () => {
test('it renders', () => {
const wrapper = shallow(
<Provider store={store}>
<Provider store={createMockStore()}>
<ErrorToastDispatcher toastLifeTimeMs={9999999999} />
</Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@
import React from 'react';
import { act, render } from '@testing-library/react';
import { OverviewCardWithActions } from './overview_card';
import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
TestProviders,
} from '../../../mock';
import { createMockStore, mockGlobalState, TestProviders } from '../../../mock';
import { SeverityBadge } from '../../severity_badge';
import type { State } from '../../../store';
import { createStore } from '../../../store';
import { TimelineId } from '../../../../../common/types';
import { createAction } from '@kbn/ui-actions-plugin/public';

Expand All @@ -34,8 +27,7 @@ const state: State = {
},
};

const { storage } = createSecuritySolutionStorageMock();
const store = createStore(state, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
const store = createMockStore(state);

const props = {
title: 'Severity',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@ import type { ComponentProps, FC } from 'react';
import React from 'react';
import { act, render, screen, fireEvent, waitFor } from '@testing-library/react';
import { DEFAULT_DETECTION_PAGE_FILTERS } from '../../../../common/constants';
import {
createSecuritySolutionStorageMock,
kibanaObservable,
mockGlobalState,
SUB_PLUGINS_REDUCER,
TestProviders,
} from '../../mock';
import { TestProviders } from '../../mock';
import type {
ControlGroupOutput,
ControlGroupInput,
ControlGroupContainer,
} from '@kbn/controls-plugin/public';
import { OPTIONS_LIST_CONTROL } from '@kbn/controls-plugin/common';
import { initialInputData, sampleOutputData } from './mocks/data';
import { createStore } from '../../store';
import { useGetInitialUrlParamValue } from '../../utils/global_query_string/helpers';
import { COMMON_OPTIONS_LIST_CONTROL_INPUTS, TEST_IDS } from './constants';
import {
Expand Down Expand Up @@ -88,19 +81,12 @@ jest.mock('@kbn/controls-plugin/public/control_group/external_api/control_group_
const onFilterChangeMock = jest.fn();
const onInitMock = jest.fn();

const state = mockGlobalState;
const { storage } = createSecuritySolutionStorageMock();

const getStoreWithCustomState = (newState: typeof state = state) => {
return createStore(newState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
};

const TestComponent: FC<
ComponentProps<typeof TestProviders> & {
filterGroupProps?: Partial<ComponentProps<typeof FilterGroup>>;
}
> = (props) => (
<TestProviders store={getStoreWithCustomState()} {...props}>
<TestProviders {...props}>
<FilterGroup
initialControls={DEFAULT_DETECTION_PAGE_FILTERS}
dataViewId="security-solution-default"
Expand Down
Loading

0 comments on commit 5c00ddf

Please sign in to comment.