Skip to content

Commit 6b691a3

Browse files
committed
Add test
1 parent 844e4b7 commit 6b691a3

File tree

6 files changed

+304
-4
lines changed

6 files changed

+304
-4
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
export const esHits = [
20+
{
21+
_index: 'i',
22+
_id: '1',
23+
_score: 1,
24+
_type: '_doc',
25+
_source: { date: '2020-20-01T12:12:12.123', message: 'test1', bytes: 20 },
26+
},
27+
{
28+
_index: 'i',
29+
_id: '2',
30+
_score: 1,
31+
_type: '_doc',
32+
_source: { date: '2020-20-01T12:12:12.124', name: 'test2', extension: 'jpg' },
33+
},
34+
{
35+
_index: 'i',
36+
_id: '3',
37+
_score: 1,
38+
_type: '_doc',
39+
_source: { date: '2020-20-01T12:12:12.124', name: 'test3', extension: 'gif', bytes: 50 },
40+
},
41+
{
42+
_index: 'i',
43+
_id: '4',
44+
_score: 1,
45+
_type: '_doc',
46+
_source: { date: '2020-20-01T12:12:12.125', name: 'test4', extension: 'png', bytes: 50 },
47+
},
48+
{
49+
_index: 'i',
50+
_id: '5',
51+
_score: 1,
52+
_type: '_doc',
53+
_source: { date: '2020-20-01T12:12:12.128', name: 'test5', extension: 'doc', bytes: 50 },
54+
},
55+
];

src/plugins/discover/public/__mocks__/index_pattern.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
* under the License.
1818
*/
1919

20-
import { IndexPattern, indexPatterns } from '../kibana_services';
2120
import { IIndexPatternFieldList } from '../../../data/common/index_patterns/fields';
21+
import { IndexPattern } from '../../../data/common';
22+
import { indexPatterns } from '../../../data/public';
2223

2324
const fields = [
2425
{
@@ -67,8 +68,10 @@ const indexPattern = ({
6768
getComputedFields: () => ({}),
6869
getSourceFiltering: () => ({}),
6970
getFieldByName: () => ({}),
71+
timeFieldName: '',
7072
} as unknown) as IndexPattern;
7173

7274
indexPattern.flattenHit = indexPatterns.flattenHitWrapper(indexPattern, indexPattern.metaFields);
75+
indexPattern.isTimeBased = () => !!indexPattern.timeFieldName;
7376

7477
export const indexPatternMock = indexPattern;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { IIndexPatternFieldList } from '../../../data/common/index_patterns/fields';
21+
import { IndexPattern } from '../../../data/common';
22+
import { indexPatterns } from '../../../data/public';
23+
24+
const fields = [
25+
{
26+
name: '_index',
27+
type: 'string',
28+
scripted: false,
29+
filterable: true,
30+
},
31+
{
32+
name: 'timestamp',
33+
type: 'date',
34+
scripted: false,
35+
filterable: true,
36+
},
37+
{
38+
name: 'message',
39+
type: 'string',
40+
scripted: false,
41+
filterable: false,
42+
},
43+
{
44+
name: 'extension',
45+
type: 'string',
46+
scripted: false,
47+
filterable: true,
48+
},
49+
{
50+
name: 'bytes',
51+
type: 'number',
52+
scripted: false,
53+
filterable: true,
54+
},
55+
{
56+
name: 'scripted',
57+
type: 'number',
58+
scripted: true,
59+
filterable: false,
60+
},
61+
] as IIndexPatternFieldList;
62+
63+
fields.getByName = (name: string) => {
64+
return fields.find((field) => field.name === name);
65+
};
66+
67+
const indexPattern = ({
68+
id: 'index-pattern-with-timefield-id',
69+
title: 'index-pattern-without-timefield',
70+
metaFields: ['_index', '_score'],
71+
flattenHit: undefined,
72+
formatHit: jest.fn((hit) => hit._source),
73+
fields,
74+
getComputedFields: () => ({}),
75+
getSourceFiltering: () => ({}),
76+
getFieldByName: () => ({}),
77+
timeFieldName: 'timestamp',
78+
} as unknown) as IndexPattern;
79+
80+
indexPattern.flattenHit = indexPatterns.flattenHitWrapper(indexPattern, indexPattern.metaFields);
81+
indexPattern.isTimeBased = () => !!indexPattern.timeFieldName;
82+
83+
export const indexPatternWithTimefieldMock = indexPattern;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { IUiSettingsClient } from 'kibana/public';
20+
import { SAMPLE_SIZE_SETTING } from '../../common';
21+
22+
export const uiSettingsMock = ({
23+
get: (key: string) => {
24+
if (key === SAMPLE_SIZE_SETTING) {
25+
return 10;
26+
}
27+
return false;
28+
},
29+
} as unknown) as IUiSettingsClient;
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import React from 'react';
21+
import { shallowWithIntl } from '@kbn/test/jest';
22+
import { DiscoverLegacy } from './discover_legacy';
23+
import { inspectorPluginMock } from '../../../../inspector/public/mocks';
24+
import { esHits } from '../../__mocks__/es_hits';
25+
import { indexPatternMock } from '../../__mocks__/index_pattern';
26+
import { getTopNavLinks } from './top_nav/get_top_nav_links';
27+
import { DiscoverServices } from '../../build_services';
28+
import { GetStateReturn } from '../angular/discover_state';
29+
import { savedSearchMock } from '../../__mocks__/saved_search';
30+
import { createSearchSourceMock } from '../../../../data/common/search/search_source/mocks';
31+
import { dataPluginMock } from '../../../../data/public/mocks';
32+
import { createFilterManagerMock } from '../../../../data/public/query/filter_manager/filter_manager.mock';
33+
import { uiSettingsMock } from '../../__mocks__/ui_settings';
34+
import { IndexPattern, IndexPatternAttributes } from '../../../../data/common/index_patterns';
35+
import { SavedObject } from '../../../../../core/types';
36+
import { navigationPluginMock } from '../../../../navigation/public/mocks';
37+
import { indexPatternWithTimefieldMock } from '../../__mocks__/index_pattern_with_timefield';
38+
import { calcFieldCounts } from '../helpers/calc_field_counts';
39+
40+
const mockNavigation = navigationPluginMock.createStartContract();
41+
42+
jest.mock('../../kibana_services', () => {
43+
return {
44+
getServices: () => ({
45+
metadata: {
46+
branch: 'test',
47+
},
48+
capabilities: {
49+
discover: {
50+
save: true,
51+
},
52+
},
53+
navigation: mockNavigation,
54+
}),
55+
};
56+
});
57+
58+
function getProps(indexPattern: IndexPattern) {
59+
const searchSourceMock = createSearchSourceMock({});
60+
const state = ({} as unknown) as GetStateReturn;
61+
const services = ({
62+
capabilities: {
63+
discover: {
64+
save: true,
65+
},
66+
},
67+
} as unknown) as DiscoverServices;
68+
69+
return {
70+
addColumn: jest.fn(),
71+
fetch: jest.fn(),
72+
fetchCounter: 0,
73+
fetchError: undefined,
74+
fieldCounts: calcFieldCounts({}, esHits, indexPattern),
75+
hits: esHits.length,
76+
indexPattern,
77+
minimumVisibleRows: 10,
78+
onAddFilter: jest.fn(),
79+
onChangeInterval: jest.fn(),
80+
onMoveColumn: jest.fn(),
81+
onRemoveColumn: jest.fn(),
82+
onSetColumns: jest.fn(),
83+
onSkipBottomButtonClick: jest.fn(),
84+
onSort: jest.fn(),
85+
opts: {
86+
config: uiSettingsMock,
87+
data: dataPluginMock.createStartContract(),
88+
fixedScroll: jest.fn(),
89+
filterManager: createFilterManagerMock(),
90+
indexPatternList: (indexPattern as unknown) as Array<SavedObject<IndexPatternAttributes>>,
91+
sampleSize: 10,
92+
savedSearch: savedSearchMock,
93+
setHeaderActionMenu: jest.fn(),
94+
timefield: indexPattern.timeFieldName || '',
95+
setAppState: jest.fn(),
96+
},
97+
resetQuery: jest.fn(),
98+
resultState: 'ready',
99+
rows: esHits,
100+
searchSource: searchSourceMock,
101+
setIndexPattern: jest.fn(),
102+
showSaveQuery: true,
103+
state: { columns: [] },
104+
timefilterUpdateHandler: jest.fn(),
105+
topNavMenu: getTopNavLinks({
106+
getFieldCounts: jest.fn(),
107+
indexPattern,
108+
inspectorAdapters: inspectorPluginMock,
109+
navigateTo: jest.fn(),
110+
savedSearch: savedSearchMock,
111+
services,
112+
state,
113+
}),
114+
updateQuery: jest.fn(),
115+
updateSavedQueryId: jest.fn(),
116+
};
117+
}
118+
119+
describe('Descover legacy component', () => {
120+
test('selected index pattern without time field displays no chart toggle', () => {
121+
const component = shallowWithIntl(<DiscoverLegacy {...getProps(indexPatternMock)} />);
122+
expect(component.find('[data-test-subj="discoverChartToggle"]').length).toBe(0);
123+
});
124+
test('selected index pattern with time field displays chart toggle', () => {
125+
const component = shallowWithIntl(
126+
<DiscoverLegacy {...getProps(indexPatternWithTimefieldMock)} />
127+
);
128+
expect(component.find('[data-test-subj="discoverChartToggle"]').length).toBe(1);
129+
});
130+
});

src/plugins/discover/public/application/components/discover_legacy.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ export interface DiscoverProps {
6666
addColumn: (column: string) => void;
6767
fetch: () => void;
6868
fetchCounter: number;
69-
fetchError: Error;
69+
fetchError?: Error;
7070
fieldCounts: Record<string, number>;
71-
histogramData: Chart;
71+
histogramData?: Chart;
7272
hits: number;
7373
indexPattern: IndexPattern;
7474
minimumVisibleRows: number;
@@ -300,7 +300,7 @@ export function DiscoverLegacy({
300300
)}
301301
className="dscTimechart"
302302
>
303-
{opts.chartAggConfigs && rows.length !== 0 && (
303+
{opts.chartAggConfigs && rows.length !== 0 && histogramData && (
304304
<div className="dscHistogram" data-test-subj="discoverChart">
305305
<DiscoverHistogram
306306
chartData={histogramData}

0 commit comments

Comments
 (0)