From 94589dcd8e97fa9f84ab424585d05385a59deb08 Mon Sep 17 00:00:00 2001 From: Liza K Date: Wed, 4 Sep 2019 13:36:02 +0300 Subject: [PATCH 01/22] Create timefilter service --- .../filter/filter_manager/filter_manager.ts | 17 +++--- ...e_time_filter.js => change_time_filter.ts} | 5 +- ..._time_filter.js => extract_time_filter.ts} | 7 ++- .../data/public/filter/filter_service.ts | 6 +- src/legacy/core_plugins/data/public/index.ts | 2 + src/legacy/core_plugins/data/public/plugin.ts | 9 +++ .../data/public/timefilter/index.ts | 2 + .../public/timefilter/timefilter_service.ts | 58 +++++++++++++++++++ src/legacy/ui/public/timefilter/index.ts | 11 ++-- 9 files changed, 96 insertions(+), 21 deletions(-) rename src/legacy/core_plugins/data/public/filter/filter_manager/lib/{change_time_filter.js => change_time_filter.ts} (89%) rename src/legacy/core_plugins/data/public/filter/filter_manager/lib/{extract_time_filter.js => extract_time_filter.ts} (82%) create mode 100644 src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts index f230b035de352..a981103e3a5ad 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts @@ -29,16 +29,13 @@ import { compareFilters } from './lib/compare_filters'; import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; // @ts-ignore import { uniqFilters } from './lib/uniq_filters'; -// @ts-ignore + import { extractTimeFilter } from './lib/extract_time_filter'; -// @ts-ignore import { changeTimeFilter } from './lib/change_time_filter'; - import { onlyDisabledFiltersChanged } from './lib/only_disabled'; - import { PartitionedFilters } from './partitioned_filters'; - import { IndexPatterns } from '../../index_patterns'; +import { Timefilter } from '../../timefilter'; export class FilterManager { private indexPatterns: IndexPatterns; @@ -46,10 +43,16 @@ export class FilterManager { private updated$: Subject = new Subject(); private fetch$: Subject = new Subject(); private uiSettings: UiSettingsClientContract; + private timefilter: Timefilter; - constructor(indexPatterns: IndexPatterns, uiSettings: UiSettingsClientContract) { + constructor( + indexPatterns: IndexPatterns, + uiSettings: UiSettingsClientContract, + timefilter: Timefilter + ) { this.indexPatterns = indexPatterns; this.uiSettings = uiSettings; + this.timefilter = timefilter; } private mergeIncomingFilters(partitionedFilters: PartitionedFilters): Filter[] { @@ -191,7 +194,7 @@ export class FilterManager { public async addFiltersAndChangeTimeFilter(filters: Filter[]) { const timeFilter = await extractTimeFilter(this.indexPatterns, filters); - if (timeFilter) changeTimeFilter(timeFilter); + if (timeFilter) changeTimeFilter(this.timefilter, timeFilter); return this.addFilters(filters.filter(filter => filter !== timeFilter)); } diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.js b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts similarity index 89% rename from src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.js rename to src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts index eae71d244f888..7cf64c737fdac 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.js +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts @@ -19,14 +19,13 @@ import moment from 'moment'; import _ from 'lodash'; -import { timefilter } from 'ui/timefilter'; +import { Timefilter } from '../../../timefilter'; -export function changeTimeFilter(filter) { +export function changeTimeFilter(timefilter: Timefilter, filter: any) { const key = _.keys(filter.range)[0]; const values = filter.range[key]; timefilter.setTime({ from: moment(values.gt || values.gte), to: moment(values.lt || values.lte), - mode: 'absolute', }); } diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/extract_time_filter.js b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/extract_time_filter.ts similarity index 82% rename from src/legacy/core_plugins/data/public/filter/filter_manager/lib/extract_time_filter.js rename to src/legacy/core_plugins/data/public/filter/filter_manager/lib/extract_time_filter.ts index 3d3fb92046507..e6fea9cccd8f0 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/extract_time_filter.js +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/extract_time_filter.ts @@ -18,16 +18,17 @@ */ import _ from 'lodash'; +import { IndexPatterns } from '../../../index_patterns'; -export async function extractTimeFilter(indexPatterns, filters) { +export async function extractTimeFilter(indexPatterns: IndexPatterns, filters: any) { // Assume all the index patterns are the same since they will be added // from the same visualization. - const id = _.get(filters, '[0].meta.index'); + const id: string = _.get(filters, '[0].meta.index'); if (id == null) return; const indexPattern = await indexPatterns.get(id); - const filter = _.find(filters, function (obj) { + const filter = _.find(filters, function(obj: any) { const key = _.keys(obj.range)[0]; return key === indexPattern.timeFieldName; }); diff --git a/src/legacy/core_plugins/data/public/filter/filter_service.ts b/src/legacy/core_plugins/data/public/filter/filter_service.ts index 063b69c175b20..c36b122edca19 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_service.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_service.ts @@ -20,6 +20,7 @@ import { UiSettingsClientContract } from 'src/core/public'; import { IndexPatterns } from '../index_patterns'; import { FilterManager } from './filter_manager'; +import { Timefilter } from '../timefilter'; /** * Filter Service @@ -29,12 +30,13 @@ import { FilterManager } from './filter_manager'; export interface FilterServiceDependencies { indexPatterns: IndexPatterns; uiSettings: UiSettingsClientContract; + timefilter: Timefilter; } export class FilterService { - public setup({ indexPatterns, uiSettings }: FilterServiceDependencies) { + public setup({ indexPatterns, uiSettings, timefilter }: FilterServiceDependencies) { return { - filterManager: new FilterManager(indexPatterns, uiSettings), + filterManager: new FilterManager(indexPatterns, uiSettings, timefilter), }; } diff --git a/src/legacy/core_plugins/data/public/index.ts b/src/legacy/core_plugins/data/public/index.ts index 9c95ef7a82ab8..de1f407a82de2 100644 --- a/src/legacy/core_plugins/data/public/index.ts +++ b/src/legacy/core_plugins/data/public/index.ts @@ -65,3 +65,5 @@ export { mockFields, mockIndexPattern, } from './index_patterns'; + +export { Timefilter, TimeHistory, getTime } from './timefilter'; diff --git a/src/legacy/core_plugins/data/public/plugin.ts b/src/legacy/core_plugins/data/public/plugin.ts index ee4ecd9841095..aec97b02bc2b9 100644 --- a/src/legacy/core_plugins/data/public/plugin.ts +++ b/src/legacy/core_plugins/data/public/plugin.ts @@ -21,6 +21,7 @@ import { CoreSetup, CoreStart, Plugin } from '../../../../core/public'; import { SearchService, SearchSetup } from './search'; import { QueryService, QuerySetup } from './query'; import { FilterService, FilterSetup } from './filter'; +import { TimefilterService, TimefilterSetup } from './timefilter'; import { IndexPatternsService, IndexPatternsSetup } from './index_patterns'; import { LegacyDependenciesPluginSetup } from './shim/legacy_dependencies_plugin'; @@ -43,6 +44,7 @@ export interface DataSetup { filter: FilterSetup; query: QuerySetup; search: SearchSetup; + timefilter: TimefilterSetup; } /** @@ -62,6 +64,7 @@ export class DataPlugin implements Plugin; diff --git a/src/legacy/ui/public/timefilter/index.ts b/src/legacy/ui/public/timefilter/index.ts index 34f2a367a217c..e40c9799a28e6 100644 --- a/src/legacy/ui/public/timefilter/index.ts +++ b/src/legacy/ui/public/timefilter/index.ts @@ -18,14 +18,13 @@ */ import uiRoutes from 'ui/routes'; -import { registerTimefilterWithGlobalState, getTimefilterConfig } from './setup_router'; -import { Timefilter, TimeHistory } from '../../../core_plugins/data/public/timefilter'; +import { registerTimefilterWithGlobalState } from './setup_router'; -const config = getTimefilterConfig(); +import { setup as data } from '../../../core_plugins/data/public/legacy'; -export { Timefilter, TimeHistory, getTime } from '../../../core_plugins/data/public/timefilter'; -export const timeHistory = new TimeHistory(); -export const timefilter = new Timefilter(config, timeHistory); +export { getTime } from '../../../core_plugins/data/public'; +export const timeHistory = data.timefilter.history; +export const timefilter = data.timefilter.timefilter; uiRoutes.addSetupWork((globalState, $rootScope) => { return registerTimefilterWithGlobalState(timefilter, globalState, $rootScope); From a4b77a044d2eb051756d84539fa79d7185d57946 Mon Sep 17 00:00:00 2001 From: Liza K Date: Wed, 4 Sep 2019 13:58:39 +0300 Subject: [PATCH 02/22] Pass timehistory as a dependency to query bar --- .../query_bar/components/query_bar_top_row.tsx | 17 ++++++++--------- .../search/search_bar/components/search_bar.tsx | 4 +++- .../discover/embeddable/search_embeddable.ts | 8 +++++--- .../public/top_nav_menu/top_nav_menu.tsx | 6 ++++-- src/legacy/ui/public/kbn_top_nav/kbn_top_nav.js | 4 ++++ .../ui/public/vis/vis_filters/brush_event.js | 3 +-- .../ui/public/vis/vis_filters/vis_filters.js | 4 +++- src/legacy/ui/public/visualize/loader/vis.js | 4 ++-- 8 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx index 5a1a7030b7119..618457406d886 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx @@ -21,18 +21,16 @@ import { doesKueryExpressionHaveLuceneSyntaxError } from '@kbn/es-query'; import classNames from 'classnames'; import React, { Component } from 'react'; + import { Storage } from 'ui/storage'; -import { timeHistory } from 'ui/timefilter'; +import { documentationLinks } from 'ui/documentation_links'; +import { Toast, toastNotifications } from 'ui/notify'; +import { PersistedLog } from 'ui/persisted_log'; import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiLink, EuiSuperDatePicker } from '@elastic/eui'; - // @ts-ignore import { EuiSuperUpdateButton } from '@elastic/eui'; - import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react'; -import { documentationLinks } from 'ui/documentation_links'; -import { Toast, toastNotifications } from 'ui/notify'; -import { PersistedLog } from 'ui/persisted_log'; import { UiSettingsClientContract, SavedObjectsClientContract, @@ -40,9 +38,9 @@ import { } from 'src/core/public'; import { IndexPattern } from '../../../index_patterns'; import { QueryBarInput } from './query_bar_input'; - import { getQueryLog } from '../lib/get_query_log'; import { Query } from '../index'; +import { TimeHistory } from '../../../timefilter'; interface DateRange { from: string; @@ -73,6 +71,7 @@ interface Props { uiSettings: UiSettingsClientContract; savedObjectsClient: SavedObjectsClientContract; http: HttpServiceBase; + timeHistory: TimeHistory; } interface State { @@ -152,7 +151,7 @@ export class QueryBarTopRowUI extends Component { public onSubmit = ({ query, dateRange }: { query?: Query; dateRange: DateRange }) => { this.handleLuceneSyntaxWarning(); - timeHistory.add(dateRange); + this.props.timeHistory.add(dateRange); this.props.onSubmit({ query, dateRange }); }; @@ -264,7 +263,7 @@ export class QueryBarTopRowUI extends Component { return null; } - const recentlyUsedRanges = timeHistory + const recentlyUsedRanges = this.props.timeHistory .get() .map(({ from, to }: { from: string; to: string }) => { return { diff --git a/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx b/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx index 5f7a8ffaa6313..f71f2b9cdfcdf 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx @@ -32,7 +32,7 @@ import { SavedObjectsClientContract, HttpServiceBase, } from 'src/core/public'; -import { IndexPattern, Query, FilterBar } from '../../../../../data/public'; +import { IndexPattern, Query, FilterBar, TimeHistory } from '../../../../../data/public'; import { QueryBarTopRow } from '../../../query'; import { SavedQuery, SavedQueryAttributes } from '../index'; import { SavedQueryMeta, SaveQueryForm } from './saved_query_management/save_query_form'; @@ -64,6 +64,7 @@ export interface SearchBarProps { query?: Query; savedQuery?: SavedQuery; onQuerySubmit?: (payload: { dateRange: DateRange; query?: Query }) => void; + timeHistory: TimeHistory; // Filter bar showFilterBar?: boolean; filters?: Filter[]; @@ -375,6 +376,7 @@ class SearchBarUI extends Component { queryBar = ( requests: new RequestAdapter(), }; this.initializeSearchScope(); - this.autoRefreshFetchSubscription = timefilter.getAutoRefreshFetch$().subscribe(this.fetch); + this.autoRefreshFetchSubscription = data.timefilter.timefilter + .getAutoRefreshFetch$() + .subscribe(this.fetch); this.subscription = Rx.merge(this.getOutput$(), this.getInput$()).subscribe(() => { this.panelTitle = this.output.title || ''; diff --git a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx index f69b843d9c767..08e3a9b6df917 100644 --- a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx +++ b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.tsx @@ -24,12 +24,13 @@ import { I18nProvider } from '@kbn/i18n/react'; import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { TopNavMenuData } from './top_nav_menu_data'; import { TopNavMenuItem } from './top_nav_menu_item'; -import { SearchBar, SearchBarProps } from '../../../../core_plugins/data/public'; +import { SearchBar, SearchBarProps, TimeHistory } from '../../../../core_plugins/data/public'; type Props = Partial & { name: string; uiSettings: UiSettingsClientContract; savedObjectsClient: SavedObjectsClientContract; + timeHistory: TimeHistory; config?: TopNavMenuData[]; showSearchBar?: boolean; }; @@ -57,9 +58,10 @@ export function TopNavMenu(props: Props) { function renderSearchBar() { // Validate presense of all required fields - if (!props.showSearchBar || !props.savedObjectsClient || !props.http) return; + if (!props.showSearchBar || !props.savedObjectsClient || !props.timeHistory || !props.http) return; return ( { const localStorage = new Storage(window.localStorage); child.setAttribute('http', 'http'); child.setAttribute('store', 'store'); + child.setAttribute('history', 'history'); child.setAttribute('ui-settings', 'uiSettings'); child.setAttribute('saved-objects-client', 'savedObjectsClient'); @@ -58,6 +60,7 @@ module.directive('kbnTopNav', () => { $scope.store = localStorage; $scope.http = npSetup.core.http; $scope.uiSettings = npSetup.core.uiSettings; + $scope.history = data.timefilter.history; $scope.savedObjectsClient = chrome.getSavedObjectsClient(); // Watch config changes @@ -102,6 +105,7 @@ module.directive('kbnTopNavHelper', (reactDirective) => { ['uiSettings', { watchDepth: 'reference' }], ['savedObjectsClient', { watchDepth: 'reference' }], ['intl', { watchDepth: 'reference' }], + ['history', { watchDepth: 'reference' }], ['store', { watchDepth: 'reference' }], ['http', { watchDepth: 'reference' }], diff --git a/src/legacy/ui/public/vis/vis_filters/brush_event.js b/src/legacy/ui/public/vis/vis_filters/brush_event.js index 58941d6dc34a0..4e6300d11b7c6 100644 --- a/src/legacy/ui/public/vis/vis_filters/brush_event.js +++ b/src/legacy/ui/public/vis/vis_filters/brush_event.js @@ -20,9 +20,8 @@ import _ from 'lodash'; import moment from 'moment'; import { buildRangeFilter } from '@kbn/es-query'; -import { timefilter } from 'ui/timefilter'; -export function onBrushEvent(event, $state) { +export function onBrushEvent(timefilter, event, $state) { const isNumber = event.data.ordered; const isDate = isNumber && event.data.ordered.date; diff --git a/src/legacy/ui/public/vis/vis_filters/vis_filters.js b/src/legacy/ui/public/vis/vis_filters/vis_filters.js index ce05ca2bea145..6f168c1fb0f06 100644 --- a/src/legacy/ui/public/vis/vis_filters/vis_filters.js +++ b/src/legacy/ui/public/vis/vis_filters/vis_filters.js @@ -21,6 +21,7 @@ import _ from 'lodash'; import { pushFilterBarFilters } from '../push_filters'; import { onBrushEvent } from './brush_event'; import { uniqFilters } from '../../../../core_plugins/data/public'; +import { setup as data } from '../../../../core_plugins/data/public/legacy'; import { toggleFilterNegated } from '@kbn/es-query'; /** * For terms aggregations on `__other__` buckets, this assembles a list of applicable filter @@ -119,7 +120,8 @@ const VisFiltersProvider = (getAppState, $timeout) => { return { pushFilters, brush: (event) => { - onBrushEvent(event, getAppState()); + const { timefilter } = data.timefilter; + onBrushEvent(timefilter, event, getAppState()); }, }; }; diff --git a/src/legacy/ui/public/visualize/loader/vis.js b/src/legacy/ui/public/visualize/loader/vis.js index c86da562d7217..505ad94a1c658 100644 --- a/src/legacy/ui/public/visualize/loader/vis.js +++ b/src/legacy/ui/public/visualize/loader/vis.js @@ -32,7 +32,7 @@ import _ from 'lodash'; import { VisTypesRegistryProvider } from '../../registry/vis_types'; import { PersistedState } from '../../persisted_state'; import { FilterBarQueryFilterProvider } from '../../filter_manager/query_filter'; -import { timefilter } from 'ui/timefilter'; +import { setup as data } from '../../../../core_plugins/data/public/legacy'; export function VisProvider(Private, indexPatterns, getAppState) { const visTypes = Private(VisTypesRegistryProvider); @@ -51,7 +51,7 @@ export function VisProvider(Private, indexPatterns, getAppState) { this.API = { indexPatterns: indexPatterns, - timeFilter: timefilter, + timeFilter: data.timefilter.timefilter, queryFilter: queryFilter, events: { filter: data => { From dd0abdecd88c00518608f098a0469696432f0824 Mon Sep 17 00:00:00 2001 From: Liza K Date: Wed, 4 Sep 2019 18:37:24 +0300 Subject: [PATCH 03/22] Updated timefilter tests and mocks --- .../filter_manager/filter_manager.test.ts | 14 +-- .../filter/filter_manager/filter_manager.ts | 6 +- .../filter_state_manager.test.ts | 14 +-- ...ter.test.js => change_time_filter.test.ts} | 50 ++++---- .../filter_manager/lib/change_time_filter.ts | 4 +- .../data/public/filter/filter_service.ts | 4 +- src/legacy/core_plugins/data/public/index.ts | 2 +- src/legacy/core_plugins/data/public/mocks.ts | 7 +- .../components/query_bar_top_row.test.tsx | 15 +++ .../components/query_bar_top_row.tsx | 4 +- .../search_bar/components/search_bar.test.tsx | 10 ++ .../search_bar/components/search_bar.tsx | 5 +- .../data/public/timefilter/index.ts | 5 +- .../timefilter/lib/diff_time_picker_vals.ts | 2 +- .../data/public/timefilter/time_history.ts | 2 + .../data/public/timefilter/timefilter.test.ts | 39 +++---- .../data/public/timefilter/timefilter.ts | 110 +++++++++--------- .../timefilter/timefilter_service.mock.ts | 76 ++++++++++++ .../public/timefilter/timefilter_service.ts | 10 +- .../types.ts} | 26 +++-- .../public/dashboard/dashboard_state.test.ts | 43 +++---- .../kibana/public/doc/__tests__/doc.js | 4 +- .../public/top_nav_menu/top_nav_menu.test.tsx | 7 ++ .../public/top_nav_menu/top_nav_menu.tsx | 8 +- .../ui/public/kbn_top_nav/kbn_top_nav.js | 6 +- src/legacy/ui/public/timefilter/index.ts | 11 +- .../ui/public/timefilter/setup_router.ts | 4 +- .../vis/vis_filters/brush_event.test.js | 53 ++++----- .../loader/embedded_visualize_handler.test.ts | 8 +- .../navigation_menu/top_nav/top_nav.tsx | 8 +- .../public/views/__tests__/base_controller.js | 20 ++-- .../views/__tests__/base_table_controller.js | 4 +- 32 files changed, 337 insertions(+), 244 deletions(-) rename src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/{change_time_filter.test.js => change_time_filter.test.ts} (67%) create mode 100644 src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts rename src/legacy/core_plugins/data/public/{filter/filter_manager/lib/__tests__/change_time_filter.test.mocks.ts => timefilter/types.ts} (63%) diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.test.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.test.ts index a6700dc37e8d8..5eaa6744216c4 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.test.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.test.ts @@ -35,18 +35,13 @@ import { getFiltersArray } from './test_helpers/get_filters_array'; import { coreMock } from '../../../../../../core/public/mocks'; const setupMock = coreMock.createSetup(); +import { timefilterServiceMock } from '../../timefilter/timefilter_service.mock'; +const timefilterSetupMock = timefilterServiceMock.createSetupContract(); + setupMock.uiSettings.get.mockImplementation((key: string) => { return true; }); -jest.mock('ui/timefilter', () => { - return { - timefilter: { - setTime: jest.fn(), - }, - }; -}); - describe('filter_manager', () => { let appStateStub: StubState; let globalStateStub: StubState; @@ -66,7 +61,8 @@ describe('filter_manager', () => { indexPatterns = new StubIndexPatterns(); filterManager = new FilterManager( (indexPatterns as unknown) as IndexPatterns, - setupMock.uiSettings + setupMock.uiSettings, + timefilterSetupMock.timefilter ); readyFilters = getFiltersArray(); diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts index a981103e3a5ad..1605c647a2672 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts @@ -35,7 +35,7 @@ import { changeTimeFilter } from './lib/change_time_filter'; import { onlyDisabledFiltersChanged } from './lib/only_disabled'; import { PartitionedFilters } from './partitioned_filters'; import { IndexPatterns } from '../../index_patterns'; -import { Timefilter } from '../../timefilter'; +import { TimefilterContract } from '../../timefilter'; export class FilterManager { private indexPatterns: IndexPatterns; @@ -43,12 +43,12 @@ export class FilterManager { private updated$: Subject = new Subject(); private fetch$: Subject = new Subject(); private uiSettings: UiSettingsClientContract; - private timefilter: Timefilter; + private timefilter: TimefilterContract; constructor( indexPatterns: IndexPatterns, uiSettings: UiSettingsClientContract, - timefilter: Timefilter + timefilter: TimefilterContract ) { this.indexPatterns = indexPatterns; this.uiSettings = uiSettings; diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts index a4345018aa830..a4826e059fa22 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts @@ -31,18 +31,13 @@ import { StubIndexPatterns } from './test_helpers/stub_index_pattern'; import { coreMock } from '../../../../../../core/public/mocks'; const setupMock = coreMock.createSetup(); +import { timefilterServiceMock } from '../../timefilter/timefilter_service.mock'; +const timefilterSetupMock = timefilterServiceMock.createSetupContract(); + setupMock.uiSettings.get.mockImplementation((key: string) => { return true; }); -jest.mock('ui/timefilter', () => { - return { - timefilter: { - setTime: jest.fn(), - }, - }; -}); - describe('filter_state_manager', () => { let appStateStub: StubState; let globalStateStub: StubState; @@ -55,7 +50,8 @@ describe('filter_state_manager', () => { const indexPatterns = new StubIndexPatterns(); filterManager = new FilterManager( (indexPatterns as unknown) as IndexPatterns, - setupMock.uiSettings + setupMock.uiSettings, + timefilterSetupMock.timefilter ); }); diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.js b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.ts similarity index 67% rename from src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.js rename to src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.ts index 3a3cc176308c6..d3d05d20e9fba 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.js +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.ts @@ -16,50 +16,42 @@ * specific language governing permissions and limitations * under the License. */ - -import './change_time_filter.test.mocks'; - -jest.mock('ui/chrome', - () => ({ - getBasePath: () => `/some/base/path`, - getUiSettingsClient: () => { - return { - get: (key) => { - switch(key) { - case 'timepicker:timeDefaults': - return { from: 'now-15m', to: 'now' }; - case 'timepicker:refreshIntervalDefaults': - return { pause: false, value: 0 }; - default: - throw new Error(`Unexpected config key: ${key}`); - } - } - }; - }, - }), { virtual: true }); - import expect from '@kbn/expect'; import { changeTimeFilter } from '../change_time_filter'; -import { timefilter } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; + +import { timefilterServiceMock } from '../../../../timefilter/timefilter_service.mock'; +const timefilterMock = timefilterServiceMock.createSetupContract(); +const timefilter = timefilterMock.timefilter; -describe('changeTimeFilter()', function () { +let _time: TimeRange | undefined; +timefilter.setTime.mockImplementation((time: any) => { + _time = { + from: time.from.toISOString(), + to: time.to.toISOString(), + }; +}); +timefilter.getTime.mockImplementation(() => { + return _time!; +}); + +describe('changeTimeFilter()', function() { const gt = 1388559600000; const lt = 1388646000000; - test('should change the timefilter to match the range gt/lt', function () { + test('should change the timefilter to match the range gt/lt', function() { const filter = { range: { '@timestamp': { gt, lt } } }; - changeTimeFilter(filter); + changeTimeFilter(timefilter, filter); const { to, from } = timefilter.getTime(); expect(to).to.be(new Date(lt).toISOString()); expect(from).to.be(new Date(gt).toISOString()); }); - test('should change the timefilter to match the range gte/lte', function () { + test('should change the timefilter to match the range gte/lte', function() { const filter = { range: { '@timestamp': { gte: gt, lte: lt } } }; - changeTimeFilter(filter); + changeTimeFilter(timefilter, filter); const { to, from } = timefilter.getTime(); expect(to).to.be(new Date(lt).toISOString()); expect(from).to.be(new Date(gt).toISOString()); }); - }); diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts index 7cf64c737fdac..6c6ed775951c8 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/lib/change_time_filter.ts @@ -19,9 +19,9 @@ import moment from 'moment'; import _ from 'lodash'; -import { Timefilter } from '../../../timefilter'; +import { TimefilterContract } from '../../../timefilter'; -export function changeTimeFilter(timefilter: Timefilter, filter: any) { +export function changeTimeFilter(timefilter: TimefilterContract, filter: any) { const key = _.keys(filter.range)[0]; const values = filter.range[key]; timefilter.setTime({ diff --git a/src/legacy/core_plugins/data/public/filter/filter_service.ts b/src/legacy/core_plugins/data/public/filter/filter_service.ts index c36b122edca19..8853fae455c1d 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_service.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_service.ts @@ -20,7 +20,7 @@ import { UiSettingsClientContract } from 'src/core/public'; import { IndexPatterns } from '../index_patterns'; import { FilterManager } from './filter_manager'; -import { Timefilter } from '../timefilter'; +import { TimefilterContract } from '../timefilter'; /** * Filter Service @@ -30,7 +30,7 @@ import { Timefilter } from '../timefilter'; export interface FilterServiceDependencies { indexPatterns: IndexPatterns; uiSettings: UiSettingsClientContract; - timefilter: Timefilter; + timefilter: TimefilterContract; } export class FilterService { diff --git a/src/legacy/core_plugins/data/public/index.ts b/src/legacy/core_plugins/data/public/index.ts index de1f407a82de2..d96e3be8d7e38 100644 --- a/src/legacy/core_plugins/data/public/index.ts +++ b/src/legacy/core_plugins/data/public/index.ts @@ -66,4 +66,4 @@ export { mockIndexPattern, } from './index_patterns'; -export { Timefilter, TimeHistory, getTime } from './timefilter'; +export { TimeHistoryContract, TimefilterContract, getTime, InputTimeRange } from './timefilter'; diff --git a/src/legacy/core_plugins/data/public/mocks.ts b/src/legacy/core_plugins/data/public/mocks.ts index 0ca0b4947d538..2a82927bb3ebf 100644 --- a/src/legacy/core_plugins/data/public/mocks.ts +++ b/src/legacy/core_plugins/data/public/mocks.ts @@ -17,19 +17,18 @@ * under the License. */ -import { DataSetup } from '.'; import { filterServiceMock } from './filter/filter_service.mock'; import { indexPatternsServiceMock } from './index_patterns/index_patterns_service.mock'; import { queryServiceMock } from './query/query_service.mock'; +import { timefilterServiceMock } from './timefilter/timefilter_service.mock'; function createDataSetupMock() { - const mock: MockedKeys> = { + return { filter: filterServiceMock.createSetupContract(), indexPatterns: indexPatternsServiceMock.createSetupContract(), query: queryServiceMock.createSetupContract(), + timefilter: timefilterServiceMock.createSetupContract(), }; - - return mock; } function createDataStartMock() { diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.test.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.test.tsx index c48a3ea719813..76250459ed05a 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.test.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.test.tsx @@ -28,6 +28,13 @@ import { IndexPattern } from '../../../index'; import { coreMock } from '../../../../../../../core/public/mocks'; const startMock = coreMock.createStart(); +import { timefilterServiceMock } from '../../../timefilter/timefilter_service.mock'; +const timefilterSetupMock = timefilterServiceMock.createSetupContract(); + +timefilterSetupMock.history.get.mockImplementation(() => { + return []; +}); + startMock.uiSettings.get.mockImplementation((key: string) => { switch (key) { case 'timepicker:quickRanges': @@ -100,6 +107,7 @@ describe('QueryBarTopRowTopRow', () => { { { { { { { { { return { FilterBar: () =>
, @@ -98,6 +101,7 @@ describe('SearchBar', () => { { { { { { { it('Should render query bar and filter bar', () => { const component = mountWithIntl( void; - timeHistory: TimeHistory; + timeHistory: TimeHistoryContract; // Filter bar showFilterBar?: boolean; filters?: Filter[]; diff --git a/src/legacy/core_plugins/data/public/timefilter/index.ts b/src/legacy/core_plugins/data/public/timefilter/index.ts index 5582d91dceae3..17564801cf148 100644 --- a/src/legacy/core_plugins/data/public/timefilter/index.ts +++ b/src/legacy/core_plugins/data/public/timefilter/index.ts @@ -19,6 +19,7 @@ export { TimefilterService, TimefilterSetup } from './timefilter_service'; -export { Timefilter } from './timefilter'; -export { TimeHistory } from './time_history'; +export * from './types'; +export { Timefilter, TimefilterContract } from './timefilter'; +export { TimeHistory, TimeHistoryContract } from './time_history'; export { getTime } from './get_time'; diff --git a/src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.ts b/src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.ts index 23a05bc29a15b..27da80661dbce 100644 --- a/src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.ts +++ b/src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.ts @@ -20,7 +20,7 @@ import _ from 'lodash'; import { RefreshInterval } from 'src/plugins/data/public'; -import { InputTimeRange } from '../timefilter'; +import { InputTimeRange } from '../types'; const valueOf = function(o: any) { if (o) return o.valueOf(); diff --git a/src/legacy/core_plugins/data/public/timefilter/time_history.ts b/src/legacy/core_plugins/data/public/timefilter/time_history.ts index 98e759fb27a9d..ebbf561b815e1 100644 --- a/src/legacy/core_plugins/data/public/timefilter/time_history.ts +++ b/src/legacy/core_plugins/data/public/timefilter/time_history.ts @@ -52,3 +52,5 @@ export class TimeHistory { return this.history.get(); } } + +export type TimeHistoryContract = PublicMethodsOf; diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts index 47896189aa080..bb0047ecafe7c 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts @@ -17,26 +17,6 @@ * under the License. */ -import './timefilter.test.mocks'; - -jest.mock('ui/chrome', () => ({ - getBasePath: () => `/some/base/path`, - getUiSettingsClient: () => { - return { - get: (key: string) => { - switch (key) { - case 'timepicker:timeDefaults': - return { from: 'now-15m', to: 'now' }; - case 'timepicker:refreshIntervalDefaults': - return { pause: false, value: 0 }; - default: - throw new Error(`Unexpected config key: ${key}`); - } - }, - }; - }, -})); - jest.mock('./lib/parse_querystring', () => ({ parseQueryString: () => { return { @@ -50,10 +30,19 @@ jest.mock('./lib/parse_querystring', () => ({ import sinon from 'sinon'; import expect from '@kbn/expect'; import moment from 'moment'; -import { timefilter } from 'ui/timefilter'; +import { Timefilter } from './timefilter'; import { Subscription } from 'rxjs'; import { TimeRange, RefreshInterval } from 'src/plugins/data/public'; +import { timefilterServiceMock } from './timefilter_service.mock'; +const timefilterSetupMock = timefilterServiceMock.createSetupContract(); + +const timefilterConfig = { + timeDefaults: { from: 'now-15m', to: 'now' }, + refreshIntervalDefaults: { pause: false, value: 0 }, +}; +const timefilter = new Timefilter(timefilterConfig, timefilterSetupMock.history); + function stubNowTime(nowTime: any) { // @ts-ignore global.nowTime = nowTime; @@ -242,13 +231,13 @@ describe('isTimeRangeSelectorEnabled', () => { test('should emit updated when disabled', () => { timefilter.disableTimeRangeSelector(); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(false); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); expect(update.called).to.be(true); }); test('should emit updated when enabled', () => { timefilter.enableTimeRangeSelector(); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(true); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); expect(update.called).to.be(true); }); }); @@ -268,13 +257,13 @@ describe('isAutoRefreshSelectorEnabled', () => { test('should emit updated when disabled', () => { timefilter.disableAutoRefreshSelector(); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(false); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); expect(update.called).to.be(true); }); test('should emit updated when enabled', () => { timefilter.enableAutoRefreshSelector(); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(true); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); expect(update.called).to.be(true); }); }); diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts index b07060018f9d7..753161909f8ac 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts @@ -19,26 +19,13 @@ import _ from 'lodash'; import { Subject, BehaviorSubject } from 'rxjs'; -import moment, { Moment } from 'moment'; +import moment from 'moment'; import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; -import { IndexPattern } from 'src/legacy/core_plugins/data/public'; -import { TimeHistory } from './time_history'; +import { IndexPattern, TimeHistoryContract } from '../index'; import { areRefreshIntervalsDifferent, areTimeRangesDifferent } from './lib/diff_time_picker_vals'; import { parseQueryString } from './lib/parse_querystring'; import { calculateBounds, getTime } from './get_time'; - -export interface TimefilterConfig { - timeDefaults: TimeRange; - refreshIntervalDefaults: RefreshInterval; -} - -// Timefilter accepts moment input but always returns string output -export type InputTimeRange = - | TimeRange - | { - from: Moment; - to: Moment; - }; +import { TimefilterConfig, InputTimeRange } from './types'; export class Timefilter { // Fired when isTimeRangeSelectorEnabled \ isAutoRefreshSelectorEnabled are toggled @@ -53,38 +40,46 @@ export class Timefilter { private _time: TimeRange; private _refreshInterval!: RefreshInterval; - private _history: TimeHistory; + private _history: TimeHistoryContract; - public isTimeRangeSelectorEnabled: boolean = false; - public isAutoRefreshSelectorEnabled: boolean = false; + private isTimeRangeSelectorEnabled: boolean = false; + private isAutoRefreshSelectorEnabled: boolean = false; - constructor(config: TimefilterConfig, timeHistory: TimeHistory) { + constructor(config: TimefilterConfig, timeHistory: TimeHistoryContract) { this._history = timeHistory; this._time = config.timeDefaults; this.setRefreshInterval(config.refreshIntervalDefaults); } - getEnabledUpdated$ = () => { + public getIsTimeRangeSelectorEnabled() { + return this.isTimeRangeSelectorEnabled; + } + + public getIsAutoRefreshSelectorEnabled() { + return this.isAutoRefreshSelectorEnabled; + } + + public getEnabledUpdated$ = () => { return this.enabledUpdated$.asObservable(); }; - getTimeUpdate$ = () => { + public getTimeUpdate$ = () => { return this.timeUpdate$.asObservable(); }; - getRefreshIntervalUpdate$ = () => { + public getRefreshIntervalUpdate$ = () => { return this.refreshIntervalUpdate$.asObservable(); }; - getAutoRefreshFetch$ = () => { + public getAutoRefreshFetch$ = () => { return this.autoRefreshFetch$.asObservable(); }; - getFetch$ = () => { + public getFetch$ = () => { return this.fetch$.asObservable(); }; - getTime = (): TimeRange => { + public getTime = (): TimeRange => { const { from, to } = this._time; return { ...this._time, @@ -100,7 +95,7 @@ export class Timefilter { * @property {string|moment} time.from * @property {string|moment} time.to */ - setTime = (time: InputTimeRange) => { + public setTime = (time: InputTimeRange) => { // Object.assign used for partially composed updates const newTime = Object.assign(this.getTime(), time); if (areTimeRangesDifferent(this.getTime(), newTime)) { @@ -114,7 +109,7 @@ export class Timefilter { } }; - getRefreshInterval = () => { + public getRefreshInterval = () => { return _.clone(this._refreshInterval); }; @@ -124,7 +119,7 @@ export class Timefilter { * @property {number} time.value Refresh interval in milliseconds. Positive integer * @property {boolean} time.pause */ - setRefreshInterval = (refreshInterval: Partial) => { + public setRefreshInterval = (refreshInterval: Partial) => { const prevRefreshInterval = this.getRefreshInterval(); const newRefreshInterval = { ...prevRefreshInterval, ...refreshInterval }; // If the refresh interval is <= 0 handle that as a paused refresh @@ -149,39 +144,19 @@ export class Timefilter { } }; - toggleRefresh = () => { - this.setRefreshInterval({ - pause: !this._refreshInterval.pause, - value: this._refreshInterval.value, - }); - }; - - createFilter = (indexPattern: IndexPattern, timeRange: TimeRange) => { + public createFilter = (indexPattern: IndexPattern, timeRange: TimeRange) => { return getTime(indexPattern, timeRange ? timeRange : this._time, this.getForceNow()); }; - getBounds = () => { + public getBounds = () => { return this.calculateBounds(this._time); }; - getForceNow = () => { - const forceNow = parseQueryString().forceNow as string; - if (!forceNow) { - return; - } - - const ticks = Date.parse(forceNow); - if (isNaN(ticks)) { - throw new Error(`forceNow query parameter, ${forceNow}, can't be parsed by Date.parse`); - } - return new Date(ticks); - }; - - calculateBounds = (timeRange: TimeRange) => { + public calculateBounds = (timeRange: TimeRange) => { return calculateBounds(timeRange, { forceNow: this.getForceNow() }); }; - getActiveBounds = () => { + public getActiveBounds = () => { if (this.isTimeRangeSelectorEnabled) { return this.getBounds(); } @@ -190,7 +165,7 @@ export class Timefilter { /** * Show the time bounds selector part of the time filter */ - enableTimeRangeSelector = () => { + public enableTimeRangeSelector = () => { this.isTimeRangeSelectorEnabled = true; this.enabledUpdated$.next(true); }; @@ -198,7 +173,7 @@ export class Timefilter { /** * Hide the time bounds selector part of the time filter */ - disableTimeRangeSelector = () => { + public disableTimeRangeSelector = () => { this.isTimeRangeSelectorEnabled = false; this.enabledUpdated$.next(false); }; @@ -206,7 +181,7 @@ export class Timefilter { /** * Show the auto refresh part of the time filter */ - enableAutoRefreshSelector = () => { + public enableAutoRefreshSelector = () => { this.isAutoRefreshSelectorEnabled = true; this.enabledUpdated$.next(true); }; @@ -214,12 +189,31 @@ export class Timefilter { /** * Hide the auto refresh part of the time filter */ - disableAutoRefreshSelector = () => { + public disableAutoRefreshSelector = () => { this.isAutoRefreshSelectorEnabled = false; this.enabledUpdated$.next(false); }; - notifyShouldFetch = () => { + /** + * Added to allow search_poll to trigger an auto refresh event. + * Before this change, search_poll used to access a now private member of this instance. + */ + public notifyShouldFetch = () => { this.autoRefreshFetch$.next(); }; + + private getForceNow = () => { + const forceNow = parseQueryString().forceNow as string; + if (!forceNow) { + return; + } + + const ticks = Date.parse(forceNow); + if (isNaN(ticks)) { + throw new Error(`forceNow query parameter, ${forceNow}, can't be parsed by Date.parse`); + } + return new Date(ticks); + }; } + +export type TimefilterContract = PublicMethodsOf; diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts new file mode 100644 index 0000000000000..f777b0415b4b7 --- /dev/null +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts @@ -0,0 +1,76 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { TimefilterService, TimeHistoryContract, TimefilterContract } from '.'; + +export type TimefilterServiceClientContract = PublicMethodsOf; + +const createSetupContractMock = () => { + const timefilterMock: jest.Mocked = { + getIsAutoRefreshSelectorEnabled: jest.fn(), + getIsTimeRangeSelectorEnabled: jest.fn(), + getEnabledUpdated$: jest.fn(), + getTimeUpdate$: jest.fn(), + getRefreshIntervalUpdate$: jest.fn(), + getAutoRefreshFetch$: jest.fn(), + getFetch$: jest.fn(), + getTime: jest.fn(), + setTime: jest.fn(), + setRefreshInterval: jest.fn(), + getRefreshInterval: jest.fn(), + getActiveBounds: jest.fn(), + disableAutoRefreshSelector: jest.fn(), + disableTimeRangeSelector: jest.fn(), + enableAutoRefreshSelector: jest.fn(), + enableTimeRangeSelector: jest.fn(), + getBounds: jest.fn(), + calculateBounds: jest.fn(), + notifyShouldFetch: jest.fn(), + createFilter: jest.fn(), + }; + + const historyMock: jest.Mocked = { + add: jest.fn(), + get: jest.fn(), + }; + + const setupContract = { + timefilter: timefilterMock, + history: historyMock, + }; + + return setupContract; +}; + +const createMock = () => { + const mocked: jest.Mocked = { + setup: jest.fn(), + start: jest.fn(), + stop: jest.fn(), + }; + + mocked.setup.mockReturnValue(createSetupContractMock()); + return mocked; +}; + +export const timefilterServiceMock = { + create: createMock, + createSetupContract: createSetupContractMock, + createStartContract: createSetupContractMock, +}; diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts index a799aa904ef2a..96c490a195d3d 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts @@ -18,8 +18,7 @@ */ import { UiSettingsClientContract } from 'src/core/public'; -import { Timefilter } from './timefilter'; -import { TimeHistory } from './index'; +import { TimeHistory, Timefilter, TimeHistoryContract, TimefilterContract } from './index'; /** * Filter Service @@ -31,7 +30,7 @@ export interface TimeFilterServiceDependencies { } export class TimefilterService { - public setup({ uiSettings }: TimeFilterServiceDependencies) { + public setup({ uiSettings }: TimeFilterServiceDependencies): TimefilterSetup { const timefilterConfig = { timeDefaults: uiSettings.get('timepicker:timeDefaults'), refreshIntervalDefaults: uiSettings.get('timepicker:refreshIntervalDefaults'), @@ -55,4 +54,7 @@ export class TimefilterService { } /** @public */ -export type TimefilterSetup = ReturnType; +export interface TimefilterSetup { + timefilter: TimefilterContract; + history: TimeHistoryContract; +} diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.mocks.ts b/src/legacy/core_plugins/data/public/timefilter/types.ts similarity index 63% rename from src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.mocks.ts rename to src/legacy/core_plugins/data/public/timefilter/types.ts index add054c492510..879776cee178e 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/lib/__tests__/change_time_filter.test.mocks.ts +++ b/src/legacy/core_plugins/data/public/timefilter/types.ts @@ -16,13 +16,23 @@ * specific language governing permissions and limitations * under the License. */ +import { Moment } from 'moment'; +import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; -import { chromeServiceMock } from '../../../../../../../../core/public/mocks'; +export interface TimefilterConfig { + timeDefaults: TimeRange; + refreshIntervalDefaults: RefreshInterval; +} -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +// Timefilter accepts moment input but always returns string output +export type InputTimeRange = + | TimeRange + | { + from: Moment; + to: Moment; + }; + +export interface TimeRangeBounds { + min: Moment | undefined; + max: Moment | undefined; +} diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts index be76f498c8911..d7ea9766749e1 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts @@ -24,36 +24,27 @@ import { getAppStateMock, getSavedDashboardMock } from './__tests__'; import { AppStateClass } from 'ui/state_management/app_state'; import { DashboardAppState } from './types'; import { TimeRange } from 'src/plugins/data/public'; -import { Timefilter } from 'ui/timefilter'; import { ViewMode } from '../../../embeddable_api/public/np_ready/public'; +import { dataPluginMock } from '../../../../core_plugins/data/public/mocks'; +const dataSetupMock = dataPluginMock.createSetup(); + describe('DashboardState', function() { let dashboardState: DashboardStateManager; const savedDashboard = getSavedDashboardMock(); let mockTime: TimeRange = { to: 'now', from: 'now-15m' }; - const mockTimefilter: Partial = { - setTime(time) { - mockTime = time as TimeRange; - }, - getTime() { - return mockTime; - }, - disableAutoRefreshSelector: jest.fn(), - setRefreshInterval: jest.fn(), - getRefreshInterval: jest.fn(), - disableTimeRangeSelector: jest.fn(), - enableAutoRefreshSelector: jest.fn(), - getActiveBounds: jest.fn(), - enableTimeRangeSelector: () => {}, - isAutoRefreshSelectorEnabled: true, - isTimeRangeSelectorEnabled: true, - getAutoRefreshFetch$: jest.fn(), - getEnabledUpdated$: jest.fn(), - getRefreshIntervalUpdate$: jest.fn(), - getFetch$: jest.fn(), - getTimeUpdate$: jest.fn(), - }; + const mockTimefilter = dataSetupMock.timefilter!.timefilter; + + mockTimefilter.setTime.mockImplementation((time: any) => { + mockTime = { + from: time.from.toISOString(), + to: time.to.toISOString(), + }; + }); + mockTimefilter.getTime.mockImplementation(() => { + return mockTime; + }); function initDashboardState() { dashboardState = new DashboardStateManager({ @@ -73,7 +64,7 @@ describe('DashboardState', function() { mockTime.to = '2015-09-29 06:31:44.000'; initDashboardState(); - dashboardState.syncTimefilterWithDashboard(mockTimefilter as Timefilter); + dashboardState.syncTimefilterWithDashboard(mockTimefilter); expect(mockTime.to).toBe('now/w'); expect(mockTime.from).toBe('now/w'); @@ -88,7 +79,7 @@ describe('DashboardState', function() { mockTime.to = '2015-09-29 06:31:44.000'; initDashboardState(); - dashboardState.syncTimefilterWithDashboard(mockTimefilter as Timefilter); + dashboardState.syncTimefilterWithDashboard(mockTimefilter); expect(mockTime.to).toBe('now'); expect(mockTime.from).toBe('now-13d'); @@ -103,7 +94,7 @@ describe('DashboardState', function() { mockTime.to = 'now/w'; initDashboardState(); - dashboardState.syncTimefilterWithDashboard(mockTimefilter as Timefilter); + dashboardState.syncTimefilterWithDashboard(mockTimefilter); expect(mockTime.to).toBe(savedDashboard.timeTo); expect(mockTime.from).toBe(savedDashboard.timeFrom); diff --git a/src/legacy/core_plugins/kibana/public/doc/__tests__/doc.js b/src/legacy/core_plugins/kibana/public/doc/__tests__/doc.js index fff8b800337eb..14106f5379211 100644 --- a/src/legacy/core_plugins/kibana/public/doc/__tests__/doc.js +++ b/src/legacy/core_plugins/kibana/public/doc/__tests__/doc.js @@ -139,8 +139,8 @@ describe('Doc app controller', function () { it('should disable the time filter', function (done) { init(); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(false); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(false); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); done(); }); diff --git a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx index 3e31cbd0d84c1..c35149aa47c8d 100644 --- a/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx +++ b/src/legacy/core_plugins/kibana_react/public/top_nav_menu/top_nav_menu.test.tsx @@ -25,6 +25,9 @@ import { shallowWithIntl } from 'test_utils/enzyme_helpers'; import { coreMock } from '../../../../../core/public/mocks'; const startMock = coreMock.createStart(); +import { timefilterServiceMock } from '../../../../core_plugins/data/public/timefilter/timefilter_service.mock'; +const timefilterSetupMock = timefilterServiceMock.createSetupContract(); + jest.mock('../../../../core_plugins/data/public', () => { return { SearchBar: () =>
, @@ -57,6 +60,7 @@ describe('TopNavMenu', () => { const component = shallowWithIntl( @@ -69,6 +73,7 @@ describe('TopNavMenu', () => { const component = shallowWithIntl( { const component = shallowWithIntl( { const component = shallowWithIntl( & { name: string; uiSettings: UiSettingsClientContract; savedObjectsClient: SavedObjectsClientContract; - timeHistory: TimeHistory; + timeHistory: TimeHistoryContract; config?: TopNavMenuData[]; showSearchBar?: boolean; }; diff --git a/src/legacy/ui/public/kbn_top_nav/kbn_top_nav.js b/src/legacy/ui/public/kbn_top_nav/kbn_top_nav.js index c67a337150908..4465b5458e0ac 100644 --- a/src/legacy/ui/public/kbn_top_nav/kbn_top_nav.js +++ b/src/legacy/ui/public/kbn_top_nav/kbn_top_nav.js @@ -49,7 +49,7 @@ module.directive('kbnTopNav', () => { const localStorage = new Storage(window.localStorage); child.setAttribute('http', 'http'); child.setAttribute('store', 'store'); - child.setAttribute('history', 'history'); + child.setAttribute('time-history', 'timeHistory'); child.setAttribute('ui-settings', 'uiSettings'); child.setAttribute('saved-objects-client', 'savedObjectsClient'); @@ -60,7 +60,7 @@ module.directive('kbnTopNav', () => { $scope.store = localStorage; $scope.http = npSetup.core.http; $scope.uiSettings = npSetup.core.uiSettings; - $scope.history = data.timefilter.history; + $scope.timeHistory = data.timefilter.history; $scope.savedObjectsClient = chrome.getSavedObjectsClient(); // Watch config changes @@ -105,7 +105,7 @@ module.directive('kbnTopNavHelper', (reactDirective) => { ['uiSettings', { watchDepth: 'reference' }], ['savedObjectsClient', { watchDepth: 'reference' }], ['intl', { watchDepth: 'reference' }], - ['history', { watchDepth: 'reference' }], + ['timeHistory', { watchDepth: 'reference' }], ['store', { watchDepth: 'reference' }], ['http', { watchDepth: 'reference' }], diff --git a/src/legacy/ui/public/timefilter/index.ts b/src/legacy/ui/public/timefilter/index.ts index e40c9799a28e6..f9c19d1f067fc 100644 --- a/src/legacy/ui/public/timefilter/index.ts +++ b/src/legacy/ui/public/timefilter/index.ts @@ -18,11 +18,18 @@ */ import uiRoutes from 'ui/routes'; +import { TimefilterContract, TimeHistoryContract } from 'src/legacy/core_plugins/data/public'; import { registerTimefilterWithGlobalState } from './setup_router'; - import { setup as data } from '../../../core_plugins/data/public/legacy'; -export { getTime } from '../../../core_plugins/data/public'; +export { + getTime, + InputTimeRange, + TimeHistoryContract, + TimefilterContract, +} from '../../../core_plugins/data/public'; +export type Timefilter = TimefilterContract; +export type TimeHistory = TimeHistoryContract; export const timeHistory = data.timefilter.history; export const timefilter = data.timefilter.timefilter; diff --git a/src/legacy/ui/public/timefilter/setup_router.ts b/src/legacy/ui/public/timefilter/setup_router.ts index cbd03df455da3..3c81fde89149b 100644 --- a/src/legacy/ui/public/timefilter/setup_router.ts +++ b/src/legacy/ui/public/timefilter/setup_router.ts @@ -23,7 +23,7 @@ import moment from 'moment'; import { subscribeWithScope } from 'ui/utils/subscribe_with_scope'; import chrome from 'ui/chrome'; import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; -import { Timefilter } from '../../../core_plugins/data/public/timefilter'; +import { TimefilterContract } from '../../../core_plugins/data/public/timefilter'; // TODO // remove everything underneath once globalState is no longer an angular service @@ -46,7 +46,7 @@ export function getTimefilterConfig() { // This function is exposed for applications that do not use uiRoutes like APM // Kibana issue https://github.com/elastic/kibana/issues/19110 tracks the removal of this dependency on uiRouter export const registerTimefilterWithGlobalState = _.once( - (timefilter: Timefilter, globalState: any, $rootScope: IScope) => { + (timefilter: TimefilterContract, globalState: any, $rootScope: IScope) => { // settings have to be re-fetched here, to make sure that settings changed by overrideLocalDefault are taken into account. const config = getTimefilterConfig(); timefilter.setTime(_.defaults(globalState.time || {}, config.timeDefaults)); diff --git a/src/legacy/ui/public/vis/vis_filters/brush_event.test.js b/src/legacy/ui/public/vis/vis_filters/brush_event.test.js index b064022819f56..b3d5d0f94364d 100644 --- a/src/legacy/ui/public/vis/vis_filters/brush_event.test.js +++ b/src/legacy/ui/public/vis/vis_filters/brush_event.test.js @@ -17,32 +17,27 @@ * under the License. */ -import './brush_event.test.mocks'; - -jest.mock('ui/chrome', - () => ({ - getBasePath: () => `/some/base/path`, - getUiSettingsClient: () => { - return { - get: (key) => { - switch (key) { - case 'timepicker:timeDefaults': - return { from: 'now-15m', to: 'now' }; - case 'timepicker:refreshIntervalDefaults': - return { pause: false, value: 0 }; - default: - throw new Error(`Unexpected config key: ${key}`); - } - } - }; - }, - }), { virtual: true }); import _ from 'lodash'; import moment from 'moment'; import expect from '@kbn/expect'; import { onBrushEvent } from './brush_event'; -import { timefilter } from 'ui/timefilter'; + +import { dataPluginMock } from '../../../../core_plugins/data/public/mocks'; +const dataSetupMock = dataPluginMock.createSetup(); +const { timefilter } = dataSetupMock.timefilter; + +let _time = undefined; +timefilter.setTime.mockImplementation((time) => { + _time = { + from: time.from.toISOString(), + to: time.to.toISOString(), + }; +}); +timefilter.getTime.mockImplementation(() => { + return _time; +}); + describe('brushEvent', () => { const DAY_IN_MS = 24 * 60 * 60 * 1000; @@ -98,7 +93,7 @@ describe('brushEvent', () => { test('ignores event when data.xAxisField not provided', () => { const event = _.cloneDeep(baseEvent); - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state) .not.have.property('$newFilters'); }); @@ -120,7 +115,7 @@ describe('brushEvent', () => { test('by ignoring the event when range spans zero time', () => { const event = _.cloneDeep(dateEvent); event.range = [JAN_01_2014, JAN_01_2014]; - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state) .not.have.property('$newFilters'); }); @@ -128,7 +123,7 @@ describe('brushEvent', () => { test('by updating the timefilter', () => { const event = _.cloneDeep(dateEvent); event.range = [JAN_01_2014, JAN_01_2014 + DAY_IN_MS]; - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); const { from, to } = timefilter.getTime(); // Set to a baseline timezone for comparison. expect(from).to.be(new Date(JAN_01_2014).toISOString()); @@ -155,7 +150,7 @@ describe('brushEvent', () => { const rangeBegin = JAN_01_2014; const rangeEnd = rangeBegin + DAY_IN_MS; event.range = [rangeBegin, rangeEnd]; - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state) .to.have.property('$newFilters'); expect($state.filters.length) @@ -189,14 +184,14 @@ describe('brushEvent', () => { test('by ignoring the event when range does not span at least 2 values', () => { const event = _.cloneDeep(numberEvent); event.range = [1]; - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state).not.have.property('$newFilters'); }); test('by creating a new filter', () => { const event = _.cloneDeep(numberEvent); event.range = [1, 2, 3, 4]; - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state) .to.have.property('$newFilters'); expect($state.filters.length) @@ -219,7 +214,7 @@ describe('brushEvent', () => { }, range: { gte: 1, lt: 4 } }); - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state) .not.have.property('$newFilters'); expect($state.filters.length) @@ -247,7 +242,7 @@ describe('brushEvent', () => { } } }); - onBrushEvent(event, $state); + onBrushEvent(timefilter, event, $state); expect($state) .not.have.property('$newFilters'); expect($state.filters.length) diff --git a/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts b/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts index 4dc0a2d5db629..254815671c764 100644 --- a/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts +++ b/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.test.ts @@ -16,6 +16,8 @@ * specific language governing permissions and limitations * under the License. */ +jest.mock('ui/new_platform'); + import { mockDataLoaderFetch, timefilter } from './embedded_visualize_handler.test.mocks'; import _ from 'lodash'; @@ -27,7 +29,11 @@ import { Inspector } from '../../inspector'; import { EmbeddedVisualizeHandler, RequestHandlerParams } from './embedded_visualize_handler'; import { AggConfigs } from 'ui/vis/agg_configs'; -jest.mock('ui/new_platform'); +jest.mock('plugins/interpreter/interpreter', () => ({ + getInterpreter: () => { + return Promise.resolve(); + }, +})); jest.mock('../../../../core_plugins/interpreter/public/registries', () => ({ registries: { diff --git a/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx b/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx index f32632cd321ae..fe2d26be24067 100644 --- a/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx +++ b/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx @@ -60,10 +60,10 @@ export const TopNav: FC = () => { const [time, setTime] = useState(timefilter.getTime()); const [recentlyUsedRanges, setRecentlyUsedRanges] = useState(getRecentlyUsedRanges()); const [isAutoRefreshSelectorEnabled, setIsAutoRefreshSelectorEnabled] = useState( - timefilter.isAutoRefreshSelectorEnabled + timefilter.getIsAutoRefreshSelectorEnabled() ); const [isTimeRangeSelectorEnabled, setIsTimeRangeSelectorEnabled] = useState( - timefilter.isTimeRangeSelectorEnabled + timefilter.getIsTimeRangeSelectorEnabled() ); const dateFormat = chrome.getUiSettingsClient().get('dateFormat'); @@ -87,8 +87,8 @@ export const TopNav: FC = () => { function timefilterUpdateListener() { setTime(timefilter.getTime()); setRefreshInterval(timefilter.getRefreshInterval()); - setIsAutoRefreshSelectorEnabled(timefilter.isAutoRefreshSelectorEnabled); - setIsTimeRangeSelectorEnabled(timefilter.isTimeRangeSelectorEnabled); + setIsAutoRefreshSelectorEnabled(timefilter.getIsAutoRefreshSelectorEnabled()); + setIsTimeRangeSelectorEnabled(timefilter.getIsTimeRangeSelectorEnabled()); } function updateFilter({ start, end }: Duration) { diff --git a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js index 22949286f50e3..e1f7095b46287 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js @@ -124,8 +124,8 @@ describe('MonitoringViewBaseController', function () { describe('time filter', () => { it('enables timepicker and auto refresh #1', () => { - expect(timefilter.isTimeRangeSelectorEnabled).to.be(true); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(true); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); }); it('enables timepicker and auto refresh #2', () => { @@ -135,8 +135,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(true); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(true); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); }); it('disables timepicker and enables auto refresh', () => { @@ -146,8 +146,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(false); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(true); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); }); it('enables timepicker and disables auto refresh', () => { @@ -157,8 +157,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(true); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(false); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); }); it('disables timepicker and auto refresh', () => { @@ -171,8 +171,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.isTimeRangeSelectorEnabled).to.be(false); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(false); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); }); it('disables timepicker and auto refresh', (done) => { diff --git a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js index 5c2791d781f4d..7fc36f67370ad 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js @@ -76,8 +76,8 @@ describe('MonitoringViewBaseTableController', function () { }); it('enables timepicker', () => { - expect(timefilter.isTimeRangeSelectorEnabled).to.be(true); - expect(timefilter.isAutoRefreshSelectorEnabled).to.be(true); + expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); }); it('sets page title', () => { From 8bbf29ab8c45713e70d9ed1156b75f79f3409b41 Mon Sep 17 00:00:00 2001 From: Liza K Date: Wed, 4 Sep 2019 19:45:59 +0300 Subject: [PATCH 04/22] Fix jest test --- .../core_plugins/data/public/timefilter/time_history.ts | 2 +- .../kibana/public/dashboard/dashboard_state.test.ts | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/legacy/core_plugins/data/public/timefilter/time_history.ts b/src/legacy/core_plugins/data/public/timefilter/time_history.ts index ebbf561b815e1..7dcd5843ae530 100644 --- a/src/legacy/core_plugins/data/public/timefilter/time_history.ts +++ b/src/legacy/core_plugins/data/public/timefilter/time_history.ts @@ -22,7 +22,7 @@ import { TimeRange } from 'src/plugins/data/public'; import { PersistedLog } from 'ui/persisted_log'; export class TimeHistory { - private history: PersistedLog; + private history: PersistedLog; constructor() { const historyOptions = { diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts index d7ea9766749e1..428b05950dfae 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts @@ -27,6 +27,7 @@ import { TimeRange } from 'src/plugins/data/public'; import { ViewMode } from '../../../embeddable_api/public/np_ready/public'; import { dataPluginMock } from '../../../../core_plugins/data/public/mocks'; +import { InputTimeRange } from 'ui/timefilter'; const dataSetupMock = dataPluginMock.createSetup(); describe('DashboardState', function() { @@ -36,11 +37,8 @@ describe('DashboardState', function() { let mockTime: TimeRange = { to: 'now', from: 'now-15m' }; const mockTimefilter = dataSetupMock.timefilter!.timefilter; - mockTimefilter.setTime.mockImplementation((time: any) => { - mockTime = { - from: time.from.toISOString(), - to: time.to.toISOString(), - }; + mockTimefilter.setTime.mockImplementation((time: InputTimeRange) => { + mockTime = time as TimeRange; }); mockTimefilter.getTime.mockImplementation(() => { return mockTime; From 889101bb65673a677dc9aa19d0173ad72a4e518d Mon Sep 17 00:00:00 2001 From: Liza K Date: Thu, 5 Sep 2019 12:05:47 +0300 Subject: [PATCH 05/22] Fixed bad import path --- src/legacy/ui/public/timefilter/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/ui/public/timefilter/index.ts b/src/legacy/ui/public/timefilter/index.ts index f9c19d1f067fc..d579148bd755a 100644 --- a/src/legacy/ui/public/timefilter/index.ts +++ b/src/legacy/ui/public/timefilter/index.ts @@ -18,7 +18,7 @@ */ import uiRoutes from 'ui/routes'; -import { TimefilterContract, TimeHistoryContract } from 'src/legacy/core_plugins/data/public'; +import { TimefilterContract, TimeHistoryContract } from '../../../core_plugins/data/public'; import { registerTimefilterWithGlobalState } from './setup_router'; import { setup as data } from '../../../core_plugins/data/public/legacy'; From 7a9f75f47b50342c21e9e17d1247a65dabe55bd7 Mon Sep 17 00:00:00 2001 From: Liza K Date: Thu, 5 Sep 2019 15:37:12 +0300 Subject: [PATCH 06/22] updated xpack test --- .../public/views/__tests__/base_table_controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js index 7fc36f67370ad..777874e0881e1 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js @@ -22,8 +22,8 @@ describe('MonitoringViewBaseTableController', function () { timefilter = { enableTimeRangeSelector: spy(), enableAutoRefreshSelector: spy(), - isTimeRangeSelectorEnabled: true, - isAutoRefreshSelectorEnabled: true + getIsTimeRangeSelectorEnabled: () => true, + getIsAutoRefreshSelectorEnabled: () => true, }; titleService = spy(); executorService = { From 926f948eeaaa06477824fcc12b7ad4eb5816abfb Mon Sep 17 00:00:00 2001 From: Liza K Date: Thu, 5 Sep 2019 17:23:14 +0300 Subject: [PATCH 07/22] make history optional on query bar --- .../components/query_bar_top_row.tsx | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx index 1c9824c92b09f..e12c079a1e033 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx @@ -71,7 +71,7 @@ interface Props { uiSettings: UiSettingsClientContract; savedObjectsClient: SavedObjectsClientContract; http: HttpServiceBase; - timeHistory: TimeHistoryContract; + timeHistory?: TimeHistoryContract; } interface State { @@ -151,7 +151,10 @@ export class QueryBarTopRowUI extends Component { public onSubmit = ({ query, dateRange }: { query?: Query; dateRange: DateRange }) => { this.handleLuceneSyntaxWarning(); - this.props.timeHistory.add(dateRange); + + if (this.props.timeHistory) { + this.props.timeHistory.add(dateRange); + } this.props.onSubmit({ query, dateRange }); }; @@ -263,14 +266,17 @@ export class QueryBarTopRowUI extends Component { return null; } - const recentlyUsedRanges = this.props.timeHistory - .get() - .map(({ from, to }: { from: string; to: string }) => { - return { - start: from, - end: to, - }; - }); + let recentlyUsedRanges; + if (this.props.timeHistory) { + recentlyUsedRanges = this.props.timeHistory + .get() + .map(({ from, to }: { from: string; to: string }) => { + return { + start: from, + end: to, + }; + }); + } const commonlyUsedRanges = this.props.uiSettings .get('timepicker:quickRanges') From f0ef9698b0bee05427aa43bb4a0fe394edcd5b00 Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 12:58:41 +0300 Subject: [PATCH 08/22] Fixed chart_utils test --- .../ml/public/util/chart_utils.test.js | 39 ++++++++++--------- .../ml/public/util/chart_utils.test.mocks.ts | 15 ------- 2 files changed, 21 insertions(+), 33 deletions(-) delete mode 100644 x-pack/legacy/plugins/ml/public/util/chart_utils.test.mocks.ts diff --git a/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js b/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js index eea6691c944b8..3b2e5161ade14 100644 --- a/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js +++ b/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js @@ -4,9 +4,29 @@ * you may not use this file except in compliance with the Elastic License. */ -import './chart_utils.test.mocks'; import seriesConfig from '../explorer/explorer_charts/__mocks__/mock_series_config_filebeat'; +jest.mock('ui/timefilter', () => { + const dateMath = require('@elastic/datemath'); + const { dataPluginMock } = require('../../../../../../src/legacy/core_plugins/data/public/mocks'); + const dataSetup = dataPluginMock.createSetup(); + const { timefilter } = dataSetup.timefilter; + let _time = undefined; + timefilter.setTime.mockImplementation((time) => { + _time = time; + }); + timefilter.getActiveBounds.mockImplementation(() => { + return { + min: dateMath.parse(_time.from), + max: dateMath.parse(_time.to), + }; + }); + return { + timefilter, + }; +}); +import { timefilter } from 'ui/timefilter'; + // A copy of these mocks for ui/chrome and ui/timefilter are also // used in explorer_charts_container.test.js. // TODO: Refactor the involved tests to avoid this duplication @@ -15,20 +35,6 @@ jest.mock('ui/chrome', getBasePath: () => { return ''; }, - getUiSettingsClient: () => { - return { - get: (key) => { - switch (key) { - case 'timepicker:timeDefaults': - return { from: 'now-15m', to: 'now', mode: 'quick' }; - case 'timepicker:refreshIntervalDefaults': - return { pause: false, value: 0 }; - default: - throw new Error(`Unexpected config key: ${key}`); - } - } - }; - }, }), { virtual: true }); import d3 from 'd3'; @@ -36,7 +42,6 @@ import moment from 'moment'; import { mount } from 'enzyme'; import React from 'react'; -import { timefilter } from 'ui/timefilter'; import { getExploreSeriesLink, @@ -46,8 +51,6 @@ import { removeLabelOverlap } from './chart_utils'; -timefilter.enableTimeRangeSelector(); -timefilter.enableAutoRefreshSelector(); timefilter.setTime({ from: moment(seriesConfig.selectedEarliest).toISOString(), to: moment(seriesConfig.selectedLatest).toISOString() diff --git a/x-pack/legacy/plugins/ml/public/util/chart_utils.test.mocks.ts b/x-pack/legacy/plugins/ml/public/util/chart_utils.test.mocks.ts deleted file mode 100644 index c7c7960b72d6e..0000000000000 --- a/x-pack/legacy/plugins/ml/public/util/chart_utils.test.mocks.ts +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { chromeServiceMock } from '../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); From 8c21433dd94aad9c74e2e22f4e6eaceae416df76 Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:04:09 +0300 Subject: [PATCH 09/22] Fixed page.test by mocking timefilter --- .../pages/transform_management/page.test.mocks.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/data_frame/pages/transform_management/page.test.mocks.ts b/x-pack/legacy/plugins/ml/public/data_frame/pages/transform_management/page.test.mocks.ts index fd3b04b2332d8..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/data_frame/pages/transform_management/page.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/data_frame/pages/transform_management/page.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From c433005ddc92e420554f31d44b333a66d3b13e29 Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:05:44 +0300 Subject: [PATCH 10/22] Fixed explorer_charts_container_service test by mocking timefilter --- .../explorer_charts_container_service.test.mocks.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container_service.test.mocks.ts b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container_service.test.mocks.ts index d3d1305fa6ffa..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container_service.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container_service.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From 0ac416101e96eee445dffe422fd83fa43481621a Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:06:48 +0300 Subject: [PATCH 11/22] Fixed explorer_chart_distribution test by mocking timefilter --- .../explorer_chart_distribution.test.mocks.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_distribution.test.mocks.ts b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_distribution.test.mocks.ts index d3d1305fa6ffa..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_distribution.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_distribution.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From 480201083cc30c25a4dc5adc0a14008e3d2cded2 Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:07:55 +0300 Subject: [PATCH 12/22] Fixed explorer_chart_single_metric test by mocking timefilter --- .../explorer_chart_single_metric.test.mocks.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_single_metric.test.mocks.ts b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_single_metric.test.mocks.ts index d3d1305fa6ffa..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_single_metric.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_chart_single_metric.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From f95ca349171d4ed6bcb5c7315000bf0c93bdbecb Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:19:00 +0300 Subject: [PATCH 13/22] Fixed explorer_charts_container test by mocking timefilter and removing some unneeded initialization --- .../explorer_charts_container.test.js | 24 ------------------- .../explorer_charts_container.test.mocks.ts | 12 +++------- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.js b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.js index 6b18a761e5642..0c410f418b2af 100644 --- a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.js +++ b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.js @@ -37,32 +37,8 @@ jest.mock('ui/chrome', getBasePath: () => { return ''; }, - getUiSettingsClient: () => { - return { - get: (key) => { - switch (key) { - case 'timepicker:timeDefaults': - return { from: 'now-15m', to: 'now', mode: 'quick' }; - case 'timepicker:refreshIntervalDefaults': - return { pause: false, value: 0 }; - default: - throw new Error(`Unexpected config key: ${key}`); - } - } - }; - }, }), { virtual: true }); -import moment from 'moment'; -import { timefilter } from 'ui/timefilter'; - -timefilter.enableTimeRangeSelector(); -timefilter.enableAutoRefreshSelector(); -timefilter.setTime({ - from: moment(seriesConfig.selectedEarliest).toISOString(), - to: moment(seriesConfig.selectedLatest).toISOString() -}); - import { shallowWithIntl, mountWithIntl } from 'test_utils/enzyme_helpers'; import React from 'react'; diff --git a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.mocks.ts b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.mocks.ts index d3d1305fa6ffa..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/explorer/explorer_charts/explorer_charts_container.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From c3332c340023da312606477ef9b1fc51346e2064 Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:20:18 +0300 Subject: [PATCH 14/22] Fixed explorer_swimlane tesy by mocking timefilter --- .../public/explorer/explorer_swimlane.test.mocks.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/explorer/explorer_swimlane.test.mocks.ts b/x-pack/legacy/plugins/ml/public/explorer/explorer_swimlane.test.mocks.ts index c7c7960b72d6e..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/explorer/explorer_swimlane.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/explorer/explorer_swimlane.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From c6a8eda5c640039c25fe286b1c1cfb272181c32f Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:22:22 +0300 Subject: [PATCH 15/22] Added missing functions to uiTimefilterMock to fix top_nav test --- .../legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts index e6e51f0bdecf4..89e4981c45081 100644 --- a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts +++ b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts @@ -37,6 +37,12 @@ const time = { }; export const uiTimefilterMock = { + getIsAutoRefreshSelectorEnabled() { + return this.isAutoRefreshSelectorEnabled; + }, + getIsTimeRangeSelectorEnabled() { + return this.isTimeRangeSelectorEnabled; + }, enableAutoRefreshSelector() { this.isAutoRefreshSelectorEnabled = true; }, From c62f757400969b9e4d3e9b131f7aed28007d724b Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 15 Sep 2019 13:24:07 +0300 Subject: [PATCH 16/22] Fixed timeseries_chart test by mocking timefilter --- .../timeseries_chart/timeseries_chart.test.mocks.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.test.mocks.ts b/x-pack/legacy/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.test.mocks.ts index fd3b04b2332d8..46178a7d02977 100644 --- a/x-pack/legacy/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.test.mocks.ts +++ b/x-pack/legacy/plugins/ml/public/timeseriesexplorer/components/timeseries_chart/timeseries_chart.test.mocks.ts @@ -4,12 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { chromeServiceMock } from '../../../../../../../../src/core/public/mocks'; - -jest.doMock('ui/new_platform', () => ({ - npStart: { - core: { - chrome: chromeServiceMock.createStartContract(), - }, - }, -})); +jest.mock('ui/timefilter', () => { + return {}; +}); From a9b4bed964a0d33ae9844608278af0b07bdf2c59 Mon Sep 17 00:00:00 2001 From: Liza K Date: Mon, 23 Sep 2019 13:50:36 +0300 Subject: [PATCH 17/22] remove bad merge --- .../core_plugins/data/public/timefilter/timefilter.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts index 609afe4e8adeb..7b291f2c64d59 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts @@ -144,13 +144,6 @@ export class Timefilter { } }; - public toggleRefresh = () => { - this.setRefreshInterval({ - pause: !this._refreshInterval.pause, - value: this._refreshInterval.value, - }); - }; - public createFilter = (indexPattern: IndexPattern, timeRange?: TimeRange) => { return getTime(indexPattern, timeRange ? timeRange : this._time, this.getForceNow()); }; From 587c6c5bc5557d7877d05a0cbff8f41b0f303bc9 Mon Sep 17 00:00:00 2001 From: Liza K Date: Mon, 23 Sep 2019 14:33:08 +0300 Subject: [PATCH 18/22] mock field formats --- .../kibana/public/dashboard/dashboard_state.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts index 428b05950dfae..d7847c8338cd2 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts @@ -25,9 +25,15 @@ import { AppStateClass } from 'ui/state_management/app_state'; import { DashboardAppState } from './types'; import { TimeRange } from 'src/plugins/data/public'; import { ViewMode } from '../../../embeddable_api/public/np_ready/public'; +import { InputTimeRange } from 'ui/timefilter'; + +jest.mock('ui/registry/field_formats', () => ({ + fieldFormats: { + getDefaultInstance: jest.fn(), + }, +})); import { dataPluginMock } from '../../../../core_plugins/data/public/mocks'; -import { InputTimeRange } from 'ui/timefilter'; const dataSetupMock = dataPluginMock.createSetup(); describe('DashboardState', function() { From fd83ebecaa814c13f5d20bd724c2ae953caa02b7 Mon Sep 17 00:00:00 2001 From: Liza K Date: Mon, 23 Sep 2019 16:54:56 +0300 Subject: [PATCH 19/22] mock field formats --- x-pack/legacy/plugins/ml/public/util/chart_utils.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js b/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js index 3b2e5161ade14..aaec5e412c4cb 100644 --- a/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js +++ b/x-pack/legacy/plugins/ml/public/util/chart_utils.test.js @@ -6,6 +6,12 @@ import seriesConfig from '../explorer/explorer_charts/__mocks__/mock_series_config_filebeat'; +jest.mock('ui/registry/field_formats', () => ({ + fieldFormats: { + getDefaultInstance: jest.fn(), + }, +})); + jest.mock('ui/timefilter', () => { const dateMath = require('@elastic/datemath'); const { dataPluginMock } = require('../../../../../../src/legacy/core_plugins/data/public/mocks'); From e6ea811cb320d5fbe690b57879d2e96eb1099352 Mon Sep 17 00:00:00 2001 From: Liza K Date: Tue, 24 Sep 2019 12:16:45 +0300 Subject: [PATCH 20/22] rename timefilter boolean getters --- .../data/public/timefilter/timefilter.test.ts | 8 ++++---- .../data/public/timefilter/timefilter.ts | 20 +++++++++---------- .../timefilter/timefilter_service.mock.ts | 4 ++-- .../navigation_menu/top_nav/top_nav.tsx | 8 ++++---- .../ml/public/contexts/ui/__mocks__/mocks.ts | 4 ++-- .../public/views/__tests__/base_controller.js | 20 +++++++++---------- .../views/__tests__/base_table_controller.js | 8 ++++---- 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts index bb0047ecafe7c..0c6b0faf3e397 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts @@ -231,13 +231,13 @@ describe('isTimeRangeSelectorEnabled', () => { test('should emit updated when disabled', () => { timefilter.disableTimeRangeSelector(); - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(false); expect(update.called).to.be(true); }); test('should emit updated when enabled', () => { timefilter.enableTimeRangeSelector(); - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(true); expect(update.called).to.be(true); }); }); @@ -257,13 +257,13 @@ describe('isAutoRefreshSelectorEnabled', () => { test('should emit updated when disabled', () => { timefilter.disableAutoRefreshSelector(); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(false); expect(update.called).to.be(true); }); test('should emit updated when enabled', () => { timefilter.enableAutoRefreshSelector(); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(true); expect(update.called).to.be(true); }); }); diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts index 7b291f2c64d59..c08ea9043da92 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts @@ -42,8 +42,8 @@ export class Timefilter { private _refreshInterval!: RefreshInterval; private _history: TimeHistoryContract; - private isTimeRangeSelectorEnabled: boolean = false; - private isAutoRefreshSelectorEnabled: boolean = false; + private _isTimeRangeSelectorEnabled: boolean = false; + private _isAutoRefreshSelectorEnabled: boolean = false; constructor(config: TimefilterConfig, timeHistory: TimeHistoryContract) { this._history = timeHistory; @@ -51,12 +51,12 @@ export class Timefilter { this.setRefreshInterval(config.refreshIntervalDefaults); } - public getIsTimeRangeSelectorEnabled() { - return this.isTimeRangeSelectorEnabled; + public isTimeRangeSelectorEnabled() { + return this._isTimeRangeSelectorEnabled; } - public getIsAutoRefreshSelectorEnabled() { - return this.isAutoRefreshSelectorEnabled; + public isAutoRefreshSelectorEnabled() { + return this._isAutoRefreshSelectorEnabled; } public getEnabledUpdated$ = () => { @@ -166,7 +166,7 @@ export class Timefilter { * Show the time bounds selector part of the time filter */ public enableTimeRangeSelector = () => { - this.isTimeRangeSelectorEnabled = true; + this._isTimeRangeSelectorEnabled = true; this.enabledUpdated$.next(true); }; @@ -174,7 +174,7 @@ export class Timefilter { * Hide the time bounds selector part of the time filter */ public disableTimeRangeSelector = () => { - this.isTimeRangeSelectorEnabled = false; + this._isTimeRangeSelectorEnabled = false; this.enabledUpdated$.next(false); }; @@ -182,7 +182,7 @@ export class Timefilter { * Show the auto refresh part of the time filter */ public enableAutoRefreshSelector = () => { - this.isAutoRefreshSelectorEnabled = true; + this._isAutoRefreshSelectorEnabled = true; this.enabledUpdated$.next(true); }; @@ -190,7 +190,7 @@ export class Timefilter { * Hide the auto refresh part of the time filter */ public disableAutoRefreshSelector = () => { - this.isAutoRefreshSelectorEnabled = false; + this._isAutoRefreshSelectorEnabled = false; this.enabledUpdated$.next(false); }; diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts index f777b0415b4b7..fad4217acd215 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.mock.ts @@ -23,8 +23,8 @@ export type TimefilterServiceClientContract = PublicMethodsOf const createSetupContractMock = () => { const timefilterMock: jest.Mocked = { - getIsAutoRefreshSelectorEnabled: jest.fn(), - getIsTimeRangeSelectorEnabled: jest.fn(), + isAutoRefreshSelectorEnabled: jest.fn(), + isTimeRangeSelectorEnabled: jest.fn(), getEnabledUpdated$: jest.fn(), getTimeUpdate$: jest.fn(), getRefreshIntervalUpdate$: jest.fn(), diff --git a/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx b/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx index 13a97608643b9..523970dfe12f8 100644 --- a/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx +++ b/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx @@ -37,10 +37,10 @@ export const TopNav: FC = () => { const [time, setTime] = useState(timefilter.getTime()); const [recentlyUsedRanges, setRecentlyUsedRanges] = useState(getRecentlyUsedRanges()); const [isAutoRefreshSelectorEnabled, setIsAutoRefreshSelectorEnabled] = useState( - timefilter.getIsAutoRefreshSelectorEnabled() + timefilter.isAutoRefreshSelectorEnabled() ); const [isTimeRangeSelectorEnabled, setIsTimeRangeSelectorEnabled] = useState( - timefilter.getIsTimeRangeSelectorEnabled() + timefilter.isTimeRangeSelectorEnabled() ); const dateFormat = chrome.getUiSettingsClient().get('dateFormat'); @@ -64,8 +64,8 @@ export const TopNav: FC = () => { function timefilterUpdateListener() { setTime(timefilter.getTime()); setRefreshInterval(timefilter.getRefreshInterval()); - setIsAutoRefreshSelectorEnabled(timefilter.getIsAutoRefreshSelectorEnabled()); - setIsTimeRangeSelectorEnabled(timefilter.getIsTimeRangeSelectorEnabled()); + setIsAutoRefreshSelectorEnabled(timefilter.isAutoRefreshSelectorEnabled()); + setIsTimeRangeSelectorEnabled(timefilter.isTimeRangeSelectorEnabled()); } function updateFilter({ start, end }: Duration) { diff --git a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts index 89e4981c45081..44a08c05f85af 100644 --- a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts +++ b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts @@ -37,10 +37,10 @@ const time = { }; export const uiTimefilterMock = { - getIsAutoRefreshSelectorEnabled() { + isAutoRefreshSelectorEnabled() { return this.isAutoRefreshSelectorEnabled; }, - getIsTimeRangeSelectorEnabled() { + isTimeRangeSelectorEnabled() { return this.isTimeRangeSelectorEnabled; }, enableAutoRefreshSelector() { diff --git a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js index e1f7095b46287..4523c5db58464 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_controller.js @@ -124,8 +124,8 @@ describe('MonitoringViewBaseController', function () { describe('time filter', () => { it('enables timepicker and auto refresh #1', () => { - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(true); }); it('enables timepicker and auto refresh #2', () => { @@ -135,8 +135,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(true); }); it('disables timepicker and enables auto refresh', () => { @@ -146,8 +146,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(false); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(true); }); it('enables timepicker and disables auto refresh', () => { @@ -157,8 +157,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(false); }); it('disables timepicker and auto refresh', () => { @@ -171,8 +171,8 @@ describe('MonitoringViewBaseController', function () { }; ctrl = new MonitoringViewBaseController(opts); - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(false); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(false); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(false); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(false); }); it('disables timepicker and auto refresh', (done) => { diff --git a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js index 777874e0881e1..8cf68a6f2a13e 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/__tests__/base_table_controller.js @@ -22,8 +22,8 @@ describe('MonitoringViewBaseTableController', function () { timefilter = { enableTimeRangeSelector: spy(), enableAutoRefreshSelector: spy(), - getIsTimeRangeSelectorEnabled: () => true, - getIsAutoRefreshSelectorEnabled: () => true, + isTimeRangeSelectorEnabled: () => true, + isAutoRefreshSelectorEnabled: () => true, }; titleService = spy(); executorService = { @@ -76,8 +76,8 @@ describe('MonitoringViewBaseTableController', function () { }); it('enables timepicker', () => { - expect(timefilter.getIsTimeRangeSelectorEnabled()).to.be(true); - expect(timefilter.getIsAutoRefreshSelectorEnabled()).to.be(true); + expect(timefilter.isTimeRangeSelectorEnabled()).to.be(true); + expect(timefilter.isAutoRefreshSelectorEnabled()).to.be(true); }); it('sets page title', () => { From 9143806deceda7ba6d6a5efacfeb92a9221ee547 Mon Sep 17 00:00:00 2001 From: Liza K Date: Tue, 24 Sep 2019 13:15:36 +0300 Subject: [PATCH 21/22] fix ml mocks --- .../plugins/ml/public/contexts/ui/__mocks__/mocks.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts index 44a08c05f85af..96a110c65935a 100644 --- a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts +++ b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts @@ -44,10 +44,10 @@ export const uiTimefilterMock = { return this.isTimeRangeSelectorEnabled; }, enableAutoRefreshSelector() { - this.isAutoRefreshSelectorEnabled = true; + this._isAutoRefreshSelectorEnabled = true; }, enableTimeRangeSelector() { - this.isTimeRangeSelectorEnabled = true; + this._isTimeRangeSelectorEnabled = true; }, getEnabledUpdated$() { return { subscribe: jest.fn() }; @@ -62,8 +62,8 @@ export const uiTimefilterMock = { getTimeUpdate$() { return { subscribe: jest.fn() }; }, - isAutoRefreshSelectorEnabled: false, - isTimeRangeSelectorEnabled: false, + _isAutoRefreshSelectorEnabled: false, + _isTimeRangeSelectorEnabled: false, refreshInterval: { value: 0, pause: true }, on: (event: string, reload: () => void) => {}, setRefreshInterval(refreshInterval: RefreshInterval) { From 8e229bdd36ae227af487608f0b16c087df0e6550 Mon Sep 17 00:00:00 2001 From: Liza K Date: Tue, 24 Sep 2019 14:57:40 +0300 Subject: [PATCH 22/22] ml mocks --- .../legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts index 96a110c65935a..785daec0ab369 100644 --- a/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts +++ b/x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts @@ -38,10 +38,10 @@ const time = { export const uiTimefilterMock = { isAutoRefreshSelectorEnabled() { - return this.isAutoRefreshSelectorEnabled; + return this._isAutoRefreshSelectorEnabled; }, isTimeRangeSelectorEnabled() { - return this.isTimeRangeSelectorEnabled; + return this._isTimeRangeSelectorEnabled; }, enableAutoRefreshSelector() { this._isAutoRefreshSelectorEnabled = true;