From 567a752f18b2ed6beac432c8d04cf064e590cb4a Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 25 Oct 2022 16:04:56 +0200 Subject: [PATCH] Fixing more unit tests --- .../top_nav/discover_topnav.test.tsx | 30 +++++---- .../components/top_nav/discover_topnav.tsx | 1 + .../top_nav/on_save_search.test.tsx | 24 ++++--- .../main/hooks/use_adhoc_data_views.test.ts | 11 +++- .../main/hooks/use_discover_state.test.tsx | 25 +++++--- .../services/discover_app_state_container.ts | 2 +- .../discover_data_state_container.test.ts | 62 ++++++------------- .../main/services/discover_state.test.ts | 2 +- .../application/main/utils/fetch_all.test.ts | 4 +- .../hooks/use_data_grid_columns.test.tsx | 5 +- .../public/search_bar/search_bar.tsx | 1 - 11 files changed, 81 insertions(+), 86 deletions(-) diff --git a/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.test.tsx b/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.test.tsx index c7a1348d42238..3fd67c9d4de11 100644 --- a/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.test.tsx +++ b/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.test.tsx @@ -7,14 +7,15 @@ */ import React from 'react'; -import { shallowWithIntl } from '@kbn/test-jest-helpers'; +import { mountWithIntl } from '@kbn/test-jest-helpers'; import { DiscoverTopNav, DiscoverTopNavProps } from './discover_topnav'; -import { TopNavMenuData } from '@kbn/navigation-plugin/public'; +import { TopNavMenu, TopNavMenuData } from '@kbn/navigation-plugin/public'; import { Query } from '@kbn/es-query'; import { setHeaderActionMenuMounter } from '../../../../kibana_services'; import { discoverServiceMock } from '../../../../__mocks__/services'; import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock'; import { DiscoverMainProvider } from '../../services/discover_state_react'; +import { dataViewMock } from '../../../../__mocks__/data_view'; setHeaderActionMenuMounter(jest.fn()); @@ -27,9 +28,11 @@ jest.mock('@kbn/kibana-react-plugin/public', () => ({ function getProps(savePermissions = true): DiscoverTopNavProps { discoverServiceMock.capabilities.discover!.save = savePermissions; + const stateContainer = getDiscoverStateMock({ isTimeBased: true }); + stateContainer.internalState.transitions.setDataView(dataViewMock); return { - stateContainer: getDiscoverStateMock({ isTimeBased: true }), + stateContainer, navigateTo: jest.fn(), query: {} as Query, savedQuery: '', @@ -45,24 +48,25 @@ function getProps(savePermissions = true): DiscoverTopNavProps { describe('Discover topnav component', () => { test('generated config of TopNavMenu config is correct when discover save permissions are assigned', () => { const props = getProps(true); - const component = shallowWithIntl(, { - context: { - wrappingComponent: DiscoverMainProvider, - wrappingComponentProps: { value: getDiscoverStateMock({}) }, - }, - }); - const topMenuConfig = component.props().config.map((obj: TopNavMenuData) => obj.id); + const component = mountWithIntl( + + + + ); + const topNavMenu = component.find(TopNavMenu); + const topMenuConfig = topNavMenu.props().config?.map((obj: TopNavMenuData) => obj.id); expect(topMenuConfig).toEqual(['options', 'new', 'open', 'share', 'inspect', 'save']); }); test('generated config of TopNavMenu config is correct when no discover save permissions are assigned', () => { const props = getProps(false); - const component = shallowWithIntl( - + const component = mountWithIntl( + ); - const topMenuConfig = component.props().config.map((obj: TopNavMenuData) => obj.id); + const topNavMenu = component.find(TopNavMenu).props(); + const topMenuConfig = topNavMenu.config?.map((obj: TopNavMenuData) => obj.id); expect(topMenuConfig).toEqual(['options', 'new', 'open', 'share', 'inspect']); }); }); diff --git a/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.tsx b/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.tsx index f7a91c77c2b9c..19062448a5409 100644 --- a/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.tsx +++ b/src/plugins/discover/public/application/main/components/top_nav/discover_topnav.tsx @@ -236,6 +236,7 @@ export const DiscoverTopNav = ({ textBasedLanguageModeErrors ? [textBasedLanguageModeErrors] : undefined } onTextBasedSavedAndExit={onTextBasedSavedAndExit} + data-test-subj="dsc_topNav" /> ); }; diff --git a/src/plugins/discover/public/application/main/components/top_nav/on_save_search.test.tsx b/src/plugins/discover/public/application/main/components/top_nav/on_save_search.test.tsx index 925672f341067..aa3c752bca33b 100644 --- a/src/plugins/discover/public/application/main/components/top_nav/on_save_search.test.tsx +++ b/src/plugins/discover/public/application/main/components/top_nav/on_save_search.test.tsx @@ -18,7 +18,6 @@ import { DiscoverServices } from '../../../../build_services'; import { i18nServiceMock } from '@kbn/core/public/mocks'; import { ReactElement } from 'react'; import { discoverServiceMock } from '../../../../__mocks__/services'; -import * as persistSavedSearchUtils from '../../utils/persist_saved_search'; import { SavedSearch } from '@kbn/saved-search-plugin/public'; import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock'; @@ -70,6 +69,7 @@ describe('onSaveSearch', () => { const serviceMock = discoverServiceMock; const stateMock = getDiscoverStateMock({ isTimeBased: true }); stateMock.setAppState({ rowsPerPage: 250 }); + stateMock.internalState.transitions.setDataView(dataViewMock); let saveModal: ReactElement | undefined; jest.spyOn(savedObjectsPlugin, 'showSaveModal').mockImplementationOnce((modal) => { saveModal = modal; @@ -78,6 +78,7 @@ describe('onSaveSearch', () => { ...savedSearchMock, tags: ['tag1', 'tag2'], }; + await onSaveSearch({ dataView: dataViewMock, navigateTo: jest.fn(), @@ -86,13 +87,12 @@ describe('onSaveSearch', () => { stateContainer: stateMock, updateAdHocDataViewId: jest.fn(), }); + expect(savedSearch.tags).toEqual(['tag1', 'tag2']); - jest - .spyOn(persistSavedSearchUtils, 'persistSavedSearch') - .mockImplementationOnce((newSavedSearch, _) => { - savedSearch = newSavedSearch; - return Promise.resolve(newSavedSearch.id); - }); + stateMock.savedSearchState.persist = jest.fn((newSavedSearch: SavedSearch) => { + savedSearch = newSavedSearch; + return Promise.resolve({ id: newSavedSearch.id }); + }); saveModal?.props.onSave({ newTitle: savedSearch.title, newCopyOnSave: false, @@ -128,12 +128,10 @@ describe('onSaveSearch', () => { updateAdHocDataViewId: jest.fn(), }); expect(savedSearch.tags).toEqual(['tag1', 'tag2']); - jest - .spyOn(persistSavedSearchUtils, 'persistSavedSearch') - .mockImplementationOnce((newSavedSearch, _) => { - savedSearch = newSavedSearch; - return Promise.resolve(newSavedSearch.id); - }); + stateMock.savedSearchState.persist = jest.fn((newSavedSearch: SavedSearch) => { + savedSearch = newSavedSearch; + return Promise.resolve({ id: newSavedSearch.id }); + }); saveModal?.props.onSave({ newTitle: savedSearch.title, newCopyOnSave: false, diff --git a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.test.ts b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.test.ts index 396014ec29479..2a385c9904d9c 100644 --- a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.test.ts +++ b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.test.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import { createSearchSourceMock } from '@kbn/data-plugin/public/mocks'; import type { DataView } from '@kbn/data-views-plugin/public'; import { act, renderHook } from '@testing-library/react-hooks'; import { @@ -67,12 +68,20 @@ const mockDataView = { toSpec: () => ({}), } as DataView; +const savedSearchMock = { + id: 'some-id', + searchSource: createSearchSourceMock({ index: mockDataView }), +}; + describe('useAdHocDataViews', () => { it('should save data view with new id and update saved search', async () => { + const stateContainer = getDiscoverStateMock({ isTimeBased: true }); + stateContainer.savedSearchState.set(savedSearchMock); + const hook = renderHook((d: DataView) => useAdHocDataViews({ dataView: mockDataView, - stateContainer: getDiscoverStateMock({ isTimeBased: true }), + stateContainer, services: discoverServiceMock, }) ); diff --git a/src/plugins/discover/public/application/main/hooks/use_discover_state.test.tsx b/src/plugins/discover/public/application/main/hooks/use_discover_state.test.tsx index 3dbee49ce5f84..4a733da299697 100644 --- a/src/plugins/discover/public/application/main/hooks/use_discover_state.test.tsx +++ b/src/plugins/discover/public/application/main/hooks/use_discover_state.test.tsx @@ -12,19 +12,30 @@ import { useDiscoverState } from './use_discover_state'; import { setUrlTracker } from '../../../kibana_services'; import { urlTrackerMock } from '../../../__mocks__/url_tracker.mock'; import { getDiscoverStateMock } from '../../../__mocks__/discover_state.mock'; +import React, { ReactElement } from 'react'; +import { DiscoverMainProvider } from '../services/discover_state_react'; +import { dataViewMock } from '../../../__mocks__/data_view'; setUrlTracker(urlTrackerMock); describe('test useDiscoverState', () => { test('return is valid', async () => { const stateContainer = getDiscoverStateMock({ isTimeBased: true }); + stateContainer.internalState.transitions.setDataView(dataViewMock); - const { result } = renderHook(() => { - return useDiscoverState({ - services: discoverServiceMock, - setExpandedDoc: jest.fn(), - stateContainer, - }); - }); + const { result } = renderHook( + () => { + return useDiscoverState({ + services: discoverServiceMock, + setExpandedDoc: jest.fn(), + stateContainer, + }); + }, + { + wrapper: ({ children }: { children: ReactElement }) => ( + {children} + ), + } + ); expect(result.current.adHocDataViewList).toBeInstanceOf(Object); }); }); diff --git a/src/plugins/discover/public/application/main/services/discover_app_state_container.ts b/src/plugins/discover/public/application/main/services/discover_app_state_container.ts index ac170a99312cf..3b1bf5102506a 100644 --- a/src/plugins/discover/public/application/main/services/discover_app_state_container.ts +++ b/src/plugins/discover/public/application/main/services/discover_app_state_container.ts @@ -234,7 +234,7 @@ function getInitialState( services, }); return handleSourceColumnState( - savedSearch.id + savedSearch.id && (!appStateFromUrl || !Object.keys(appStateFromUrl).length) ? { ...defaultAppState } : { ...defaultAppState, diff --git a/src/plugins/discover/public/application/main/services/discover_data_state_container.test.ts b/src/plugins/discover/public/application/main/services/discover_data_state_container.test.ts index 42654900765c3..848e31edebb85 100644 --- a/src/plugins/discover/public/application/main/services/discover_data_state_container.test.ts +++ b/src/plugins/discover/public/application/main/services/discover_data_state_container.test.ts @@ -7,31 +7,21 @@ */ import { Subject } from 'rxjs'; import { waitFor } from '@testing-library/react'; -import { createSearchSessionMock } from '../../../__mocks__/search_session'; import { discoverServiceMock } from '../../../__mocks__/services'; -import { savedSearchMock, savedSearchMockWithSQL } from '../../../__mocks__/saved_search'; +import { savedSearchMockWithSQL } from '../../../__mocks__/saved_search'; import { getDiscoverStateContainer } from './discover_state'; import { FetchStatus } from '../../types'; import { setUrlTracker } from '../../../kibana_services'; import { urlTrackerMock } from '../../../__mocks__/url_tracker.mock'; -import { getDataStateContainer, RecordRawType } from './discover_data_state_container'; +import { RecordRawType } from './discover_data_state_container'; +import { createBrowserHistory } from 'history'; +import { getDiscoverStateMock } from '../../../__mocks__/discover_state.mock'; setUrlTracker(urlTrackerMock); describe('test getDataStateContainer', () => { test('return is valid', async () => { - const { history, searchSessionManager } = createSearchSessionMock(); - const stateContainer = getDiscoverStateContainer({ - savedSearch: savedSearchMock, - services: discoverServiceMock, - history, - }); - - const dataState = getDataStateContainer({ - getSavedSearch: () => savedSearchMock, - searchSessionManager, - services: discoverServiceMock, - getAppState: () => stateContainer.appState.getState(), - }); + const stateContainer = getDiscoverStateMock({ isTimeBased: true }); + const dataState = stateContainer.dataState; expect(dataState.refetch$).toBeInstanceOf(Subject); expect(dataState.data$.main$.getValue().fetchStatus).toBe(FetchStatus.LOADING); @@ -40,23 +30,13 @@ describe('test getDataStateContainer', () => { expect(dataState.data$.charts$.getValue().fetchStatus).toBe(FetchStatus.LOADING); }); test('refetch$ triggers a search', async () => { - const { history, searchSessionManager } = createSearchSessionMock(); - const stateContainer = getDiscoverStateContainer({ - savedSearch: savedSearchMock, - services: discoverServiceMock, - history, - }); + const stateContainer = getDiscoverStateMock({ isTimeBased: true }); discoverServiceMock.data.query.timefilter.timefilter.getTime = jest.fn(() => { return { from: '2021-05-01T20:00:00Z', to: '2021-05-02T20:00:00Z' }; }); + const dataState = stateContainer.dataState; - const dataState = getDataStateContainer({ - getSavedSearch: () => savedSearchMock, - searchSessionManager, - services: discoverServiceMock, - getAppState: () => stateContainer.appState.getState(), - }); const unsubscribe = dataState.subscribe(); dataState.refetch$.next(undefined); @@ -70,40 +50,34 @@ describe('test getDataStateContainer', () => { }); test('reset sets back to initial state', async () => { - const { history, searchSessionManager } = createSearchSessionMock(); - const stateContainer = getDiscoverStateContainer({ - savedSearch: savedSearchMock, - services: discoverServiceMock, - history, - }); + const stateContainer = getDiscoverStateMock({ isTimeBased: true }); discoverServiceMock.data.query.timefilter.timefilter.getTime = jest.fn(() => { return { from: '2021-05-01T20:00:00Z', to: '2021-05-02T20:00:00Z' }; }); - const dataState = getDataStateContainer({ - getSavedSearch: () => savedSearchMock, - searchSessionManager, - services: discoverServiceMock, - getAppState: () => stateContainer.appState.getState(), - }); + const dataState = stateContainer.dataState; const unsubscribe = dataState.subscribe(); + await waitFor(() => { + expect(dataState.data$.main$.value.fetchStatus).toBe(FetchStatus.LOADING); + }); + dataState.refetch$.next(undefined); await waitFor(() => { expect(dataState.data$.main$.value.fetchStatus).toBe(FetchStatus.COMPLETE); }); - dataState.refetch$.next('reset'); - + dataState.reset(); await waitFor(() => { - expect(dataState.data$.main$.getValue().fetchStatus).toBe(FetchStatus.LOADING); + expect(dataState.data$.main$.value.fetchStatus).toBe(FetchStatus.LOADING); }); + unsubscribe(); }); test('useSavedSearch returns plain record raw type', async () => { - const { history } = createSearchSessionMock(); + const history = createBrowserHistory(); const stateContainer = getDiscoverStateContainer({ savedSearch: savedSearchMockWithSQL, services: discoverServiceMock, diff --git a/src/plugins/discover/public/application/main/services/discover_state.test.ts b/src/plugins/discover/public/application/main/services/discover_state.test.ts index d5bfa1d92617d..cf185ec2fdf6e 100644 --- a/src/plugins/discover/public/application/main/services/discover_state.test.ts +++ b/src/plugins/discover/public/application/main/services/discover_state.test.ts @@ -93,7 +93,7 @@ describe('Test discover initial state sort handling', () => { services: discoverServiceMock, history, }); - await state.setAppState({}, true); + // await state.setAppState({}, true); const stopSync = state.appState.syncState().stop; expect(state.appState.getState().sort).toEqual([['order_date', 'desc']]); stopSync(); diff --git a/src/plugins/discover/public/application/main/utils/fetch_all.test.ts b/src/plugins/discover/public/application/main/utils/fetch_all.test.ts index 98048d6d0d410..86fafce7a770e 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_all.test.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_all.test.ts @@ -50,7 +50,7 @@ const mockFetchTotalHits = fetchTotalHits as unknown as jest.MockedFunction; const mockFetchSQL = fetchSql as unknown as jest.MockedFunction; -function subjectCollector(subject: Subject): () => Promise { +export function subjectCollector(subject: Subject): () => Promise { const promise = firstValueFrom( subject.pipe(reduce((history, value) => history.concat([value]), [] as T[])) ); @@ -230,7 +230,9 @@ describe('test fetchAll', () => { ]; const documents = hits.map((hit) => buildDataTableRecord(hit, dataViewMock)); mockFetchSQL.mockResolvedValue(documents); + const query = { sql: 'SELECT * from foo' }; + savedSearchMock.searchSource.setField('query', query); deps = { abortController: new AbortController(), data: discoverServiceMock.data, diff --git a/src/plugins/discover/public/hooks/use_data_grid_columns.test.tsx b/src/plugins/discover/public/hooks/use_data_grid_columns.test.tsx index f0ebe91593e48..80e861ea99c42 100644 --- a/src/plugins/discover/public/hooks/use_data_grid_columns.test.tsx +++ b/src/plugins/discover/public/hooks/use_data_grid_columns.test.tsx @@ -11,7 +11,6 @@ import { useColumns } from './use_data_grid_columns'; import { dataViewMock } from '../__mocks__/data_view'; import { configMock } from '../__mocks__/config'; import { dataViewsMock } from '../__mocks__/data_views'; -import { AppState } from '../application/context/services/context_state'; import { Capabilities } from '@kbn/core/types'; describe('useColumns', () => { @@ -21,9 +20,7 @@ describe('useColumns', () => { dataView: dataViewMock, dataViews: dataViewsMock, setAppState: () => {}, - state: { - columns: ['Time', 'message'], - } as AppState, + columns: ['Time', 'message'], useNewFieldsApi: false, }; diff --git a/src/plugins/unified_search/public/search_bar/search_bar.tsx b/src/plugins/unified_search/public/search_bar/search_bar.tsx index 5e4c673b85eee..ebaa3a317c270 100644 --- a/src/plugins/unified_search/public/search_bar/search_bar.tsx +++ b/src/plugins/unified_search/public/search_bar/search_bar.tsx @@ -310,7 +310,6 @@ class SearchBarUI extends C }; public onQueryBarChange = (queryAndDateRange: { dateRange: TimeRange; query?: QT | Query }) => { - console.log('{ onQueryBarChange }', this.props.onQueryChange); this.setState({ query: queryAndDateRange.query, dateRangeFrom: queryAndDateRange.dateRange.from,