|
| 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 | +}); |
0 commit comments