Skip to content

Commit

Permalink
Merge pull request #2 from dimaanj/fix-ci-unified-histogram-pr
Browse files Browse the repository at this point in the history
[Discover] Fix jest tests of unified histogram pr
  • Loading branch information
davismcphee authored Nov 5, 2022
2 parents af9a681 + e45eef3 commit 191c1ac
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const fields = [
},
{
name: 'timestamp',
displayName: 'timestamp',
type: 'date',
scripted: false,
filterable: true,
Expand All @@ -26,26 +27,30 @@ const fields = [
},
{
name: 'message',
displayName: 'message',
type: 'string',
scripted: false,
filterable: false,
},
{
name: 'extension',
displayName: 'extension',
type: 'string',
scripted: false,
filterable: true,
aggregatable: true,
},
{
name: 'bytes',
displayName: 'bytes',
type: 'number',
scripted: false,
filterable: true,
aggregatable: true,
},
{
name: 'scripted',
displayName: 'scripted',
type: 'number',
scripted: true,
filterable: false,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/discover/public/__mocks__/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ export const discoverServiceMock = {
savedObjectsTagging: {},
dataViews: dataViewsMock,
timefilter: { createFilter: jest.fn() },
lens: { EmbeddableComponent: jest.fn(() => null) },
} as unknown as DiscoverServices;
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
*/

import React from 'react';
import { Subject, BehaviorSubject } from 'rxjs';
import { Subject, BehaviorSubject, of } from 'rxjs';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import type { Query, AggregateQuery } from '@kbn/es-query';
import { setHeaderActionMenuMounter } from '../../../../kibana_services';
import { DiscoverLayout, SIDEBAR_CLOSED_KEY } from './discover_layout';
import { esHits } from '../../../../__mocks__/es_hits';
import { dataViewMock } from '../../../../__mocks__/data_view';
import { savedSearchMock } from '../../../../__mocks__/saved_search';
import { createSearchSourceMock } from '@kbn/data-plugin/common/search/search_source/mocks';
import {
createSearchSourceMock,
searchSourceInstanceMock,
} from '@kbn/data-plugin/common/search/search_source/mocks';
import type { DataView } from '@kbn/data-views-plugin/public';
import { dataViewWithTimefieldMock } from '../../../../__mocks__/data_view_with_timefield';
import { GetStateReturn } from '../../services/discover_state';
import { DiscoverLayoutProps } from './types';
import {
AvailableFields$,
DataCharts$,
DataDocuments$,
DataMain$,
DataTotalHits$,
Expand All @@ -37,66 +39,10 @@ import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { DiscoverServices } from '../../../../build_services';
import { buildDataTableRecord } from '../../../../utils/build_data_record';
import { DiscoverAppStateProvider } from '../../services/discover_app_state_container';
import type { UnifiedHistogramChartData } from '@kbn/unified-histogram-plugin/public';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { setTimeout } from 'timers/promises';
import { act } from 'react-dom/test-utils';

jest.mock('@kbn/unified-histogram-plugin/public', () => {
const originalModule = jest.requireActual('@kbn/unified-histogram-plugin/public');

const chartData = {
xAxisOrderedValues: [
1623880800000, 1623967200000, 1624053600000, 1624140000000, 1624226400000, 1624312800000,
1624399200000, 1624485600000, 1624572000000, 1624658400000, 1624744800000, 1624831200000,
1624917600000, 1625004000000, 1625090400000,
],
xAxisFormat: { id: 'date', params: { pattern: 'YYYY-MM-DD' } },
xAxisLabel: 'order_date per day',
yAxisFormat: { id: 'number' },
ordered: {
date: true,
interval: {
asMilliseconds: jest.fn(),
},
intervalESUnit: 'd',
intervalESValue: 1,
min: '2021-03-18T08:28:56.411Z',
max: '2021-07-01T07:28:56.411Z',
},
yAxisLabel: 'Count',
values: [
{ x: 1623880800000, y: 134 },
{ x: 1623967200000, y: 152 },
{ x: 1624053600000, y: 141 },
{ x: 1624140000000, y: 138 },
{ x: 1624226400000, y: 142 },
{ x: 1624312800000, y: 157 },
{ x: 1624399200000, y: 149 },
{ x: 1624485600000, y: 146 },
{ x: 1624572000000, y: 170 },
{ x: 1624658400000, y: 137 },
{ x: 1624744800000, y: 150 },
{ x: 1624831200000, y: 144 },
{ x: 1624917600000, y: 147 },
{ x: 1625004000000, y: 137 },
{ x: 1625090400000, y: 66 },
],
} as unknown as UnifiedHistogramChartData;

return {
...originalModule,
buildChartData: jest.fn().mockImplementation(() => ({
chartData,
bucketInterval: {
scaled: true,
description: 'test',
scale: 2,
},
})),
};
});
import { createSearchSessionMock } from '../../../../__mocks__/search_session';

function getAppStateContainer() {
const appStateContainer = getDiscoverStateMock({ isTimeBased: true }).appStateContainer;
Expand Down Expand Up @@ -127,6 +73,14 @@ async function mountComponent(
return { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' };
};

(services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({
language: 'kuery',
query: '',
});
(searchSourceInstanceMock.fetch$ as jest.Mock).mockImplementation(
jest.fn().mockReturnValue(of({ rawResponse: { hits: { total: 2 } } }))
);

const dataViewList = [dataView];

const main$ = new BehaviorSubject({
Expand All @@ -150,16 +104,10 @@ async function mountComponent(
result: Number(esHits.length),
}) as DataTotalHits$;

const charts$ = new BehaviorSubject({
fetchStatus: FetchStatus.COMPLETE,
response: {} as unknown as SearchResponse,
}) as DataCharts$;

const savedSearchData$ = {
main$,
documents$,
totalHits$,
charts$,
availableFields$,
};

Expand All @@ -175,7 +123,7 @@ async function mountComponent(
savedSearchData$,
savedSearchRefetch$: new Subject(),
searchSource: searchSourceMock,
state: { columns: [], query },
state: { columns: [], query, hideChart: false, interval: 'auto' },
stateContainer: {
setAppState: () => {},
appStateContainer: {
Expand All @@ -188,6 +136,7 @@ async function mountComponent(
persistDataView: jest.fn(),
updateAdHocDataViewId: jest.fn(),
adHocDataViewList: [],
searchSessionManager: createSearchSessionMock().searchSessionManager,
};

const component = mountWithIntl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
*/

import React from 'react';
import { Subject, BehaviorSubject } from 'rxjs';
import { Subject, BehaviorSubject, of } from 'rxjs';
import { findTestSubject, mountWithIntl } from '@kbn/test-jest-helpers';
import { esHits } from '../../../../__mocks__/es_hits';
import { dataViewMock } from '../../../../__mocks__/data_view';
import { savedSearchMock } from '../../../../__mocks__/saved_search';
import { GetStateReturn } from '../../services/discover_state';
import {
AvailableFields$,
DataCharts$,
DataDocuments$,
DataMain$,
DataTotalHits$,
Expand All @@ -33,67 +32,11 @@ import { setTimeout } from 'timers/promises';
import { DocumentViewModeToggle } from '../../../../components/view_mode_toggle';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { LocalStorageMock } from '../../../../__mocks__/local_storage_mock';
import {
UnifiedHistogramChartData,
UnifiedHistogramLayout,
} from '@kbn/unified-histogram-plugin/public';
import { UnifiedHistogramLayout } from '@kbn/unified-histogram-plugin/public';
import { HISTOGRAM_HEIGHT_KEY } from './use_discover_histogram';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';

jest.mock('@kbn/unified-histogram-plugin/public', () => {
const originalModule = jest.requireActual('@kbn/unified-histogram-plugin/public');

const chartData = {
xAxisOrderedValues: [
1623880800000, 1623967200000, 1624053600000, 1624140000000, 1624226400000, 1624312800000,
1624399200000, 1624485600000, 1624572000000, 1624658400000, 1624744800000, 1624831200000,
1624917600000, 1625004000000, 1625090400000,
],
xAxisFormat: { id: 'date', params: { pattern: 'YYYY-MM-DD' } },
xAxisLabel: 'order_date per day',
yAxisFormat: { id: 'number' },
ordered: {
date: true,
interval: {
asMilliseconds: jest.fn(),
},
intervalESUnit: 'd',
intervalESValue: 1,
min: '2021-03-18T08:28:56.411Z',
max: '2021-07-01T07:28:56.411Z',
},
yAxisLabel: 'Count',
values: [
{ x: 1623880800000, y: 134 },
{ x: 1623967200000, y: 152 },
{ x: 1624053600000, y: 141 },
{ x: 1624140000000, y: 138 },
{ x: 1624226400000, y: 142 },
{ x: 1624312800000, y: 157 },
{ x: 1624399200000, y: 149 },
{ x: 1624485600000, y: 146 },
{ x: 1624572000000, y: 170 },
{ x: 1624658400000, y: 137 },
{ x: 1624744800000, y: 150 },
{ x: 1624831200000, y: 144 },
{ x: 1624917600000, y: 147 },
{ x: 1625004000000, y: 137 },
{ x: 1625090400000, y: 66 },
],
} as unknown as UnifiedHistogramChartData;

return {
...originalModule,
buildChartData: jest.fn().mockImplementation(() => ({
chartData,
bucketInterval: {
scaled: true,
description: 'test',
scale: 2,
},
})),
};
});
import { createSearchSessionMock } from '../../../../__mocks__/search_session';
import { RequestAdapter } from '@kbn/inspector-plugin/public';
import { searchSourceInstanceMock } from '@kbn/data-plugin/common/search/search_source/mocks';

const mountComponent = async ({
isPlainRecord = false,
Expand All @@ -115,6 +58,14 @@ const mountComponent = async ({
return { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' };
};

(services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({
language: 'kuery',
query: '',
});
(searchSourceInstanceMock.fetch$ as jest.Mock).mockImplementation(
jest.fn().mockReturnValue(of({ rawResponse: { hits: { total: 2 } } }))
);

if (storage) {
services = { ...services, storage };
}
Expand All @@ -140,16 +91,10 @@ const mountComponent = async ({
result: Number(esHits.length),
}) as DataTotalHits$;

const charts$ = new BehaviorSubject({
fetchStatus: FetchStatus.COMPLETE,
response: {} as unknown as SearchResponse,
}) as DataCharts$;

const savedSearchData$ = {
main$,
documents$,
totalHits$,
charts$,
availableFields$,
};

Expand Down Expand Up @@ -177,6 +122,8 @@ const mountComponent = async ({
onFieldEdited: jest.fn(),
columns: [],
resizeRef: { current: null },
searchSessionManager: createSearchSessionMock().searchSessionManager,
inspectorAdapters: { requests: new RequestAdapter() },
};

const coreTheme$ = new BehaviorSubject<CoreTheme>({ darkMode: false });
Expand Down
Loading

0 comments on commit 191c1ac

Please sign in to comment.