From fb4d76d44e81a1c4de3df1219a503a2d6679dd68 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Thu, 18 Apr 2019 13:30:56 +0300 Subject: [PATCH 01/13] Cleaned up usage of angular dependencies from apply filters \ filter bar --- .../ui/public/apply_filters/directive.js | 11 ++- .../filter_bar/lib/generate_mapping_chain.js | 26 +++---- .../filter_bar/lib/map_and_flatten_filters.js | 18 ++--- .../ui/public/filter_bar/lib/map_default.js | 23 +++--- .../ui/public/filter_bar/lib/map_exists.js | 18 ++--- .../ui/public/filter_bar/lib/map_filter.js | 77 +++++++++---------- .../lib/map_flatten_and_wrap_filters.js | 34 -------- .../filter_bar/lib/map_geo_bounding_box.js | 8 +- .../public/filter_bar/lib/map_geo_polygon.js | 8 +- .../ui/public/filter_bar/lib/map_match_all.js | 18 ++--- .../ui/public/filter_bar/lib/map_missing.js | 18 ++--- .../ui/public/filter_bar/lib/map_phrase.js | 8 +- .../ui/public/filter_bar/lib/map_phrases.js | 16 ++-- .../public/filter_bar/lib/map_query_string.js | 18 ++--- .../ui/public/filter_bar/lib/map_range.js | 9 +-- .../ui/public/filter_bar/query_filter.js | 9 +-- 16 files changed, 126 insertions(+), 193 deletions(-) delete mode 100644 src/legacy/ui/public/filter_bar/lib/map_flatten_and_wrap_filters.js diff --git a/src/legacy/ui/public/apply_filters/directive.js b/src/legacy/ui/public/apply_filters/directive.js index d364a1843494a..f69bde8dce8d4 100644 --- a/src/legacy/ui/public/apply_filters/directive.js +++ b/src/legacy/ui/public/apply_filters/directive.js @@ -21,17 +21,16 @@ import 'ngreact'; import { uiModules } from '../modules'; import template from './directive.html'; import { ApplyFiltersPopover } from './apply_filters_popover'; -import { FilterBarLibMapAndFlattenFiltersProvider } from '../filter_bar/lib/map_and_flatten_filters'; +import { mapAndFlattenFilters } from '../filter_bar/lib/map_and_flatten_filters'; +import { wrapInI18nContext } from 'ui/i18n'; const app = uiModules.get('app/kibana', ['react']); app.directive('applyFiltersPopoverComponent', (reactDirective) => { - return reactDirective(ApplyFiltersPopover); + return reactDirective(wrapInI18nContext(ApplyFiltersPopover)); }); -app.directive('applyFiltersPopover', (reactDirective, Private) => { - const mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); - +app.directive('applyFiltersPopover', (indexPatterns) => { return { template, restrict: 'E', @@ -47,7 +46,7 @@ app.directive('applyFiltersPopover', (reactDirective, Private) => { // popover, because it has to reset its state whenever the new filters change. Setting a `key` // property on the component accomplishes this due to how React handles the `key` property. $scope.$watch('filters', filters => { - mapAndFlattenFilters(filters).then(mappedFilters => { + mapAndFlattenFilters(indexPatterns, filters).then(mappedFilters => { $scope.state = { filters: mappedFilters, key: Date.now(), diff --git a/src/legacy/ui/public/filter_bar/lib/generate_mapping_chain.js b/src/legacy/ui/public/filter_bar/lib/generate_mapping_chain.js index 35471ff6ff8d0..02905999464fd 100644 --- a/src/legacy/ui/public/filter_bar/lib/generate_mapping_chain.js +++ b/src/legacy/ui/public/filter_bar/lib/generate_mapping_chain.js @@ -17,24 +17,18 @@ * under the License. */ -export function FilterBarLibGenerateMappingChainProvider(Promise) { - +export function generateMappingChain(fn, next) { const noop = function () { - return Promise.reject(new Error('No mappings have been found for filter.')); + throw new Error('No mappings have been found for filter.'); }; - return function (fn) { - return function (next) { - next = next || noop; - return function (filter) { - return fn(filter).catch(function (result) { - if (result === filter) { - return next(filter); - } - return Promise.reject(result); - }); - }; - }; + next = next || noop; + return async function (filter) { + return await fn(filter).catch(function (result) { + if (result === filter) { + return next(filter); + } + throw result; + }); }; - } diff --git a/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js b/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js index efc8305783fbb..90b2f06356fa6 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js +++ b/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js @@ -18,16 +18,12 @@ */ import _ from 'lodash'; -import { FilterBarLibMapFilterProvider } from './map_filter'; +import { mapFilter } from './map_filter'; -export function FilterBarLibMapAndFlattenFiltersProvider(Private, Promise) { - const mapFilter = Private(FilterBarLibMapFilterProvider); - return function (filters) { - return _(filters) - .flatten() - .compact() - .map(mapFilter) - .thru(Promise.all) - .value(); - }; +export async function mapAndFlattenFilters(indexPatterns, filters) { + const flattened = _(filters) + .flatten() + .compact() + .value(); + return await Promise.all(flattened.map(item => mapFilter(indexPatterns, item))); } diff --git a/src/legacy/ui/public/filter_bar/lib/map_default.js b/src/legacy/ui/public/filter_bar/lib/map_default.js index 5eabd1b79e251..b0733b1d6d7bb 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_default.js +++ b/src/legacy/ui/public/filter_bar/lib/map_default.js @@ -20,21 +20,18 @@ import angular from 'angular'; import _ from 'lodash'; -export function FilterBarLibMapDefaultProvider(Promise) { - +export async function checkIsDefault(filter) { const metaProperty = /(^\$|meta)/; - return function (filter) { - const key = _.find(_.keys(filter), function (key) { - return !key.match(metaProperty); - }); + const key = _.find(_.keys(filter), function (key) { + return !key.match(metaProperty); + }); - if (key) { - const type = 'custom'; - const value = angular.toJson(filter[key]); - return Promise.resolve({ type, key, value }); - } + if (key) { + const type = 'custom'; + const value = angular.toJson(filter[key]); + return { type, key, value }; + } - return Promise.reject(filter); - }; + throw filter; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_exists.js b/src/legacy/ui/public/filter_bar/lib/map_exists.js index 89a09ac9e0f41..6ad2daceca026 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_exists.js +++ b/src/legacy/ui/public/filter_bar/lib/map_exists.js @@ -17,14 +17,12 @@ * under the License. */ -export function FilterBarLibMapExistsProvider(Promise) { - return function (filter) { - if (filter.exists) { - const type = 'exists'; - const key = filter.exists.field; - const value = type; - return Promise.resolve({ type, key, value }); - } - return Promise.reject(filter); - }; +export async function checkIsExists(filter) { + if (filter.exists) { + const type = 'exists'; + const key = filter.exists.field; + const value = type; + return { type, key, value }; + } + throw filter; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_filter.js b/src/legacy/ui/public/filter_bar/lib/map_filter.js index c7bdd9befee80..c3fe5d2085e42 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/map_filter.js @@ -18,22 +18,19 @@ */ import _ from 'lodash'; -import { FilterBarLibGenerateMappingChainProvider } from './generate_mapping_chain'; -import { FilterBarLibMapMatchAllProvider } from './map_match_all'; -import { FilterBarLibMapPhraseProvider } from './map_phrase'; -import { FilterBarLibMapPhrasesProvider } from './map_phrases'; -import { FilterBarLibMapRangeProvider } from './map_range'; -import { FilterBarLibMapExistsProvider } from './map_exists'; -import { FilterBarLibMapMissingProvider } from './map_missing'; -import { FilterBarLibMapQueryStringProvider } from './map_query_string'; -import { FilterBarLibMapGeoBoundingBoxProvider } from './map_geo_bounding_box'; -import { FilterBarLibMapGeoPolygonProvider } from './map_geo_polygon'; -import { FilterBarLibMapDefaultProvider } from './map_default'; - -export function FilterBarLibMapFilterProvider(Promise, Private) { - - const generateMappingChain = Private(FilterBarLibGenerateMappingChainProvider); +import { checkIsMatchAll } from './map_match_all'; +import { checkIsPhrase } from './map_phrase'; +import { checkIsPhrases } from './map_phrases'; +import { checkIsRange } from './map_range'; +import { checkIsExists } from './map_exists'; +import { checkIsMissing } from './map_missing'; +import { checkIsQueryString } from './map_query_string'; +import { checkIsGeoBoundingBox } from './map_geo_bounding_box'; +import { checkIsGeoPolygon } from './map_geo_polygon'; +import { checkIsDefault } from './map_default'; +import { generateMappingChain } from './generate_mapping_chain'; +export async function mapFilter(indexPatterns, filter) { /** Mappers **/ // Each mapper is a simple promise function that test if the mapper can @@ -51,27 +48,26 @@ export function FilterBarLibMapFilterProvider(Promise, Private) { // that either handles the mapping operation or not // and add it here. ProTip: These are executed in order listed const mappers = [ - Private(FilterBarLibMapMatchAllProvider), - Private(FilterBarLibMapRangeProvider), - Private(FilterBarLibMapPhraseProvider), - Private(FilterBarLibMapPhrasesProvider), - Private(FilterBarLibMapExistsProvider), - Private(FilterBarLibMapMissingProvider), - Private(FilterBarLibMapQueryStringProvider), - Private(FilterBarLibMapGeoBoundingBoxProvider), - Private(FilterBarLibMapGeoPolygonProvider), - Private(FilterBarLibMapDefaultProvider), + checkIsMatchAll, + checkIsRange(indexPatterns), + checkIsPhrase(indexPatterns), + checkIsPhrases, + checkIsExists, + checkIsMissing, + checkIsQueryString, + checkIsGeoBoundingBox(indexPatterns), + checkIsGeoPolygon(indexPatterns), + checkIsDefault, ]; const noop = function () { - return Promise.reject(new Error('No mappings have been found for filter.')); + throw new Error('No mappings have been found for filter.'); }; // Create a chain of responsibility by reducing all the // mappers down into one function. const mapFn = _.reduceRight(mappers, function (memo, map) { - const filterChainFn = generateMappingChain(map); - return filterChainFn(memo); + return generateMappingChain(map, memo); }, noop); /** @@ -80,18 +76,15 @@ export function FilterBarLibMapFilterProvider(Promise, Private) { * @param {object} filter The filter the map * @returns {Promise} */ - return function (filter) { - // Apply the mapping function - return mapFn(filter).then(function (result) { - filter.meta = filter.meta || {}; - filter.meta.type = result.type; - filter.meta.key = result.key; - filter.meta.value = result.value; - filter.meta.params = result.params; - filter.meta.disabled = !!(filter.meta.disabled); - filter.meta.negate = !!(filter.meta.negate); - filter.meta.alias = filter.meta.alias || null; - return filter; - }); - }; + return mapFn(filter).then(function (result) { + filter.meta = filter.meta || {}; + filter.meta.type = result.type; + filter.meta.key = result.key; + filter.meta.value = result.value; + filter.meta.params = result.params; + filter.meta.disabled = !!(filter.meta.disabled); + filter.meta.negate = !!(filter.meta.negate); + filter.meta.alias = filter.meta.alias || null; + return filter; + }); } diff --git a/src/legacy/ui/public/filter_bar/lib/map_flatten_and_wrap_filters.js b/src/legacy/ui/public/filter_bar/lib/map_flatten_and_wrap_filters.js deleted file mode 100644 index 1c327d28a6b83..0000000000000 --- a/src/legacy/ui/public/filter_bar/lib/map_flatten_and_wrap_filters.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 _ from 'lodash'; -import { FilterBarLibMapAndFlattenFiltersProvider } from './map_and_flatten_filters'; - -export function FilterBarLibMapFlattenAndWrapFiltersProvider(Private) { - const mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); - return function (filters) { - return mapAndFlattenFilters(filters).then(function (filters) { - return _.map(filters, function (filter) { - filter.meta = filter.meta || {}; - filter.meta.apply = true; - return filter; - }); - }); - }; -} diff --git a/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js b/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js index 70cdc1dfabb87..590c4488dc0a1 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js +++ b/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js @@ -20,8 +20,8 @@ import _ from 'lodash'; import { SavedObjectNotFound } from '../../errors'; -export function FilterBarLibMapGeoBoundingBoxProvider(Promise, indexPatterns) { - return function (filter) { +export function checkIsGeoBoundingBox(indexPatterns) { + return async function (filter) { if (filter.geo_bounding_box) { function getParams(indexPattern) { const type = 'geo_bounding_box'; @@ -43,7 +43,7 @@ export function FilterBarLibMapGeoBoundingBoxProvider(Promise, indexPatterns) { return { type, key, value, params }; } - return indexPatterns + return await indexPatterns .get(filter.meta.index) .then(getParams) .catch((error) => { @@ -53,6 +53,6 @@ export function FilterBarLibMapGeoBoundingBoxProvider(Promise, indexPatterns) { throw error; }); } - return Promise.reject(filter); + throw filter; }; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js b/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js index b93151c479ef5..6127533a496dc 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js +++ b/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js @@ -20,8 +20,8 @@ import _ from 'lodash'; import { SavedObjectNotFound } from '../../errors'; -export function FilterBarLibMapGeoPolygonProvider(Promise, indexPatterns) { - return function (filter) { +export function checkIsGeoPolygon(indexPatterns) { + return async function (filter) { if (filter.geo_polygon) { function getParams(indexPattern) { const type = 'geo_polygon'; @@ -42,7 +42,7 @@ export function FilterBarLibMapGeoPolygonProvider(Promise, indexPatterns) { return { type, key, value, params }; } - return indexPatterns + return await indexPatterns .get(filter.meta.index) .then(getParams) .catch((error) => { @@ -52,6 +52,6 @@ export function FilterBarLibMapGeoPolygonProvider(Promise, indexPatterns) { throw error; }); } - return Promise.reject(filter); + throw filter; }; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_match_all.js b/src/legacy/ui/public/filter_bar/lib/map_match_all.js index deabe382758e4..d3464ab83db8b 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_match_all.js +++ b/src/legacy/ui/public/filter_bar/lib/map_match_all.js @@ -17,14 +17,12 @@ * under the License. */ -export function FilterBarLibMapMatchAllProvider(Promise) { - return function (filter) { - if (filter.match_all) { - const type = 'match_all'; - const key = filter.meta.field; - const value = filter.meta.formattedValue || 'all'; - return Promise.resolve({ type, key, value }); - } - return Promise.reject(filter); - }; +export async function checkIsMatchAll(filter) { + if (filter.match_all) { + const type = 'match_all'; + const key = filter.meta.field; + const value = filter.meta.formattedValue || 'all'; + return { type, key, value }; + } + throw filter; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_missing.js b/src/legacy/ui/public/filter_bar/lib/map_missing.js index a737671d0dd4f..de2b08d73ae70 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_missing.js +++ b/src/legacy/ui/public/filter_bar/lib/map_missing.js @@ -17,14 +17,12 @@ * under the License. */ -export function FilterBarLibMapMissingProvider(Promise) { - return function (filter) { - if (filter.missing) { - const type = 'missing'; - const key = filter.missing.field; - const value = type; - return Promise.resolve({ type, key, value }); - } - return Promise.reject(filter); - }; +export async function checkIsMissing(filter) { + if (filter.missing) { + const type = 'missing'; + const key = filter.missing.field; + const value = type; + return { type, key, value }; + } + throw filter; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_phrase.js b/src/legacy/ui/public/filter_bar/lib/map_phrase.js index 687c844f7eec7..454d6472e62d2 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_phrase.js +++ b/src/legacy/ui/public/filter_bar/lib/map_phrase.js @@ -20,11 +20,11 @@ import _ from 'lodash'; import { SavedObjectNotFound } from '../../errors'; -export function FilterBarLibMapPhraseProvider(Promise, indexPatterns) { - return function (filter) { +export function checkIsPhrase(indexPatterns) { + return async function (filter) { const isScriptedPhraseFilter = isScriptedPhrase(filter); if (!_.has(filter, ['query', 'match']) && !isScriptedPhraseFilter) { - return Promise.reject(filter); + throw filter; } function getParams(indexPattern) { @@ -41,7 +41,7 @@ export function FilterBarLibMapPhraseProvider(Promise, indexPatterns) { return { type, key, value, params }; } - return indexPatterns + return await indexPatterns .get(filter.meta.index) .then(getParams) .catch((error) => { diff --git a/src/legacy/ui/public/filter_bar/lib/map_phrases.js b/src/legacy/ui/public/filter_bar/lib/map_phrases.js index eb6209e252b6f..cfd6c85655dde 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_phrases.js +++ b/src/legacy/ui/public/filter_bar/lib/map_phrases.js @@ -17,13 +17,11 @@ * under the License. */ -export function FilterBarLibMapPhrasesProvider(Promise) { - return function (filter) { - const { type, key, value, params } = filter.meta; - if (type !== 'phrases') { - return Promise.reject(filter); - } else { - return Promise.resolve({ type, key, value, params }); - } - }; +export async function checkIsPhrases(filter) { + const { type, key, value, params } = filter.meta; + if (type !== 'phrases') { + throw filter; + } else { + return { type, key, value, params }; + } } diff --git a/src/legacy/ui/public/filter_bar/lib/map_query_string.js b/src/legacy/ui/public/filter_bar/lib/map_query_string.js index dec26bde5d4c7..047fd712e14ee 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_query_string.js +++ b/src/legacy/ui/public/filter_bar/lib/map_query_string.js @@ -17,14 +17,12 @@ * under the License. */ -export function FilterBarLibMapQueryStringProvider(Promise) { - return function (filter) { - if (filter.query && filter.query.query_string) { - const type = 'query_string'; - const key = 'query'; - const value = filter.query.query_string.query; - return Promise.resolve({ type, key, value }); - } - return Promise.reject(filter); - }; +export async function checkIsQueryString(filter) { + if (filter.query && filter.query.query_string) { + const type = 'query_string'; + const key = 'query'; + const value = filter.query.query_string.query; + return { type, key, value }; + } + throw filter; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_range.js b/src/legacy/ui/public/filter_bar/lib/map_range.js index c6a3ac3bcf6c1..671993aaadd8e 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_range.js +++ b/src/legacy/ui/public/filter_bar/lib/map_range.js @@ -20,11 +20,11 @@ import { has, get } from 'lodash'; import { SavedObjectNotFound } from '../../errors'; -export function FilterBarLibMapRangeProvider(Promise, indexPatterns) { - return function (filter) { +export function checkIsRange(indexPatterns) { + return async function (filter) { const isScriptedRangeFilter = isScriptedRange(filter); if (!filter.range && !isScriptedRangeFilter) { - return Promise.reject(filter); + throw filter; } function getParams(indexPattern) { @@ -51,7 +51,7 @@ export function FilterBarLibMapRangeProvider(Promise, indexPatterns) { return { type, key, value, params }; } - return indexPatterns + return await indexPatterns .get(filter.meta.index) .then(getParams) .catch((error) => { @@ -60,7 +60,6 @@ export function FilterBarLibMapRangeProvider(Promise, indexPatterns) { } throw error; }); - }; } diff --git a/src/legacy/ui/public/filter_bar/query_filter.js b/src/legacy/ui/public/filter_bar/query_filter.js index 5b73013973fb8..477cdd516f435 100644 --- a/src/legacy/ui/public/filter_bar/query_filter.js +++ b/src/legacy/ui/public/filter_bar/query_filter.js @@ -23,13 +23,12 @@ import { onlyStateChanged } from './lib/only_state_changed'; import { uniqFilters } from './lib/uniq_filters'; import { compareFilters } from './lib/compare_filters'; import { EventsProvider } from '../events'; -import { FilterBarLibMapAndFlattenFiltersProvider } from './lib/map_and_flatten_filters'; +import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; import { FilterBarLibExtractTimeFilterProvider } from './lib/extract_time_filter'; import { changeTimeFilter } from './lib/change_time_filter'; -export function FilterBarQueryFilterProvider(Private, $rootScope, getAppState, globalState, config) { +export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, getAppState, globalState, config) { const EventEmitter = Private(EventsProvider); - const mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); const extractTimeFilter = Private(FilterBarLibExtractTimeFilterProvider); const queryFilter = new EventEmitter(); @@ -85,7 +84,7 @@ export function FilterBarQueryFilterProvider(Private, $rootScope, getAppState, g filters = [filters]; } - return mapAndFlattenFilters(filters) + return mapAndFlattenFilters(indexPatterns, filters) .then(function (filters) { if (!filterState.filters) { filterState.filters = []; @@ -220,7 +219,7 @@ export function FilterBarQueryFilterProvider(Private, $rootScope, getAppState, g }; queryFilter.setFilters = filters => { - return mapAndFlattenFilters(filters) + return mapAndFlattenFilters(indexPatterns, filters) .then(mappedFilters => { const appState = getAppState(); const [globalFilters, appFilters] = _.partition(mappedFilters, filter => { From a0c7ede64c9719e10e87c70a9f34a4a24f77a597 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Thu, 18 Apr 2019 13:35:23 +0300 Subject: [PATCH 02/13] Cleaned up usage of angular dependencies from exact time filter --- .../filter_bar/lib/extract_time_filter.js | 28 +++++++++---------- .../ui/public/filter_bar/query_filter.js | 5 ++-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js b/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js index ad12737dcf5cc..a42cd43b306fd 100644 --- a/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js @@ -19,21 +19,19 @@ import _ from 'lodash'; -export function FilterBarLibExtractTimeFilterProvider(indexPatterns, Promise) { - return Promise.method(function (filters) { - // Assume all the index patterns are the same since they will be added - // from the same visualization. - const id = _.get(filters, '[0].meta.index'); - if (id == null) return; +export async function getExtractTimeFilter(indexPatterns, filters) { + // Assume all the index patterns are the same since they will be added + // from the same visualization. + const id = _.get(filters, '[0].meta.index'); + if (id == null) return; - return indexPatterns.get(id).then(function (indexPattern) { - const filter = _.find(filters, function (obj) { - const key = _.keys(obj.range)[0]; - return key === indexPattern.timeFieldName; - }); - if (filter && filter.range) { - return filter; - } - }); + const indexPattern = await indexPatterns.get(id); + + const filter = _.find(filters, function (obj) { + const key = _.keys(obj.range)[0]; + return key === indexPattern.timeFieldName; }); + if (filter && filter.range) { + return filter; + } } diff --git a/src/legacy/ui/public/filter_bar/query_filter.js b/src/legacy/ui/public/filter_bar/query_filter.js index 477cdd516f435..3fa762dd54a4b 100644 --- a/src/legacy/ui/public/filter_bar/query_filter.js +++ b/src/legacy/ui/public/filter_bar/query_filter.js @@ -24,12 +24,11 @@ import { uniqFilters } from './lib/uniq_filters'; import { compareFilters } from './lib/compare_filters'; import { EventsProvider } from '../events'; import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; -import { FilterBarLibExtractTimeFilterProvider } from './lib/extract_time_filter'; +import { getExtractTimeFilter } from './lib/extract_time_filter'; import { changeTimeFilter } from './lib/change_time_filter'; export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, getAppState, globalState, config) { const EventEmitter = Private(EventsProvider); - const extractTimeFilter = Private(FilterBarLibExtractTimeFilterProvider); const queryFilter = new EventEmitter(); @@ -231,7 +230,7 @@ export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, }; queryFilter.addFiltersAndChangeTimeFilter = async filters => { - const timeFilter = await extractTimeFilter(filters); + const timeFilter = await getExtractTimeFilter(indexPatterns, filters); if (timeFilter) changeTimeFilter(timeFilter); queryFilter.addFilters(filters.filter(filter => filter !== timeFilter)); }; From eabb479b3b5edc584ba10c97520309dbcf85e294 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Thu, 18 Apr 2019 18:55:23 +0300 Subject: [PATCH 03/13] Improved syntax of filters using index patterns --- .../filter_bar/lib/map_geo_bounding_box.js | 60 +++++++-------- .../public/filter_bar/lib/map_geo_polygon.js | 57 +++++++------- .../ui/public/filter_bar/lib/map_phrase.js | 55 +++++++------- .../ui/public/filter_bar/lib/map_range.js | 74 ++++++++++--------- 4 files changed, 125 insertions(+), 121 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js b/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js index 590c4488dc0a1..69ed21d9fe5eb 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js +++ b/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js @@ -20,39 +20,39 @@ import _ from 'lodash'; import { SavedObjectNotFound } from '../../errors'; +function getParams(filter, indexPattern) { + const type = 'geo_bounding_box'; + const key = _.keys(filter.geo_bounding_box) + .filter(key => key !== 'ignore_unmapped')[0]; + const params = filter.geo_bounding_box[key]; + + // Sometimes a filter will end up with an invalid index param. This could happen for a lot of reasons, + // for example a user might manually edit the url or the index pattern's ID might change due to + // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback + // on displaying the raw value if the index is invalid. + const topLeft = indexPattern + ? indexPattern.fields.byName[key].format.convert(params.top_left) + : JSON.stringify(params.top_left); + const bottomRight = indexPattern + ? indexPattern.fields.byName[key].format.convert(params.bottom_right) + : JSON.stringify(params.bottom_right); + const value = topLeft + ' to ' + bottomRight; + return { type, key, value, params }; +} + export function checkIsGeoBoundingBox(indexPatterns) { return async function (filter) { - if (filter.geo_bounding_box) { - function getParams(indexPattern) { - const type = 'geo_bounding_box'; - const key = _.keys(filter.geo_bounding_box) - .filter(key => key !== 'ignore_unmapped')[0]; - const params = filter.geo_bounding_box[key]; - - // Sometimes a filter will end up with an invalid index param. This could happen for a lot of reasons, - // for example a user might manually edit the url or the index pattern's ID might change due to - // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback - // on displaying the raw value if the index is invalid. - const topLeft = indexPattern - ? indexPattern.fields.byName[key].format.convert(params.top_left) - : JSON.stringify(params.top_left); - const bottomRight = indexPattern - ? indexPattern.fields.byName[key].format.convert(params.bottom_right) - : JSON.stringify(params.bottom_right); - const value = topLeft + ' to ' + bottomRight; - return { type, key, value, params }; + if (!filter.geo_bounding_box) { + throw filter; + } + try { + const indexPattern = await indexPatterns.get(filter.meta.index); + return getParams(filter, indexPattern); + } catch (error) { + if (error instanceof SavedObjectNotFound) { + return getParams(filter); } - - return await indexPatterns - .get(filter.meta.index) - .then(getParams) - .catch((error) => { - if (error instanceof SavedObjectNotFound) { - return getParams(); - } - throw error; - }); + throw error; } - throw filter; }; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js b/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js index 6127533a496dc..4c35d34ea9245 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js +++ b/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js @@ -20,38 +20,39 @@ import _ from 'lodash'; import { SavedObjectNotFound } from '../../errors'; +function getParams(filter, indexPattern) { + const type = 'geo_polygon'; + const key = _.keys(filter.geo_polygon) + .filter(key => key !== 'ignore_unmapped')[0]; + const params = filter.geo_polygon[key]; + + // Sometimes a filter will end up with an invalid index param. This could happen for a lot of reasons, + // for example a user might manually edit the url or the index pattern's ID might change due to + // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback + // on displaying the raw value if the index is invalid. + const points = params.points.map((point) => { + return indexPattern + ? indexPattern.fields.byName[key].format.convert(point) + : JSON.stringify(point); + }); + const value = points.join(', '); + return { type, key, value, params }; +} + export function checkIsGeoPolygon(indexPatterns) { return async function (filter) { - if (filter.geo_polygon) { - function getParams(indexPattern) { - const type = 'geo_polygon'; - const key = _.keys(filter.geo_polygon) - .filter(key => key !== 'ignore_unmapped')[0]; - const params = filter.geo_polygon[key]; + if (!filter.geo_polygon) { + throw filter; + } - // Sometimes a filter will end up with an invalid index param. This could happen for a lot of reasons, - // for example a user might manually edit the url or the index pattern's ID might change due to - // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback - // on displaying the raw value if the index is invalid. - const points = params.points.map((point) => { - return indexPattern - ? indexPattern.fields.byName[key].format.convert(point) - : JSON.stringify(point); - }); - const value = points.join(', '); - return { type, key, value, params }; + try { + const indexPattern = await indexPatterns.get(filter.meta.index); + return getParams(filter, indexPattern); + } catch (error) { + if (error instanceof SavedObjectNotFound) { + return getParams(filter); } - - return await indexPatterns - .get(filter.meta.index) - .then(getParams) - .catch((error) => { - if (error instanceof SavedObjectNotFound) { - return getParams(); - } - throw error; - }); + throw error; } - throw filter; }; } diff --git a/src/legacy/ui/public/filter_bar/lib/map_phrase.js b/src/legacy/ui/public/filter_bar/lib/map_phrase.js index 454d6472e62d2..992054202e969 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_phrase.js +++ b/src/legacy/ui/public/filter_bar/lib/map_phrase.js @@ -20,6 +20,26 @@ import _ from 'lodash'; import { SavedObjectNotFound } from '../../errors'; +function isScriptedPhrase(filter) { + const value = _.get(filter, ['script', 'script', 'params', 'value']); + return typeof value !== 'undefined'; +} + +function getParams(filter, indexPattern) { + const isScriptedPhraseFilter = isScriptedPhrase(filter); + const type = 'phrase'; + const key = isScriptedPhraseFilter ? filter.meta.field : Object.keys(filter.query.match)[0]; + const query = isScriptedPhraseFilter ? filter.script.script.params.value : filter.query.match[key].query; + const params = { query }; + + // Sometimes a filter will end up with an invalid index or field param. This could happen for a lot of reasons, + // for example a user might manually edit the url or the index pattern's ID might change due to + // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback + // on displaying the raw value if the index or field is invalid. + const value = (indexPattern && indexPattern.fields.byName[key]) ? indexPattern.fields.byName[key].format.convert(query) : query; + return { type, key, value, params }; +} + export function checkIsPhrase(indexPatterns) { return async function (filter) { const isScriptedPhraseFilter = isScriptedPhrase(filter); @@ -27,33 +47,14 @@ export function checkIsPhrase(indexPatterns) { throw filter; } - function getParams(indexPattern) { - const type = 'phrase'; - const key = isScriptedPhraseFilter ? filter.meta.field : Object.keys(filter.query.match)[0]; - const query = isScriptedPhraseFilter ? filter.script.script.params.value : filter.query.match[key].query; - const params = { query }; - - // Sometimes a filter will end up with an invalid index or field param. This could happen for a lot of reasons, - // for example a user might manually edit the url or the index pattern's ID might change due to - // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback - // on displaying the raw value if the index or field is invalid. - const value = (indexPattern && indexPattern.fields.byName[key]) ? indexPattern.fields.byName[key].format.convert(query) : query; - return { type, key, value, params }; + try { + const indexPattern = await indexPatterns.get(filter.meta.index); + return getParams(filter, indexPattern); + } catch (error) { + if (error instanceof SavedObjectNotFound) { + return getParams(filter); + } + throw error; } - - return await indexPatterns - .get(filter.meta.index) - .then(getParams) - .catch((error) => { - if (error instanceof SavedObjectNotFound) { - return getParams(); - } - throw error; - }); }; } - -function isScriptedPhrase(filter) { - const value = _.get(filter, ['script', 'script', 'params', 'value']); - return typeof value !== 'undefined'; -} diff --git a/src/legacy/ui/public/filter_bar/lib/map_range.js b/src/legacy/ui/public/filter_bar/lib/map_range.js index 671993aaadd8e..288965e897d99 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_range.js +++ b/src/legacy/ui/public/filter_bar/lib/map_range.js @@ -20,6 +20,37 @@ import { has, get } from 'lodash'; import { SavedObjectNotFound } from '../../errors'; + +function isScriptedRange(filter) { + const params = get(filter, ['script', 'script', 'params']); + return params && Object.keys(params).find(key => ['gte', 'gt', 'lte', 'lt'].includes(key)); +} + +function getParams(filter, indexPattern) { + const isScriptedRangeFilter = isScriptedRange(filter); + const type = 'range'; + const key = isScriptedRangeFilter ? filter.meta.field : Object.keys(filter.range)[0]; + const params = isScriptedRangeFilter ? filter.script.script.params : filter.range[key]; + + let left = has(params, 'gte') ? params.gte : params.gt; + if (left == null) left = -Infinity; + + let right = has(params, 'lte') ? params.lte : params.lt; + if (right == null) right = Infinity; + + // Sometimes a filter will end up with an invalid index param. This could happen for a lot of reasons, + // for example a user might manually edit the url or the index pattern's ID might change due to + // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback + // on displaying the raw value if the index is invalid. + let value = `${left} to ${right}`; + if (indexPattern) { + const convert = indexPattern.fields.byName[key].format.getConverterFor('text'); + value = `${convert(left)} to ${convert(right)}`; + } + + return { type, key, value, params }; +} + export function checkIsRange(indexPatterns) { return async function (filter) { const isScriptedRangeFilter = isScriptedRange(filter); @@ -27,43 +58,14 @@ export function checkIsRange(indexPatterns) { throw filter; } - function getParams(indexPattern) { - const type = 'range'; - const key = isScriptedRangeFilter ? filter.meta.field : Object.keys(filter.range)[0]; - const params = isScriptedRangeFilter ? filter.script.script.params : filter.range[key]; - - let left = has(params, 'gte') ? params.gte : params.gt; - if (left == null) left = -Infinity; - - let right = has(params, 'lte') ? params.lte : params.lt; - if (right == null) right = Infinity; - - // Sometimes a filter will end up with an invalid index param. This could happen for a lot of reasons, - // for example a user might manually edit the url or the index pattern's ID might change due to - // external factors e.g. a reindex. We only need the index in order to grab the field formatter, so we fallback - // on displaying the raw value if the index is invalid. - let value = `${left} to ${right}`; - if (indexPattern) { - const convert = indexPattern.fields.byName[key].format.getConverterFor('text'); - value = `${convert(left)} to ${convert(right)}`; + try { + const indexPattern = await indexPatterns.get(filter.meta.index); + return getParams(filter, indexPattern); + } catch (error) { + if (error instanceof SavedObjectNotFound) { + return getParams(filter); } - - return { type, key, value, params }; + throw error; } - - return await indexPatterns - .get(filter.meta.index) - .then(getParams) - .catch((error) => { - if (error instanceof SavedObjectNotFound) { - return getParams(); - } - throw error; - }); }; } - -function isScriptedRange(filter) { - const params = get(filter, ['script', 'script', 'params']); - return params && Object.keys(params).find(key => ['gte', 'gt', 'lte', 'lt'].includes(key)); -} From 4b958a61356765c5222cc8cf6073bb137d813868 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Mon, 22 Apr 2019 12:17:24 +0300 Subject: [PATCH 04/13] Removed unused Private from EventsProvider --- src/legacy/ui/public/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/ui/public/events.js b/src/legacy/ui/public/events.js index b8ad36ad15e5a..e25c8be7e85ad 100644 --- a/src/legacy/ui/public/events.js +++ b/src/legacy/ui/public/events.js @@ -30,7 +30,7 @@ import { createLegacyClass } from './utils/legacy_class'; const location = 'EventEmitter'; -export function EventsProvider(Private, Promise) { +export function EventsProvider(Promise) { createLegacyClass(Events).inherits(SimpleEmitter); function Events() { Events.Super.call(this); From 76687c0a0d3a7bc7b3e4702fbf4dced553c6f42e Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Mon, 22 Apr 2019 14:41:27 +0300 Subject: [PATCH 05/13] Fixing filter bar lib tests 1 --- .../filter_bar/lib/__tests__/map_default.js | 20 ++---- .../filter_bar/lib/__tests__/map_exists.js | 14 +--- .../__tests__/map_flatten_and_wrap_filters.js | 68 ------------------- .../filter_bar/lib/__tests__/map_match_all.js | 19 +++--- .../filter_bar/lib/__tests__/map_missing.js | 18 +---- .../lib/__tests__/map_query_string.js | 17 ++--- 6 files changed, 22 insertions(+), 134 deletions(-) delete mode 100644 src/legacy/ui/public/filter_bar/lib/__tests__/map_flatten_and_wrap_filters.js diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js index e22f4639d97e1..aef94892698f1 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js @@ -18,28 +18,18 @@ */ import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import { FilterBarLibMapDefaultProvider } from '../map_default'; +import { checkIsDefault } from '../map_default'; describe('Filter Bar Directive', function () { describe('mapDefault()', function () { - let mapDefault; - let $rootScope; - beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - $rootScope = _$rootScope_; - mapDefault = Private(FilterBarLibMapDefaultProvider); - })); - it('should return the key and value for matching filters', function (done) { const filter = { query: { match_all: {} } }; - mapDefault(filter).then(function (result) { + checkIsDefault(filter).then(function (result) { expect(result).to.have.property('key', 'query'); expect(result).to.have.property('value', '{"match_all":{}}'); done(); }); - $rootScope.$apply(); }); it('should work with undefined filter types', function (done) { @@ -52,21 +42,19 @@ describe('Filter Bar Directive', function () { } } }; - mapDefault(filter).then(function (result) { + checkIsDefault(filter).then(function (result) { expect(result).to.have.property('key', 'bool'); expect(result).to.have.property('value', JSON.stringify(filter.bool)); done(); }); - $rootScope.$apply(); }); it('should return undefined if there is no valid key', function (done) { const filter = { meta: {} }; - mapDefault(filter).catch(function (result) { + checkIsDefault(filter).catch(function (result) { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js index f0b09da6ba687..8bf076cbf26cc 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js @@ -19,36 +19,28 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapExistsProvider } from '../map_exists'; +import { checkIsExists } from '../map_exists'; describe('Filter Bar Directive', function () { describe('mapExists()', function () { - let mapExists; - let $rootScope; beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - $rootScope = _$rootScope_; - mapExists = Private(FilterBarLibMapExistsProvider); - })); it('should return the key and value for matching filters', function (done) { const filter = { exists: { field: '_type' } }; - mapExists(filter).then(function (result) { + checkIsExists(filter).then(function (result) { expect(result).to.have.property('key', '_type'); expect(result).to.have.property('value', 'exists'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { const filter = { query: { match: { query: 'foo' } } }; - mapExists(filter).catch(function (result) { + checkIsExists(filter).catch(function (result) { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_flatten_and_wrap_filters.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_flatten_and_wrap_filters.js deleted file mode 100644 index 8b0ca4715d284..0000000000000 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_flatten_and_wrap_filters.js +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 _ from 'lodash'; -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import { FilterBarLibMapFlattenAndWrapFiltersProvider } from '../map_flatten_and_wrap_filters'; - -describe('Filter Bar Directive', function () { - describe('mapFlattenAndWrapFilters()', function () { - let mapFlattenAndWrapFilters; - let $rootScope; - - beforeEach(ngMock.module( - 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } - )); - - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - mapFlattenAndWrapFilters = Private(FilterBarLibMapFlattenAndWrapFiltersProvider); - $rootScope = _$rootScope_; - })); - - const filters = [ - null, - [ - { meta: { index: 'logstash-*' }, exists: { field: '_type' } }, - { meta: { index: 'logstash-*' }, missing: { field: '_type' } } - ], - { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } } }, - { meta: { index: 'logstash-*' }, range: { bytes: { lt: 2048, gt: 1024 } } }, - { meta: { index: 'logstash-*' }, query: { match: { _type: { query: 'apache', type: 'phrase' } } } } - ]; - - it('should map, flatten and wrap filters', function (done) { - mapFlattenAndWrapFilters(filters).then(function (results) { - expect(results).to.have.length(5); - _.each(results, function (filter) { - expect(filter).to.have.property('meta'); - expect(filter.meta).to.have.property('apply', true); - }); - done(); - }); - $rootScope.$apply(); - }); - - }); -}); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js index b12128929903b..453fa46fb7495 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js @@ -19,19 +19,15 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapMatchAllProvider } from '../map_match_all'; +import { checkIsMatchAll } from '../map_match_all'; describe('ui/filter_bar/lib', function () { describe('mapMatchAll()', function () { - let $rootScope; - let mapMatchAll; let filter; beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - $rootScope = _$rootScope_; - mapMatchAll = Private(FilterBarLibMapMatchAllProvider); + beforeEach(ngMock.inject(function () { filter = { match_all: {}, meta: { @@ -44,19 +40,20 @@ describe('ui/filter_bar/lib', function () { describe('when given a filter that is not match_all', function () { it('filter is rejected', function (done) { delete filter.match_all; - mapMatchAll(filter).catch(result => { + checkIsMatchAll(filter).catch(result => { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); }); describe('when given a match_all filter', function () { let result; - beforeEach(function () { - mapMatchAll(filter).then(r => result = r); - $rootScope.$apply(); + beforeEach(function (done) { + checkIsMatchAll(filter).then(r => { + result = r; + done(); + }); }); it('key is set to meta field', function () { diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js index 005b8883be51c..8e0c040ab3c84 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js @@ -17,39 +17,27 @@ * under the License. */ -import ngMock from 'ng_mock'; import expect from '@kbn/expect'; -import { FilterBarLibMapMissingProvider } from '../map_missing'; +import { checkIsMissing } from '../map_missing'; describe('Filter Bar Directive', function () { describe('mapMissing()', function () { - let mapMissing; - - let $rootScope; - beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - $rootScope = _$rootScope_; - mapMissing = Private(FilterBarLibMapMissingProvider); - })); - it('should return the key and value for matching filters', function (done) { const filter = { missing: { field: '_type' } }; - mapMissing(filter).then(function (result) { + checkIsMissing(filter).then(function (result) { expect(result).to.have.property('key', '_type'); expect(result).to.have.property('value', 'missing'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { const filter = { query: { match: { query: 'foo' } } }; - mapMissing(filter).catch(function (result) { + checkIsMissing(filter).catch(function (result) { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js index 056ba8b8a7de1..8e9b278388cc2 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js @@ -18,36 +18,27 @@ */ import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import { FilterBarLibMapQueryStringProvider } from '../map_query_string'; +// import ngMock from 'ng_mock'; +import { checkIsQueryString } from '../map_query_string'; describe('Filter Bar Directive', function () { describe('mapQueryString()', function () { - let mapQueryString; - let $rootScope; - beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - $rootScope = _$rootScope_; - mapQueryString = Private(FilterBarLibMapQueryStringProvider); - })); it('should return the key and value for matching filters', function (done) { const filter = { query: { query_string: { query: 'foo:bar' } } }; - mapQueryString(filter).then(function (result) { + checkIsQueryString(filter).then(function (result) { expect(result).to.have.property('key', 'query'); expect(result).to.have.property('value', 'foo:bar'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { const filter = { query: { match: { query: 'foo' } } }; - mapQueryString(filter).catch(function (result) { + checkIsQueryString(filter).catch(function (result) { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); }); From 04d7a8f1ba7ca355fed1f271c21e9cf2a942c7cd Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Mon, 22 Apr 2019 16:17:52 +0300 Subject: [PATCH 06/13] Fix filter bar lib tests 2 --- src/fixtures/mock_index_patterns.js | 4 +- .../lib/__tests__/extract_time_filter.js | 22 +++----- .../lib/__tests__/generate_mapping_chain.js | 54 ++++++------------- .../lib/__tests__/map_and_flatten_filters.js | 19 +++---- .../filter_bar/lib/__tests__/map_filter.js | 32 ++++------- .../lib/__tests__/map_geo_bounding_box.js | 19 +++---- .../lib/__tests__/map_geo_polygon.js | 19 +++---- .../filter_bar/lib/__tests__/map_phrase.js | 18 +++---- .../filter_bar/lib/__tests__/map_range.js | 19 +++---- .../filter_bar/lib/extract_time_filter.js | 2 +- .../ui/public/filter_bar/query_filter.js | 4 +- 11 files changed, 75 insertions(+), 137 deletions(-) diff --git a/src/fixtures/mock_index_patterns.js b/src/fixtures/mock_index_patterns.js index a370539b2878e..cd508136372a0 100644 --- a/src/fixtures/mock_index_patterns.js +++ b/src/fixtures/mock_index_patterns.js @@ -20,10 +20,10 @@ import sinon from 'sinon'; import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; -export default function (Private, Promise) { +export default function (Private) { const indexPatterns = Private(FixturesStubbedLogstashIndexPatternProvider); const getIndexPatternStub = sinon.stub() - .returns(Promise.resolve(indexPatterns)); + .resolves(indexPatterns); return { get: getIndexPatternStub, diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/extract_time_filter.js b/src/legacy/ui/public/filter_bar/lib/__tests__/extract_time_filter.js index 970d175526841..fe2a642199cd9 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/extract_time_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/extract_time_filter.js @@ -19,24 +19,20 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibExtractTimeFilterProvider } from '../extract_time_filter'; +import { extractTimeFilter } from '../extract_time_filter'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('extractTimeFilter()', function () { - let extractTimeFilter; - let $rootScope; + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - extractTimeFilter = Private(FilterBarLibExtractTimeFilterProvider); - $rootScope = _$rootScope_; + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); })); it('should return the matching filter for the default time field', function (done) { @@ -44,11 +40,10 @@ describe('Filter Bar Directive', function () { { meta: { index: 'logstash-*' }, query: { match: { _type: { query: 'apache', type: 'phrase' } } } }, { meta: { index: 'logstash-*' }, range: { 'time': { gt: 1388559600000, lt: 1388646000000 } } } ]; - extractTimeFilter(filters).then(function (filter) { + extractTimeFilter(mockIndexPatterns, filters).then(function (filter) { expect(filter).to.eql(filters[1]); done(); }); - $rootScope.$apply(); }); it('should not return the non-matching filter for the default time field', function (done) { @@ -56,11 +51,10 @@ describe('Filter Bar Directive', function () { { meta: { index: 'logstash-*' }, query: { match: { _type: { query: 'apache', type: 'phrase' } } } }, { meta: { index: 'logstash-*' }, range: { '@timestamp': { gt: 1388559600000, lt: 1388646000000 } } } ]; - extractTimeFilter(filters).then(function (filter) { + extractTimeFilter(mockIndexPatterns, filters).then(function (filter) { expect(filter).to.be(undefined); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/generate_mapping_chain.js b/src/legacy/ui/public/filter_bar/lib/__tests__/generate_mapping_chain.js index 49f70b457f996..3344b938181e5 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/generate_mapping_chain.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/generate_mapping_chain.js @@ -20,74 +20,57 @@ import sinon from 'sinon'; import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibGenerateMappingChainProvider } from '../generate_mapping_chain'; +import { generateMappingChain } from '../generate_mapping_chain'; describe('Filter Bar Directive', function () { describe('generateMappingChain()', function () { - - let generateMappingChain; - - let $rootScope; - let Promise; beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private, _$rootScope_, _Promise_) { - $rootScope = _$rootScope_; - Promise = _Promise_; - generateMappingChain = Private(FilterBarLibGenerateMappingChainProvider); - })); it('should create a chaining function which calls the next function if the promise is rejected', function (done) { const filter = {}; const mapping = sinon.stub(); - mapping.returns(Promise.reject(filter)); - const mappingChainFn = generateMappingChain(mapping); + mapping.rejects(filter); const next = sinon.stub(); - next.returns(Promise.resolve('good')); - const chain = mappingChainFn(next); + next.resolves('good'); + const chain = generateMappingChain(mapping, next); chain(filter).then(function (result) { expect(result).to.be('good'); sinon.assert.calledOnce(next); done(); }); - $rootScope.$apply(); }); it('should create a chaining function which DOES NOT call the next function if the result is resolved', function (done) { const mapping = sinon.stub(); - mapping.returns(Promise.resolve('good')); - const mappingChainFn = generateMappingChain(mapping); + mapping.resolves('good'); const next = sinon.stub(); - next.returns(Promise.resolve('bad')); - const chain = mappingChainFn(next); + next.resolves('bad'); + const chain = generateMappingChain(mapping, next); chain({}).then(function (result) { expect(result).to.be('good'); sinon.assert.notCalled(next); done(); }); - $rootScope.$apply(); }); it('should resolve result for the mapping function', function (done) { const mapping = sinon.stub(); - mapping.returns(Promise.resolve({ key: 'test', value: 'example' })); - const mappingChainFn = generateMappingChain(mapping); + mapping.resolves({ key: 'test', value: 'example' }); const next = sinon.stub(); - const chain = mappingChainFn(next); + const chain = generateMappingChain(mapping, next); chain({}).then(function (result) { sinon.assert.notCalled(next); expect(result).to.eql({ key: 'test', value: 'example' }); done(); }); - $rootScope.$apply(); }); it('should call the mapping function with the argument to the chain', function (done) { const mapping = sinon.stub(); - mapping.returns(Promise.resolve({ key: 'test', value: 'example' })); - const mappingChainFn = generateMappingChain(mapping); + mapping.resolves({ key: 'test', value: 'example' }); const next = sinon.stub(); - const chain = mappingChainFn(next); + const chain = generateMappingChain(mapping, next); chain({ test: 'example' }).then(function (result) { sinon.assert.calledOnce(mapping); expect(mapping.args[0][0]).to.eql({ test: 'example' }); @@ -95,38 +78,33 @@ describe('Filter Bar Directive', function () { expect(result).to.eql({ key: 'test', value: 'example' }); done(); }); - $rootScope.$apply(); }); it('should resolve result for the next function', function (done) { const filter = {}; const mapping = sinon.stub(); - mapping.returns(Promise.reject(filter)); - const mappingChainFn = generateMappingChain(mapping); + mapping.rejects(filter); const next = sinon.stub(); - next.returns(Promise.resolve({ key: 'test', value: 'example' })); - const chain = mappingChainFn(next); + next.resolves({ key: 'test', value: 'example' }); + const chain = generateMappingChain(mapping, next); chain(filter).then(function (result) { sinon.assert.calledOnce(mapping); sinon.assert.calledOnce(next); expect(result).to.eql({ key: 'test', value: 'example' }); done(); }); - $rootScope.$apply(); }); it('should reject with an error if no functions match', function (done) { const filter = {}; const mapping = sinon.stub(); - mapping.returns(Promise.reject(filter)); - const mappingChainFn = generateMappingChain(mapping); - const chain = mappingChainFn(); + mapping.rejects(filter); + const chain = generateMappingChain(mapping); chain(filter).catch(function (err) { expect(err).to.be.an(Error); expect(err.message).to.be('No mappings have been found for filter.'); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_and_flatten_filters.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_and_flatten_filters.js index f979c4fd1450e..2282e9fdf8f9a 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_and_flatten_filters.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_and_flatten_filters.js @@ -19,24 +19,20 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapAndFlattenFiltersProvider } from '../map_and_flatten_filters'; +import { mapAndFlattenFilters } from '../map_and_flatten_filters'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapAndFlattenFilters()', function () { - let mapAndFlattenFilters; - let $rootScope; + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - mapAndFlattenFilters = Private(FilterBarLibMapAndFlattenFiltersProvider); - $rootScope = _$rootScope_; + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); })); const filters = [ @@ -51,7 +47,7 @@ describe('Filter Bar Directive', function () { ]; it('should map and flatten the filters', function (done) { - mapAndFlattenFilters(filters).then(function (results) { + mapAndFlattenFilters(mockIndexPatterns, filters).then(function (results) { expect(results).to.have.length(5); expect(results[0]).to.have.property('meta'); expect(results[1]).to.have.property('meta'); @@ -70,7 +66,6 @@ describe('Filter Bar Directive', function () { expect(results[4].meta).to.have.property('value', 'apache'); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_filter.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_filter.js index 0e307e2669ac2..5867c3b75f10e 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_filter.js @@ -19,30 +19,25 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapFilterProvider } from '../map_filter'; +import { mapFilter } from '../map_filter'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { - let mapFilter; - let $rootScope; - + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (_$rootScope_, Private) { - mapFilter = Private(FilterBarLibMapFilterProvider); - $rootScope = _$rootScope_; + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); })); describe('mapFilter()', function () { it('should map query filters', function (done) { const before = { meta: { index: 'logstash-*' }, query: { match: { '_type': { query: 'apache' } } } }; - mapFilter(before).then(function (after) { + mapFilter(mockIndexPatterns, before).then(function (after) { expect(after).to.have.property('meta'); expect(after.meta).to.have.property('key', '_type'); expect(after.meta).to.have.property('value', 'apache'); @@ -50,12 +45,11 @@ describe('Filter Bar Directive', function () { expect(after.meta).to.have.property('negate', false); done(); }); - $rootScope.$apply(); }); it('should map exists filters', function (done) { const before = { meta: { index: 'logstash-*' }, exists: { field: '@timestamp' } }; - mapFilter(before).then(function (after) { + mapFilter(mockIndexPatterns, before).then(function (after) { expect(after).to.have.property('meta'); expect(after.meta).to.have.property('key', '@timestamp'); expect(after.meta).to.have.property('value', 'exists'); @@ -63,12 +57,11 @@ describe('Filter Bar Directive', function () { expect(after.meta).to.have.property('negate', false); done(); }); - $rootScope.$apply(); }); it('should map missing filters', function (done) { const before = { meta: { index: 'logstash-*' }, missing: { field: '@timestamp' } }; - mapFilter(before).then(function (after) { + mapFilter(mockIndexPatterns, before).then(function (after) { expect(after).to.have.property('meta'); expect(after.meta).to.have.property('key', '@timestamp'); expect(after.meta).to.have.property('value', 'missing'); @@ -76,12 +69,11 @@ describe('Filter Bar Directive', function () { expect(after.meta).to.have.property('negate', false); done(); }); - $rootScope.$apply(); }); it('should map json filter', function (done) { const before = { meta: { index: 'logstash-*' }, query: { match_all: {} } }; - mapFilter(before).then(function (after) { + mapFilter(mockIndexPatterns, before).then(function (after) { expect(after).to.have.property('meta'); expect(after.meta).to.have.property('key', 'query'); expect(after.meta).to.have.property('value', '{"match_all":{}}'); @@ -89,17 +81,15 @@ describe('Filter Bar Directive', function () { expect(after.meta).to.have.property('negate', false); done(); }); - $rootScope.$apply(); }); it('should finish with a catch', function (done) { const before = { meta: { index: 'logstash-*' } }; - mapFilter(before).catch(function (error) { + mapFilter(mockIndexPatterns, before).catch(function (error) { expect(error).to.be.an(Error); expect(error.message).to.be('No mappings have been found for filter.'); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js index 6f9d757f4b9b8..02417ae6403fb 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js @@ -19,24 +19,22 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapGeoBoundingBoxProvider } from '../map_geo_bounding_box'; +import { checkIsGeoBoundingBox } from '../map_geo_bounding_box'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapGeoBoundingBox()', function () { let mapGeoBoundingBox; - let $rootScope; + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - mapGeoBoundingBox = Private(FilterBarLibMapGeoBoundingBoxProvider); - $rootScope = _$rootScope_; + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); + mapGeoBoundingBox = checkIsGeoBoundingBox(mockIndexPatterns); })); it('should return the key and value for matching filters with bounds', function (done) { @@ -58,7 +56,6 @@ describe('Filter Bar Directive', function () { expect(result.value.replace(/&[a-z]+?;/g, '').replace(/[^a-z0-9]/g, '')).to.be('lat5lon10tolat15lon20'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { @@ -67,7 +64,6 @@ describe('Filter Bar Directive', function () { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); it('should return the key and value even when using ignore_unmapped', function (done) { @@ -90,7 +86,6 @@ describe('Filter Bar Directive', function () { expect(result.value.replace(/&[a-z]+?;/g, '').replace(/[^a-z0-9]/g, '')).to.be('lat5lon10tolat15lon20'); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js index 6b17ac5b6b691..ffb27ebd69a1e 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js @@ -19,24 +19,22 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapGeoPolygonProvider } from '../map_geo_polygon'; +import { checkIsGeoPolygon } from '../map_geo_polygon'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapGeoPolygon()', function () { let mapGeoPolygon; - let $rootScope; + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - mapGeoPolygon = Private(FilterBarLibMapGeoPolygonProvider); - $rootScope = _$rootScope_; + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); + mapGeoPolygon = checkIsGeoPolygon(mockIndexPatterns); })); it('should return the key and value for matching filters with bounds', function (done) { @@ -60,7 +58,6 @@ describe('Filter Bar Directive', function () { expect(result.value.replace(/&[a-z]+?;/g, '').replace(/[^a-z0-9]/g, '')).to.be('lat5lon10lat15lon20'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { @@ -69,7 +66,6 @@ describe('Filter Bar Directive', function () { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); it('should return the key and value even when using ignore_unmapped', function (done) { @@ -94,7 +90,6 @@ describe('Filter Bar Directive', function () { expect(result.value.replace(/&[a-z]+?;/g, '').replace(/[^a-z0-9]/g, '')).to.be('lat5lon10lat15lon20'); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js index 4a0262d93a194..f838d25d70095 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js @@ -19,24 +19,22 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapPhraseProvider } from '../map_phrase'; +import { checkIsPhrase } from '../map_phrase'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapPhrase()', function () { let mapPhrase; - let $rootScope; + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - $rootScope = _$rootScope_; - mapPhrase = Private(FilterBarLibMapPhraseProvider); + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); + mapPhrase = checkIsPhrase(mockIndexPatterns); })); it('should return the key and value for matching filters', function (done) { @@ -46,7 +44,6 @@ describe('Filter Bar Directive', function () { expect(result).to.have.property('value', 'apache'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { @@ -55,7 +52,6 @@ describe('Filter Bar Directive', function () { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js index 866a692751170..06f9369551c54 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js @@ -19,24 +19,22 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { FilterBarLibMapRangeProvider } from '../map_range'; +import { checkIsRange } from '../map_range'; +import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapRange()', function () { let mapRange; - let $rootScope; + let mockIndexPatterns; beforeEach(ngMock.module( 'kibana', - 'kibana/courier', - function ($provide) { - $provide.service('indexPatterns', require('fixtures/mock_index_patterns')); - } + 'kibana/courier' )); - beforeEach(ngMock.inject(function (Private, _$rootScope_) { - mapRange = Private(FilterBarLibMapRangeProvider); - $rootScope = _$rootScope_; + beforeEach(ngMock.inject(function (Private) { + mockIndexPatterns = Private(IndexPatternMock); + mapRange = checkIsRange(mockIndexPatterns); })); it('should return the key and value for matching filters with gt/lt', function (done) { @@ -46,7 +44,6 @@ describe('Filter Bar Directive', function () { expect(result).to.have.property('value', '1,024 to 2,048'); done(); }); - $rootScope.$apply(); }); it('should return the key and value for matching filters with gte/lte', function (done) { @@ -56,7 +53,6 @@ describe('Filter Bar Directive', function () { expect(result).to.have.property('value', '1,024 to 2,048'); done(); }); - $rootScope.$apply(); }); it('should return undefined for none matching', function (done) { @@ -65,7 +61,6 @@ describe('Filter Bar Directive', function () { expect(result).to.be(filter); done(); }); - $rootScope.$apply(); }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js b/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js index a42cd43b306fd..3d3fb92046507 100644 --- a/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/extract_time_filter.js @@ -19,7 +19,7 @@ import _ from 'lodash'; -export async function getExtractTimeFilter(indexPatterns, filters) { +export async function extractTimeFilter(indexPatterns, filters) { // Assume all the index patterns are the same since they will be added // from the same visualization. const id = _.get(filters, '[0].meta.index'); diff --git a/src/legacy/ui/public/filter_bar/query_filter.js b/src/legacy/ui/public/filter_bar/query_filter.js index 3fa762dd54a4b..39ff7f6a18592 100644 --- a/src/legacy/ui/public/filter_bar/query_filter.js +++ b/src/legacy/ui/public/filter_bar/query_filter.js @@ -24,7 +24,7 @@ import { uniqFilters } from './lib/uniq_filters'; import { compareFilters } from './lib/compare_filters'; import { EventsProvider } from '../events'; import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; -import { getExtractTimeFilter } from './lib/extract_time_filter'; +import { extractTimeFilter } from './lib/extract_time_filter'; import { changeTimeFilter } from './lib/change_time_filter'; export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, getAppState, globalState, config) { @@ -230,7 +230,7 @@ export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, }; queryFilter.addFiltersAndChangeTimeFilter = async filters => { - const timeFilter = await getExtractTimeFilter(indexPatterns, filters); + const timeFilter = await extractTimeFilter(indexPatterns, filters); if (timeFilter) changeTimeFilter(timeFilter); queryFilter.addFilters(filters.filter(filter => filter !== timeFilter)); }; From d3ec3e958e44f004ca2062fb1ea14ef528f72865 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Mon, 22 Apr 2019 16:33:14 +0300 Subject: [PATCH 07/13] Deleted unused push_filter --- .../filter_bar/__tests__/push_filter.js | 87 ------------------- .../ui/public/filter_bar/push_filter.js | 34 -------- 2 files changed, 121 deletions(-) delete mode 100644 src/legacy/ui/public/filter_bar/__tests__/push_filter.js delete mode 100644 src/legacy/ui/public/filter_bar/push_filter.js diff --git a/src/legacy/ui/public/filter_bar/__tests__/push_filter.js b/src/legacy/ui/public/filter_bar/__tests__/push_filter.js deleted file mode 100644 index 81b418e5665c6..0000000000000 --- a/src/legacy/ui/public/filter_bar/__tests__/push_filter.js +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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 expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import { FilterBarPushFilterProvider } from '../push_filter'; -describe('Filter Bar pushFilter()', function () { - - let pushFilterFn; - - beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private) { - pushFilterFn = Private(FilterBarPushFilterProvider); - })); - - it('is a function that returns a function', function () { - expect(pushFilterFn).to.be.a(Function); - expect(pushFilterFn({})).to.be.a(Function); - }); - - it('throws an error if passed something besides an object', function () { - expect(pushFilterFn).withArgs(true).to.throwError(); - }); - - describe('pushFilter($state)()', function () { - let $state; - let pushFilter; - let filter; - - beforeEach(ngMock.inject(function () { - $state = { $newFilters: [] }; - pushFilter = pushFilterFn($state); - filter = { query: { query_string: { query: '' } } }; - })); - - it('should create the filters property it needed', function () { - const altState = {}; - pushFilterFn(altState)(filter); - expect(altState.$newFilters).to.be.an(Array); - }); - - it('should replace the filters property instead of modifying it', function () { - // If we push directly instead of using pushFilter a $watch('filters') does not trigger - - let oldFilters; - - oldFilters = $state.$newFilters; - $state.$newFilters.push(filter); - expect($state.$newFilters).to.equal(oldFilters); // Same object - - oldFilters = $state.$newFilters; - pushFilter(filter); - expect($state.$newFilters).to.not.equal(oldFilters); // New object! - }); - - it('should add meta data to the filter', function () { - pushFilter(filter, true, 'myIndex'); - expect($state.$newFilters[0].meta).to.be.an(Object); - - expect($state.$newFilters[0].meta.negate).to.be(true); - expect($state.$newFilters[0].meta.index).to.be('myIndex'); - - pushFilter(filter, false, 'myIndex'); - expect($state.$newFilters[0].meta.negate).to.be(false); - }); - - - - }); - -}); diff --git a/src/legacy/ui/public/filter_bar/push_filter.js b/src/legacy/ui/public/filter_bar/push_filter.js deleted file mode 100644 index 265d9c4785064..0000000000000 --- a/src/legacy/ui/public/filter_bar/push_filter.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 _ from 'lodash'; - -export function FilterBarPushFilterProvider() { - return function ($state) { - if (!_.isObject($state)) throw new Error('pushFilters requires a state object'); - return function (filter, negate, index) { - // Hierarchical and tabular data set their aggConfigResult parameter - // differently because of how the point is rewritten between the two. So - // we need to check if the point.orig is set, if not use try the point.aggConfigResult - const pendingFilter = { meta: { negate: negate, index: index } }; - _.extend(pendingFilter, filter); - $state.$newFilters = [pendingFilter]; - }; - }; -} From 7a44ec14cb1bd58fe230d72d64ca56f726ef48da Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Mon, 22 Apr 2019 17:49:06 +0300 Subject: [PATCH 08/13] Changed push filters not to be a provider --- src/legacy/ui/public/filter_bar/push_filters.js | 12 +++++------- src/legacy/ui/public/vis/vis_filters.js | 5 ++--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/push_filters.js b/src/legacy/ui/public/filter_bar/push_filters.js index cce206c922b7b..771de14f9446d 100644 --- a/src/legacy/ui/public/filter_bar/push_filters.js +++ b/src/legacy/ui/public/filter_bar/push_filters.js @@ -19,11 +19,9 @@ import _ from 'lodash'; -export function FilterBarPushFiltersProvider() { - return function ($state) { - if (!_.isObject($state)) throw new Error('pushFilters requires a state object'); - return function (filters) { - $state.$newFilters = filters; - }; - }; +// TODO: should it be here or in vis filters (only place where it's used). +// $newFilters is not defined by filter_bar as well. +export function pushFilterBarFilters($state, filters) { + if (!_.isObject($state)) throw new Error('pushFilters requires a state object'); + $state.$newFilters = filters; } diff --git a/src/legacy/ui/public/vis/vis_filters.js b/src/legacy/ui/public/vis/vis_filters.js index 1a43f35e8537a..f10a490b06192 100644 --- a/src/legacy/ui/public/vis/vis_filters.js +++ b/src/legacy/ui/public/vis/vis_filters.js @@ -18,7 +18,7 @@ */ import _ from 'lodash'; -import { FilterBarPushFiltersProvider } from '../filter_bar/push_filters'; +import { pushFilterBarFilters } from '../filter_bar/push_filters'; import { FilterBarQueryFilterProvider } from '../filter_bar/query_filter'; import { onBrushEvent } from '../utils/brush_event'; @@ -84,7 +84,6 @@ const createFilter = (aggConfigs, table, columnIndex, rowIndex, cellValue) => { }; const VisFiltersProvider = (Private, getAppState) => { - const filterBarPushFilters = Private(FilterBarPushFiltersProvider); const queryFilter = Private(FilterBarQueryFilterProvider); const pushFilters = (filters, simulate) => { @@ -92,7 +91,7 @@ const VisFiltersProvider = (Private, getAppState) => { if (filters.length && !simulate) { const flatFilters = _.flatten(filters); const deduplicatedFilters = flatFilters.filter((v, i) => i === flatFilters.findIndex(f => _.isEqual(v, f))); - filterBarPushFilters(appState)(deduplicatedFilters); + pushFilterBarFilters(appState, deduplicatedFilters); } }; From 0eacdb48a83a8aceb79aeaca2a7d1076ecb94566 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Wed, 24 Apr 2019 19:40:19 +0300 Subject: [PATCH 09/13] Added wrapping in Promise resolve to stabilized tests --- src/legacy/ui/public/filter_bar/query_filter.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/query_filter.js b/src/legacy/ui/public/filter_bar/query_filter.js index 39ff7f6a18592..d651bf7df9dbd 100644 --- a/src/legacy/ui/public/filter_bar/query_filter.js +++ b/src/legacy/ui/public/filter_bar/query_filter.js @@ -27,7 +27,7 @@ import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; import { extractTimeFilter } from './lib/extract_time_filter'; import { changeTimeFilter } from './lib/change_time_filter'; -export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, getAppState, globalState, config) { +export function FilterBarQueryFilterProvider(Private, Promise, indexPatterns, $rootScope, getAppState, globalState, config) { const EventEmitter = Private(EventsProvider); const queryFilter = new EventEmitter(); @@ -83,14 +83,14 @@ export function FilterBarQueryFilterProvider(Private, indexPatterns, $rootScope, filters = [filters]; } - return mapAndFlattenFilters(indexPatterns, filters) + return Promise.resolve(mapAndFlattenFilters(indexPatterns, filters) .then(function (filters) { if (!filterState.filters) { filterState.filters = []; } filterState.filters = filterState.filters.concat(filters); - }); + })); }; /** From d399e4b94dc20d07c5518a8e1f4ecb7eea999a9f Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Mon, 29 Apr 2019 15:55:16 +0300 Subject: [PATCH 10/13] Renamed mapper functions --- .../filter_bar/lib/__tests__/map_default.js | 8 ++-- .../filter_bar/lib/__tests__/map_exists.js | 6 +-- .../lib/__tests__/map_geo_bounding_box.js | 12 +++--- .../lib/__tests__/map_geo_polygon.js | 12 +++--- .../filter_bar/lib/__tests__/map_match_all.js | 6 +-- .../filter_bar/lib/__tests__/map_missing.js | 6 +-- .../filter_bar/lib/__tests__/map_phrase.js | 10 ++--- .../lib/__tests__/map_query_string.js | 6 +-- .../filter_bar/lib/__tests__/map_range.js | 10 ++--- .../ui/public/filter_bar/lib/map_default.js | 2 +- .../ui/public/filter_bar/lib/map_exists.js | 2 +- .../ui/public/filter_bar/lib/map_filter.js | 40 +++++++++---------- .../filter_bar/lib/map_geo_bounding_box.js | 2 +- .../public/filter_bar/lib/map_geo_polygon.js | 2 +- .../ui/public/filter_bar/lib/map_match_all.js | 2 +- .../ui/public/filter_bar/lib/map_missing.js | 2 +- .../ui/public/filter_bar/lib/map_phrase.js | 2 +- .../ui/public/filter_bar/lib/map_phrases.js | 2 +- .../public/filter_bar/lib/map_query_string.js | 2 +- .../ui/public/filter_bar/lib/map_range.js | 2 +- 20 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js index aef94892698f1..d97860ade56f5 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_default.js @@ -18,14 +18,14 @@ */ import expect from '@kbn/expect'; -import { checkIsDefault } from '../map_default'; +import { mapDefault } from '../map_default'; describe('Filter Bar Directive', function () { describe('mapDefault()', function () { it('should return the key and value for matching filters', function (done) { const filter = { query: { match_all: {} } }; - checkIsDefault(filter).then(function (result) { + mapDefault(filter).then(function (result) { expect(result).to.have.property('key', 'query'); expect(result).to.have.property('value', '{"match_all":{}}'); done(); @@ -42,7 +42,7 @@ describe('Filter Bar Directive', function () { } } }; - checkIsDefault(filter).then(function (result) { + mapDefault(filter).then(function (result) { expect(result).to.have.property('key', 'bool'); expect(result).to.have.property('value', JSON.stringify(filter.bool)); done(); @@ -51,7 +51,7 @@ describe('Filter Bar Directive', function () { it('should return undefined if there is no valid key', function (done) { const filter = { meta: {} }; - checkIsDefault(filter).catch(function (result) { + mapDefault(filter).catch(function (result) { expect(result).to.be(filter); done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js index 8bf076cbf26cc..b9ff72e6fb22c 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_exists.js @@ -19,7 +19,7 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { checkIsExists } from '../map_exists'; +import { mapExists } from '../map_exists'; describe('Filter Bar Directive', function () { describe('mapExists()', function () { @@ -28,7 +28,7 @@ describe('Filter Bar Directive', function () { it('should return the key and value for matching filters', function (done) { const filter = { exists: { field: '_type' } }; - checkIsExists(filter).then(function (result) { + mapExists(filter).then(function (result) { expect(result).to.have.property('key', '_type'); expect(result).to.have.property('value', 'exists'); done(); @@ -37,7 +37,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { query: { match: { query: 'foo' } } }; - checkIsExists(filter).catch(function (result) { + mapExists(filter).catch(function (result) { expect(result).to.be(filter); done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js index 02417ae6403fb..697402942b853 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_bounding_box.js @@ -19,12 +19,12 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { checkIsGeoBoundingBox } from '../map_geo_bounding_box'; +import { mapGeoBoundingBox } from '../map_geo_bounding_box'; import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapGeoBoundingBox()', function () { - let mapGeoBoundingBox; + let mapGeoBoundingBoxFn; let mockIndexPatterns; beforeEach(ngMock.module( @@ -34,7 +34,7 @@ describe('Filter Bar Directive', function () { beforeEach(ngMock.inject(function (Private) { mockIndexPatterns = Private(IndexPatternMock); - mapGeoBoundingBox = checkIsGeoBoundingBox(mockIndexPatterns); + mapGeoBoundingBoxFn = mapGeoBoundingBox(mockIndexPatterns); })); it('should return the key and value for matching filters with bounds', function (done) { @@ -49,7 +49,7 @@ describe('Filter Bar Directive', function () { } } }; - mapGeoBoundingBox(filter).then(function (result) { + mapGeoBoundingBoxFn(filter).then(function (result) { expect(result).to.have.property('key', 'point'); expect(result).to.have.property('value'); // remove html entities and non-alphanumerics to get the gist of the value @@ -60,7 +60,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } } }; - mapGeoBoundingBox(filter).catch(function (result) { + mapGeoBoundingBoxFn(filter).catch(function (result) { expect(result).to.be(filter); done(); }); @@ -79,7 +79,7 @@ describe('Filter Bar Directive', function () { } } }; - mapGeoBoundingBox(filter).then(function (result) { + mapGeoBoundingBoxFn(filter).then(function (result) { expect(result).to.have.property('key', 'point'); expect(result).to.have.property('value'); // remove html entities and non-alphanumerics to get the gist of the value diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js index ffb27ebd69a1e..9526bee500139 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_geo_polygon.js @@ -19,12 +19,12 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { checkIsGeoPolygon } from '../map_geo_polygon'; +import { mapGeoPolygon } from '../map_geo_polygon'; import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapGeoPolygon()', function () { - let mapGeoPolygon; + let mapGeoPolygonFn; let mockIndexPatterns; beforeEach(ngMock.module( @@ -34,7 +34,7 @@ describe('Filter Bar Directive', function () { beforeEach(ngMock.inject(function (Private) { mockIndexPatterns = Private(IndexPatternMock); - mapGeoPolygon = checkIsGeoPolygon(mockIndexPatterns); + mapGeoPolygonFn = mapGeoPolygon(mockIndexPatterns); })); it('should return the key and value for matching filters with bounds', function (done) { @@ -51,7 +51,7 @@ describe('Filter Bar Directive', function () { } } }; - mapGeoPolygon(filter).then(function (result) { + mapGeoPolygonFn(filter).then(function (result) { expect(result).to.have.property('key', 'point'); expect(result).to.have.property('value'); // remove html entities and non-alphanumerics to get the gist of the value @@ -62,7 +62,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } } }; - mapGeoPolygon(filter).catch(function (result) { + mapGeoPolygonFn(filter).catch(function (result) { expect(result).to.be(filter); done(); }); @@ -83,7 +83,7 @@ describe('Filter Bar Directive', function () { } } }; - mapGeoPolygon(filter).then(function (result) { + mapGeoPolygonFn(filter).then(function (result) { expect(result).to.have.property('key', 'point'); expect(result).to.have.property('value'); // remove html entities and non-alphanumerics to get the gist of the value diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js index 453fa46fb7495..d1b340611b898 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_match_all.js @@ -19,7 +19,7 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { checkIsMatchAll } from '../map_match_all'; +import { mapMatchAll } from '../map_match_all'; describe('ui/filter_bar/lib', function () { describe('mapMatchAll()', function () { @@ -40,7 +40,7 @@ describe('ui/filter_bar/lib', function () { describe('when given a filter that is not match_all', function () { it('filter is rejected', function (done) { delete filter.match_all; - checkIsMatchAll(filter).catch(result => { + mapMatchAll(filter).catch(result => { expect(result).to.be(filter); done(); }); @@ -50,7 +50,7 @@ describe('ui/filter_bar/lib', function () { describe('when given a match_all filter', function () { let result; beforeEach(function (done) { - checkIsMatchAll(filter).then(r => { + mapMatchAll(filter).then(r => { result = r; done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js index 8e0c040ab3c84..a05c21b6928fb 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_missing.js @@ -18,14 +18,14 @@ */ import expect from '@kbn/expect'; -import { checkIsMissing } from '../map_missing'; +import { mapMissing } from '../map_missing'; describe('Filter Bar Directive', function () { describe('mapMissing()', function () { it('should return the key and value for matching filters', function (done) { const filter = { missing: { field: '_type' } }; - checkIsMissing(filter).then(function (result) { + mapMissing(filter).then(function (result) { expect(result).to.have.property('key', '_type'); expect(result).to.have.property('value', 'missing'); done(); @@ -34,7 +34,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { query: { match: { query: 'foo' } } }; - checkIsMissing(filter).catch(function (result) { + mapMissing(filter).catch(function (result) { expect(result).to.be(filter); done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js index f838d25d70095..0d6a91ef74b6a 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_phrase.js @@ -19,12 +19,12 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { checkIsPhrase } from '../map_phrase'; +import { mapPhrase } from '../map_phrase'; import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapPhrase()', function () { - let mapPhrase; + let mapPhraseFn; let mockIndexPatterns; beforeEach(ngMock.module( @@ -34,12 +34,12 @@ describe('Filter Bar Directive', function () { beforeEach(ngMock.inject(function (Private) { mockIndexPatterns = Private(IndexPatternMock); - mapPhrase = checkIsPhrase(mockIndexPatterns); + mapPhraseFn = mapPhrase(mockIndexPatterns); })); it('should return the key and value for matching filters', function (done) { const filter = { meta: { index: 'logstash-*' }, query: { match: { _type: { query: 'apache', type: 'phrase' } } } }; - mapPhrase(filter).then(function (result) { + mapPhraseFn(filter).then(function (result) { expect(result).to.have.property('key', '_type'); expect(result).to.have.property('value', 'apache'); done(); @@ -48,7 +48,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } } }; - mapPhrase(filter).catch(function (result) { + mapPhraseFn(filter).catch(function (result) { expect(result).to.be(filter); done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js index 8e9b278388cc2..d7804e908993b 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js @@ -19,14 +19,14 @@ import expect from '@kbn/expect'; // import ngMock from 'ng_mock'; -import { checkIsQueryString } from '../map_query_string'; +import { mapQueryString } from '../map_query_string'; describe('Filter Bar Directive', function () { describe('mapQueryString()', function () { it('should return the key and value for matching filters', function (done) { const filter = { query: { query_string: { query: 'foo:bar' } } }; - checkIsQueryString(filter).then(function (result) { + mapQueryString(filter).then(function (result) { expect(result).to.have.property('key', 'query'); expect(result).to.have.property('value', 'foo:bar'); done(); @@ -35,7 +35,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { query: { match: { query: 'foo' } } }; - checkIsQueryString(filter).catch(function (result) { + mapQueryString(filter).catch(function (result) { expect(result).to.be(filter); done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js index 06f9369551c54..99985016fb026 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js @@ -19,12 +19,12 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; -import { checkIsRange } from '../map_range'; +import { mapRange } from '../map_range'; import IndexPatternMock from 'fixtures/mock_index_patterns'; describe('Filter Bar Directive', function () { describe('mapRange()', function () { - let mapRange; + let mapRangeFn; let mockIndexPatterns; beforeEach(ngMock.module( @@ -34,7 +34,7 @@ describe('Filter Bar Directive', function () { beforeEach(ngMock.inject(function (Private) { mockIndexPatterns = Private(IndexPatternMock); - mapRange = checkIsRange(mockIndexPatterns); + mapRangeFn = mapRange(mockIndexPatterns); })); it('should return the key and value for matching filters with gt/lt', function (done) { @@ -48,7 +48,7 @@ describe('Filter Bar Directive', function () { it('should return the key and value for matching filters with gte/lte', function (done) { const filter = { meta: { index: 'logstash-*' }, range: { bytes: { lte: 2048, gte: 1024 } } }; - mapRange(filter).then(function (result) { + mapRangeFn(filter).then(function (result) { expect(result).to.have.property('key', 'bytes'); expect(result).to.have.property('value', '1,024 to 2,048'); done(); @@ -57,7 +57,7 @@ describe('Filter Bar Directive', function () { it('should return undefined for none matching', function (done) { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } } }; - mapRange(filter).catch(function (result) { + mapRangeFn(filter).catch(function (result) { expect(result).to.be(filter); done(); }); diff --git a/src/legacy/ui/public/filter_bar/lib/map_default.js b/src/legacy/ui/public/filter_bar/lib/map_default.js index b0733b1d6d7bb..6b9b09829c839 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_default.js +++ b/src/legacy/ui/public/filter_bar/lib/map_default.js @@ -20,7 +20,7 @@ import angular from 'angular'; import _ from 'lodash'; -export async function checkIsDefault(filter) { +export async function mapDefault(filter) { const metaProperty = /(^\$|meta)/; const key = _.find(_.keys(filter), function (key) { diff --git a/src/legacy/ui/public/filter_bar/lib/map_exists.js b/src/legacy/ui/public/filter_bar/lib/map_exists.js index 6ad2daceca026..3e26d93f20433 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_exists.js +++ b/src/legacy/ui/public/filter_bar/lib/map_exists.js @@ -17,7 +17,7 @@ * under the License. */ -export async function checkIsExists(filter) { +export async function mapExists(filter) { if (filter.exists) { const type = 'exists'; const key = filter.exists.field; diff --git a/src/legacy/ui/public/filter_bar/lib/map_filter.js b/src/legacy/ui/public/filter_bar/lib/map_filter.js index c3fe5d2085e42..82197f770c2eb 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/map_filter.js @@ -18,16 +18,16 @@ */ import _ from 'lodash'; -import { checkIsMatchAll } from './map_match_all'; -import { checkIsPhrase } from './map_phrase'; -import { checkIsPhrases } from './map_phrases'; -import { checkIsRange } from './map_range'; -import { checkIsExists } from './map_exists'; -import { checkIsMissing } from './map_missing'; -import { checkIsQueryString } from './map_query_string'; -import { checkIsGeoBoundingBox } from './map_geo_bounding_box'; -import { checkIsGeoPolygon } from './map_geo_polygon'; -import { checkIsDefault } from './map_default'; +import { mapMatchAll } from './map_match_all'; +import { mapPhrase } from './map_phrase'; +import { mapPhrases } from './map_phrases'; +import { mapRange } from './map_range'; +import { mapExists } from './map_exists'; +import { mapMissing } from './map_missing'; +import { mapQueryString } from './map_query_string'; +import { mapGeoBoundingBox } from './map_geo_bounding_box'; +import { mapGeoPolygon } from './map_geo_polygon'; +import { mapDefault } from './map_default'; import { generateMappingChain } from './generate_mapping_chain'; export async function mapFilter(indexPatterns, filter) { @@ -48,16 +48,16 @@ export async function mapFilter(indexPatterns, filter) { // that either handles the mapping operation or not // and add it here. ProTip: These are executed in order listed const mappers = [ - checkIsMatchAll, - checkIsRange(indexPatterns), - checkIsPhrase(indexPatterns), - checkIsPhrases, - checkIsExists, - checkIsMissing, - checkIsQueryString, - checkIsGeoBoundingBox(indexPatterns), - checkIsGeoPolygon(indexPatterns), - checkIsDefault, + mapMatchAll, + mapRange(indexPatterns), + mapPhrase(indexPatterns), + mapPhrases, + mapExists, + mapMissing, + mapQueryString, + mapGeoBoundingBox(indexPatterns), + mapGeoPolygon(indexPatterns), + mapDefault, ]; const noop = function () { diff --git a/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js b/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js index 69ed21d9fe5eb..89c3ccd96f95c 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js +++ b/src/legacy/ui/public/filter_bar/lib/map_geo_bounding_box.js @@ -40,7 +40,7 @@ function getParams(filter, indexPattern) { return { type, key, value, params }; } -export function checkIsGeoBoundingBox(indexPatterns) { +export function mapGeoBoundingBox(indexPatterns) { return async function (filter) { if (!filter.geo_bounding_box) { throw filter; diff --git a/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js b/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js index 4c35d34ea9245..1ff1446d2ab72 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js +++ b/src/legacy/ui/public/filter_bar/lib/map_geo_polygon.js @@ -39,7 +39,7 @@ function getParams(filter, indexPattern) { return { type, key, value, params }; } -export function checkIsGeoPolygon(indexPatterns) { +export function mapGeoPolygon(indexPatterns) { return async function (filter) { if (!filter.geo_polygon) { throw filter; diff --git a/src/legacy/ui/public/filter_bar/lib/map_match_all.js b/src/legacy/ui/public/filter_bar/lib/map_match_all.js index d3464ab83db8b..2e830946dcd03 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_match_all.js +++ b/src/legacy/ui/public/filter_bar/lib/map_match_all.js @@ -17,7 +17,7 @@ * under the License. */ -export async function checkIsMatchAll(filter) { +export async function mapMatchAll(filter) { if (filter.match_all) { const type = 'match_all'; const key = filter.meta.field; diff --git a/src/legacy/ui/public/filter_bar/lib/map_missing.js b/src/legacy/ui/public/filter_bar/lib/map_missing.js index de2b08d73ae70..e2476fc003a47 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_missing.js +++ b/src/legacy/ui/public/filter_bar/lib/map_missing.js @@ -17,7 +17,7 @@ * under the License. */ -export async function checkIsMissing(filter) { +export async function mapMissing(filter) { if (filter.missing) { const type = 'missing'; const key = filter.missing.field; diff --git a/src/legacy/ui/public/filter_bar/lib/map_phrase.js b/src/legacy/ui/public/filter_bar/lib/map_phrase.js index 992054202e969..3082719ac983e 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_phrase.js +++ b/src/legacy/ui/public/filter_bar/lib/map_phrase.js @@ -40,7 +40,7 @@ function getParams(filter, indexPattern) { return { type, key, value, params }; } -export function checkIsPhrase(indexPatterns) { +export function mapPhrase(indexPatterns) { return async function (filter) { const isScriptedPhraseFilter = isScriptedPhrase(filter); if (!_.has(filter, ['query', 'match']) && !isScriptedPhraseFilter) { diff --git a/src/legacy/ui/public/filter_bar/lib/map_phrases.js b/src/legacy/ui/public/filter_bar/lib/map_phrases.js index cfd6c85655dde..88c0272c71805 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_phrases.js +++ b/src/legacy/ui/public/filter_bar/lib/map_phrases.js @@ -17,7 +17,7 @@ * under the License. */ -export async function checkIsPhrases(filter) { +export async function mapPhrases(filter) { const { type, key, value, params } = filter.meta; if (type !== 'phrases') { throw filter; diff --git a/src/legacy/ui/public/filter_bar/lib/map_query_string.js b/src/legacy/ui/public/filter_bar/lib/map_query_string.js index 047fd712e14ee..adb6db3d03143 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_query_string.js +++ b/src/legacy/ui/public/filter_bar/lib/map_query_string.js @@ -17,7 +17,7 @@ * under the License. */ -export async function checkIsQueryString(filter) { +export async function mapQueryString(filter) { if (filter.query && filter.query.query_string) { const type = 'query_string'; const key = 'query'; diff --git a/src/legacy/ui/public/filter_bar/lib/map_range.js b/src/legacy/ui/public/filter_bar/lib/map_range.js index 288965e897d99..e413671ce4350 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_range.js +++ b/src/legacy/ui/public/filter_bar/lib/map_range.js @@ -51,7 +51,7 @@ function getParams(filter, indexPattern) { return { type, key, value, params }; } -export function checkIsRange(indexPatterns) { +export function mapRange(indexPatterns) { return async function (filter) { const isScriptedRangeFilter = isScriptedRange(filter); if (!filter.range && !isScriptedRangeFilter) { From a614e17402ce611447b015b16f348a1b30638094 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Wed, 1 May 2019 14:54:57 +0300 Subject: [PATCH 11/13] stabilized karma tests --- .../filter_bar/__tests__/_add_filters.js | 61 +++++++++---------- .../filter_bar/lib/__tests__/map_range.js | 2 +- .../filter_bar/lib/map_and_flatten_filters.js | 6 +- .../ui/public/filter_bar/query_filter.js | 6 +- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js b/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js index 49fbafdf4ee92..ab0c1de161801 100644 --- a/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js +++ b/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js @@ -25,12 +25,18 @@ import MockState from 'fixtures/mock_state'; import { FilterBarQueryFilterProvider } from '../query_filter'; describe('add filters', function () { + require('test_utils/no_digest_promises').activateForSuite(); + let filters; let queryFilter; let $rootScope; let appState; let globalState; + function timeout(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + beforeEach(ngMock.module( 'kibana', 'kibana/courier', @@ -73,44 +79,33 @@ describe('add filters', function () { }); describe('adding filters', function () { - it('should add filters to appState', function () { - $rootScope.$digest(); - - queryFilter.addFilters(filters); - $rootScope.$digest(); - + it('should add filters to appState', async function () { + await queryFilter.addFilters(filters); expect(appState.filters.length).to.be(3); expect(globalState.filters.length).to.be(0); }); - it('should add filters to globalState', function () { - $rootScope.$digest(); - - queryFilter.addFilters(filters, true); - $rootScope.$digest(); + it('should add filters to globalState', async function () { + await queryFilter.addFilters(filters, true); expect(appState.filters.length).to.be(0); expect(globalState.filters.length).to.be(3); }); - it('should accept a single filter', function () { - $rootScope.$digest(); - - queryFilter.addFilters(filters[0]); - $rootScope.$digest(); + it('should accept a single filter', async function () { + await queryFilter.addFilters(filters[0]); expect(appState.filters.length).to.be(1); expect(globalState.filters.length).to.be(0); }); - it('should allow overwriting a positive filter by a negated one', () => { - $rootScope.$digest(); + it('should allow overwriting a positive filter by a negated one', async function () { // Add negate: false version of the filter const filter = _.cloneDeep(filters[0]); filter.meta.negate = false; - queryFilter.addFilters(filter); + await queryFilter.addFilters(filter); $rootScope.$digest(); expect(appState.filters.length).to.be(1); expect(appState.filters[0]).to.eql(filter); @@ -119,22 +114,21 @@ describe('add filters', function () { const negatedFilter = _.cloneDeep(filters[0]); negatedFilter.meta.negate = true; - queryFilter.addFilters(negatedFilter); + await queryFilter.addFilters(negatedFilter); $rootScope.$digest(); // The negated filter should overwrite the positive one expect(appState.filters.length).to.be(1); expect(appState.filters[0]).to.eql(negatedFilter); }); - it('should allow overwriting a negated filter by a positive one', () => { - $rootScope.$digest(); - + it('should allow overwriting a negated filter by a positive one', async function () { // Add negate: true version of the same filter const negatedFilter = _.cloneDeep(filters[0]); negatedFilter.meta.negate = true; - queryFilter.addFilters(negatedFilter); + await queryFilter.addFilters(negatedFilter); $rootScope.$digest(); + // The negated filter should overwrite the positive one expect(appState.filters.length).to.be(1); expect(appState.filters[0]).to.eql(negatedFilter); @@ -143,19 +137,20 @@ describe('add filters', function () { const filter = _.cloneDeep(filters[0]); filter.meta.negate = false; - queryFilter.addFilters(filter); + await queryFilter.addFilters(filter); $rootScope.$digest(); expect(appState.filters.length).to.be(1); expect(appState.filters[0]).to.eql(filter); }); - it('should fire the update and fetch events', function () { + it('should fire the update and fetch events', async function () { const emitSpy = sinon.spy(queryFilter, 'emit'); // set up the watchers, add new filters, and crank the digest loop $rootScope.$digest(); - queryFilter.addFilters(filters); + await queryFilter.addFilters(filters); $rootScope.$digest(); + await timeout(0); // updates should trigger state saves expect(appState.save.callCount).to.be(1); @@ -170,36 +165,36 @@ describe('add filters', function () { }); describe('filter reconciliation', function () { - it('should de-dupe appState filters being added', function () { + it('should de-dupe appState filters being added', async function () { const newFilter = _.cloneDeep(filters[1]); appState.filters = filters; $rootScope.$digest(); expect(appState.filters.length).to.be(3); - queryFilter.addFilters(newFilter); + await queryFilter.addFilters(newFilter); $rootScope.$digest(); expect(appState.filters.length).to.be(3); }); - it('should de-dupe globalState filters being added', function () { + it('should de-dupe globalState filters being added', async function () { const newFilter = _.cloneDeep(filters[1]); globalState.filters = filters; $rootScope.$digest(); expect(globalState.filters.length).to.be(3); - queryFilter.addFilters(newFilter, true); + await queryFilter.addFilters(newFilter, true); $rootScope.$digest(); expect(globalState.filters.length).to.be(3); }); - it('should mutate global filters on appState filter changes', function () { + it('should mutate global filters on appState filter changes', async function () { const idx = 1; globalState.filters = filters; $rootScope.$digest(); const appFilter = _.cloneDeep(filters[idx]); appFilter.meta.negate = true; - queryFilter.addFilters(appFilter); + await queryFilter.addFilters(appFilter); $rootScope.$digest(); const res = queryFilter.getFilters(); diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js index 99985016fb026..df8cf9c863eb1 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_range.js @@ -39,7 +39,7 @@ describe('Filter Bar Directive', function () { it('should return the key and value for matching filters with gt/lt', function (done) { const filter = { meta: { index: 'logstash-*' }, range: { bytes: { lt: 2048, gt: 1024 } } }; - mapRange(filter).then(function (result) { + mapRangeFn(filter).then(function (result) { expect(result).to.have.property('key', 'bytes'); expect(result).to.have.property('value', '1,024 to 2,048'); done(); diff --git a/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js b/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js index 90b2f06356fa6..e48de790e56f7 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js +++ b/src/legacy/ui/public/filter_bar/lib/map_and_flatten_filters.js @@ -20,10 +20,10 @@ import _ from 'lodash'; import { mapFilter } from './map_filter'; -export async function mapAndFlattenFilters(indexPatterns, filters) { +export function mapAndFlattenFilters(indexPatterns, filters) { const flattened = _(filters) .flatten() .compact() - .value(); - return await Promise.all(flattened.map(item => mapFilter(indexPatterns, item))); + .map(item => mapFilter(indexPatterns, item)).value(); + return Promise.all(flattened); } diff --git a/src/legacy/ui/public/filter_bar/query_filter.js b/src/legacy/ui/public/filter_bar/query_filter.js index d651bf7df9dbd..736e52d0b1417 100644 --- a/src/legacy/ui/public/filter_bar/query_filter.js +++ b/src/legacy/ui/public/filter_bar/query_filter.js @@ -83,14 +83,14 @@ export function FilterBarQueryFilterProvider(Private, Promise, indexPatterns, $r filters = [filters]; } - return Promise.resolve(mapAndFlattenFilters(indexPatterns, filters) + return Promise.resolve(mapAndFlattenFilters(indexPatterns, filters)) .then(function (filters) { if (!filterState.filters) { filterState.filters = []; } filterState.filters = filterState.filters.concat(filters); - })); + }); }; /** @@ -218,7 +218,7 @@ export function FilterBarQueryFilterProvider(Private, Promise, indexPatterns, $r }; queryFilter.setFilters = filters => { - return mapAndFlattenFilters(indexPatterns, filters) + return Promise.resolve(mapAndFlattenFilters(indexPatterns, filters)) .then(mappedFilters => { const appState = getAppState(); const [globalFilters, appFilters] = _.partition(mappedFilters, filter => { From b546cf3370475331d3194747999b1d66eebad38d Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Sun, 5 May 2019 12:34:34 +0300 Subject: [PATCH 12/13] Code review changes --- .../filter_bar/__tests__/_add_filters.js | 13 ++++---- .../ui/public/filter_bar/lib/map_filter.js | 31 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js b/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js index ab0c1de161801..0af048d5227eb 100644 --- a/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js +++ b/src/legacy/ui/public/filter_bar/__tests__/_add_filters.js @@ -33,10 +33,6 @@ describe('add filters', function () { let appState; let globalState; - function timeout(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - beforeEach(ngMock.module( 'kibana', 'kibana/courier', @@ -146,21 +142,26 @@ describe('add filters', function () { it('should fire the update and fetch events', async function () { const emitSpy = sinon.spy(queryFilter, 'emit'); + const awaitFetch = new Promise(resolve => { + queryFilter.on('fetch', () => { + resolve(); + }); + }); + // set up the watchers, add new filters, and crank the digest loop $rootScope.$digest(); await queryFilter.addFilters(filters); $rootScope.$digest(); - await timeout(0); // updates should trigger state saves expect(appState.save.callCount).to.be(1); expect(globalState.save.callCount).to.be(1); // this time, events should be emitted + await awaitFetch; expect(emitSpy.callCount).to.be(2); expect(emitSpy.firstCall.args[0]).to.be('update'); expect(emitSpy.secondCall.args[0]).to.be('fetch'); - }); }); diff --git a/src/legacy/ui/public/filter_bar/lib/map_filter.js b/src/legacy/ui/public/filter_bar/lib/map_filter.js index 82197f770c2eb..ab468e9243273 100644 --- a/src/legacy/ui/public/filter_bar/lib/map_filter.js +++ b/src/legacy/ui/public/filter_bar/lib/map_filter.js @@ -70,21 +70,18 @@ export async function mapFilter(indexPatterns, filter) { return generateMappingChain(map, memo); }, noop); - /** - * Map the filter into an object with the key and value exposed so it's - * easier to work with in the template - * @param {object} filter The filter the map - * @returns {Promise} - */ - return mapFn(filter).then(function (result) { - filter.meta = filter.meta || {}; - filter.meta.type = result.type; - filter.meta.key = result.key; - filter.meta.value = result.value; - filter.meta.params = result.params; - filter.meta.disabled = !!(filter.meta.disabled); - filter.meta.negate = !!(filter.meta.negate); - filter.meta.alias = filter.meta.alias || null; - return filter; - }); + const mapped = await mapFn(filter); + + // Map the filter into an object with the key and value exposed so it's + // easier to work with in the template + filter.meta = filter.meta || {}; + filter.meta.type = mapped.type; + filter.meta.key = mapped.key; + filter.meta.value = mapped.value; + filter.meta.params = mapped.params; + filter.meta.disabled = !!(filter.meta.disabled); + filter.meta.negate = !!(filter.meta.negate); + filter.meta.alias = filter.meta.alias || null; + + return filter; } From 6cf6b76b6aaa22a7612e744bd43f60fbf86774da Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Tue, 7 May 2019 10:09:22 +0300 Subject: [PATCH 13/13] Deleted commented line --- .../ui/public/filter_bar/lib/__tests__/map_query_string.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js b/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js index d7804e908993b..dfcfd56fa9f7d 100644 --- a/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js +++ b/src/legacy/ui/public/filter_bar/lib/__tests__/map_query_string.js @@ -18,7 +18,6 @@ */ import expect from '@kbn/expect'; -// import ngMock from 'ng_mock'; import { mapQueryString } from '../map_query_string'; describe('Filter Bar Directive', function () {