Skip to content

Commit

Permalink
Fix types and Jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Nov 23, 2023
1 parent 4e9259e commit b88e211
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/plugins/discover/public/__mocks__/discover_state.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export function getDiscoverStateMock({
const container = getDiscoverStateContainer({
services: discoverServiceMock,
history,
customizationContext: {
displayMode: 'standalone',
showLogExplorerTabs: false,
},
});
container.savedSearchState.set(
savedSearch ? savedSearch : isTimeBased ? savedSearchMockWithTimeField : savedSearchMock
Expand Down
42 changes: 36 additions & 6 deletions src/plugins/discover/public/application/discover_router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import { Redirect, RouteProps } from 'react-router-dom';
import { Route } from '@kbn/shared-ux-router';
import { createSearchSessionMock } from '../__mocks__/search_session';
import { discoverServiceMock as mockDiscoverServices } from '../__mocks__/services';
import { CustomDiscoverRoutes, DiscoverRouter, DiscoverRoutes } from './discover_router';
import {
CustomDiscoverRoutes,
DiscoverRouter,
DiscoverRoutes,
DiscoverRoutesProps,
} from './discover_router';
import { DiscoverMainRoute } from './main';
import { SingleDocRoute } from './doc';
import { ContextAppRoute } from './context';
import { createProfileRegistry } from '../customizations/profile_registry';
import { addProfile } from '../../common/customizations';
import { NotFoundRoute } from './not_found';
import type { DiscoverCustomizationContext } from './types';

let mockProfile: string | undefined;

Expand All @@ -43,9 +49,15 @@ const gatherRoutes = (wrapper: ShallowWrapper) => {
});
};

const props = {
const customizationContext: DiscoverCustomizationContext = {
displayMode: 'standalone',
showLogExplorerTabs: false,
};

const props: DiscoverRoutesProps = {
isDev: false,
customizationCallbacks: [],
customizationContext,
};

describe('DiscoverRoutes', () => {
Expand Down Expand Up @@ -147,12 +159,17 @@ describe('CustomDiscoverRoutes', () => {
it('should show DiscoverRoutes for a valid profile', () => {
mockProfile = 'test';
const component = shallow(
<CustomDiscoverRoutes profileRegistry={profileRegistry} isDev={props.isDev} />
<CustomDiscoverRoutes
profileRegistry={profileRegistry}
customizationContext={customizationContext}
isDev={props.isDev}
/>
);
expect(component.find(DiscoverRoutes).getElement()).toMatchObject(
<DiscoverRoutes
prefix={addProfile('', mockProfile)}
customizationCallbacks={callbacks}
customizationContext={customizationContext}
isDev={props.isDev}
/>
);
Expand All @@ -161,7 +178,11 @@ describe('CustomDiscoverRoutes', () => {
it('should show NotFoundRoute for an invalid profile', () => {
mockProfile = 'invalid';
const component = shallow(
<CustomDiscoverRoutes profileRegistry={profileRegistry} isDev={props.isDev} />
<CustomDiscoverRoutes
profileRegistry={profileRegistry}
customizationContext={customizationContext}
isDev={props.isDev}
/>
);
expect(component.find(NotFoundRoute).getElement()).toMatchObject(<NotFoundRoute />);
});
Expand All @@ -178,6 +199,7 @@ describe('DiscoverRouter', () => {
services={mockDiscoverServices}
history={history}
profileRegistry={profileRegistry}
customizationContext={customizationContext}
isDev={props.isDev}
/>
);
Expand All @@ -186,13 +208,21 @@ describe('DiscoverRouter', () => {

it('should show DiscoverRoutes component for / route', () => {
expect(pathMap['/']).toMatchObject(
<DiscoverRoutes customizationCallbacks={callbacks} isDev={props.isDev} />
<DiscoverRoutes
customizationCallbacks={callbacks}
customizationContext={customizationContext}
isDev={props.isDev}
/>
);
});

it(`should show CustomDiscoverRoutes component for ${profilePath} route`, () => {
expect(pathMap[profilePath]).toMatchObject(
<CustomDiscoverRoutes profileRegistry={profileRegistry} isDev={props.isDev} />
<CustomDiscoverRoutes
profileRegistry={profileRegistry}
customizationContext={customizationContext}
isDev={props.isDev}
/>
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { DiscoverProfileRegistry } from '../customizations/profile_registry
import { addProfile } from '../../common/customizations';
import type { DiscoverCustomizationContext } from './types';

interface DiscoverRoutesProps {
export interface DiscoverRoutesProps {
prefix?: string;
customizationCallbacks: CustomizationCallback[];
customizationContext: DiscoverCustomizationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createHashHistory } from 'history';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { buildDataTableRecordList } from '@kbn/discover-utils';
import { esHitsMock } from '@kbn/discover-utils/src/__mocks__';
import { FetchStatus } from '../../../../types';
import { DiscoverCustomizationContext, FetchStatus } from '../../../../types';
import {
AvailableFields$,
DataDocuments$,
Expand Down Expand Up @@ -124,11 +124,17 @@ function getSavedSearch(dataView: DataView) {
} as unknown as SavedSearch;
}

const customizationContext: DiscoverCustomizationContext = {
displayMode: 'standalone',
showLogExplorerTabs: false,
};

export function getDocumentsLayoutProps(dataView: DataView) {
const stateContainer = getDiscoverStateContainer({
history: createHashHistory(),
savedSearch: getSavedSearch(dataView),
services,
customizationContext,
});
stateContainer.appState.set({
columns: ['name', 'message', 'bytes'],
Expand All @@ -153,6 +159,7 @@ export const getPlainRecordLayoutProps = (dataView: DataView) => {
history: createHashHistory(),
savedSearch: getSavedSearch(dataView),
services,
customizationContext,
});
stateContainer.appState.set({
columns: ['name', 'message', 'bytes'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { mountWithIntl } from '@kbn/test-jest-helpers';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { DiscoverTopNav, DiscoverTopNavProps } from './discover_topnav';
import { TopNavMenu, TopNavMenuData } from '@kbn/navigation-plugin/public';
import { Query } from '@kbn/es-query';
import { setHeaderActionMenuMounter } from '../../../../kibana_services';
import { discoverServiceMock as mockDiscoverService } from '../../../../__mocks__/services';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
Expand Down Expand Up @@ -77,7 +76,6 @@ function getProps(

return {
stateContainer,
query: {} as Query,
savedQuery: '',
updateQuery: jest.fn(),
onOpenInspector: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function getStateContainer({ dataView }: { dataView?: DataView } = {}) {
const stateContainer = getDiscoverStateContainer({
services: discoverServiceMock,
history,
customizationContext: {
displayMode: 'standalone',
showLogExplorerTabs: false,
},
});
stateContainer.savedSearchState.set(savedSearch);
stateContainer.appState.getState = jest.fn(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { waitFor } from '@testing-library/react';
import { setHeaderActionMenuMounter, setScopedHistory } from '../../kibana_services';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { discoverServiceMock } from '../../__mocks__/services';
import { DiscoverMainRoute } from './discover_main_route';
import { DiscoverMainRoute, MainRouteProps } from './discover_main_route';
import { MemoryRouter } from 'react-router-dom';
import { DiscoverMainApp } from './discover_main_app';
import { findTestSubject } from '@elastic/eui/lib/test';
Expand Down Expand Up @@ -101,9 +101,13 @@ describe('DiscoverMainRoute', () => {
});

const mountComponent = (hasESData = true, hasUserDataView = true) => {
const props = {
const props: MainRouteProps = {
isDev: false,
customizationCallbacks: [],
customizationContext: {
displayMode: 'standalone',
showLogExplorerTabs: false,
},
};

return mountWithIntl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import {
} from '../../../__mocks__/saved_search';
import { discoverServiceMock } from '../../../__mocks__/services';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { DiscoverAppStateContainer } from './discover_app_state_container';
import type { DiscoverAppStateContainer } from './discover_app_state_container';
import { waitFor } from '@testing-library/react';
import { FetchStatus } from '../../types';
import { DiscoverCustomizationContext, FetchStatus } from '../../types';
import { dataViewAdHoc, dataViewComplexMock } from '../../../__mocks__/data_view_complex';
import { copySavedSearch } from './discover_saved_search_container';

Expand All @@ -34,6 +34,11 @@ const startSync = (appState: DiscoverAppStateContainer) => {
return stop;
};

const customizationContext: DiscoverCustomizationContext = {
displayMode: 'standalone',
showLogExplorerTabs: false,
};

async function getState(
url: string = '/',
{ savedSearch, isEmptyUrl }: { savedSearch?: SavedSearch; isEmptyUrl?: boolean } = {}
Expand All @@ -51,6 +56,7 @@ async function getState(
const nextState = getDiscoverStateContainer({
services: discoverServiceMock,
history: nextHistory,
customizationContext,
});
nextState.appState.isEmptyURL = jest.fn(() => isEmptyUrl ?? true);
jest.spyOn(nextState.dataState, 'fetch');
Expand Down Expand Up @@ -87,9 +93,10 @@ describe('Test discover state', () => {
state = getDiscoverStateContainer({
services: discoverServiceMock,
history,
customizationContext,
});
state.savedSearchState.set(savedSearchMock);
await state.appState.update({}, true);
state.appState.update({}, true);
stopSync = startSync(state.appState);
});
afterEach(() => {
Expand Down Expand Up @@ -137,7 +144,10 @@ describe('Test discover state', () => {
test('pauseAutoRefreshInterval sets refreshInterval.pause to true', async () => {
history.push('/#?_g=(refreshInterval:(pause:!f,value:5000))');
expect(getCurrentUrl()).toBe('/#?_g=(refreshInterval:(pause:!f,value:5000))');
await state.actions.setDataView(dataViewMock);
// TODO: state.actions.setDataView should be async because it calls pauseAutoRefreshInterval which is async.
// I found this bug while removing unnecessary awaits, but it will need to be fixed in a follow up PR.
state.actions.setDataView(dataViewMock);
await new Promise(process.nextTick);
expect(getCurrentUrl()).toBe('/#?_g=(refreshInterval:(pause:!t,value:5000))');
});
});
Expand Down Expand Up @@ -190,6 +200,7 @@ describe('Test createSearchSessionRestorationDataProvider', () => {
const discoverStateContainer = getDiscoverStateContainer({
services: discoverServiceMock,
history,
customizationContext,
});
discoverStateContainer.appState.update({
index: savedSearchMock.searchSource.getField('index')!.id,
Expand Down Expand Up @@ -661,9 +672,9 @@ describe('Test discover state actions', () => {
const { state } = await getState('/', { savedSearch: savedSearchMock });
const unsubscribe = state.actions.initializeAndSync();
await state.actions.loadSavedSearch({ savedSearchId: savedSearchMock.id });
await state.savedSearchState.update({ nextState: { hideChart: true } });
state.savedSearchState.update({ nextState: { hideChart: true } });
expect(state.savedSearchState.getState().hideChart).toBe(true);
await state.actions.onOpenSavedSearch(savedSearchMock.id!);
state.actions.onOpenSavedSearch(savedSearchMock.id!);
expect(state.savedSearchState.getState().hideChart).toBe(undefined);
unsubscribe();
});
Expand Down Expand Up @@ -756,16 +767,21 @@ describe('Test discover state with embedded mode', () => {
state = getDiscoverStateContainer({
services: discoverServiceMock,
history,
displayMode: 'embedded',
customizationContext: {
...customizationContext,
displayMode: 'embedded',
},
});
state.savedSearchState.set(savedSearchMock);
await state.appState.update({}, true);
state.appState.update({}, true);
stopSync = startSync(state.appState);
});

afterEach(() => {
stopSync();
stopSync = () => {};
});

test('setting app state and syncing to URL', async () => {
state.appState.update({ index: 'modified' });
await new Promise(process.nextTick);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/discover/public/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Start = jest.Mocked<DiscoverStart>;
const createSetupContract = (): Setup => {
const setupContract: Setup = {
locator: sharePluginMock.createLocator(),
showLogExplorerTabs: jest.fn(),
};
return setupContract;
};
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export interface DiscoverSetupPlugins {
data: DataPublicPluginSetup;
expressions: ExpressionsSetup;
globalSearch?: GlobalSearchPluginSetup;
serverless?: ServerlessPluginStart;
}

/**
Expand Down Expand Up @@ -200,6 +199,7 @@ export interface DiscoverStartPlugins {
lens: LensPublicStart;
contentManagement: ContentManagementPublicStart;
noDataPage?: NoDataPagePluginStart;
serverless?: ServerlessPluginStart;
}

/**
Expand Down
Loading

0 comments on commit b88e211

Please sign in to comment.