diff --git a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/_utils.js b/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/_utils.js deleted file mode 100644 index 63f8ced97e9dc43..000000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/_utils.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 createStateStub(overrides) { - return _.merge( - { - queryParameters: { - defaultStepSize: 3, - indexPatternId: 'INDEX_PATTERN_ID', - predecessorCount: 10, - successorCount: 10, - }, - }, - overrides - ); -} diff --git a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_add_filter.js b/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_add_filter.js deleted file mode 100644 index 87eb283639c789c..000000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_add_filter.js +++ /dev/null @@ -1,65 +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 { createStateStub } from './_utils'; -import { getQueryParameterActions } from '../../np_ready/angular/context/query_parameters/actions'; -import { pluginInstance } from 'plugins/kibana/discover/legacy'; -import { npStart } from 'ui/new_platform'; - -describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(() => pluginInstance.initializeServices()); - beforeEach(ngMock.module('app/discover')); - - describe('action addFilter', function() { - let addFilter; - - beforeEach( - ngMock.inject(function createPrivateStubs() { - addFilter = getQueryParameterActions().addFilter; - }) - ); - - it('should pass the given arguments to the filterManager', function() { - const state = createStateStub(); - const filterManagerAddStub = npStart.plugins.data.query.filterManager.addFilters; - - addFilter(state)('FIELD_NAME', 'FIELD_VALUE', 'FILTER_OPERATION'); - - //get the generated filter - const generatedFilter = filterManagerAddStub.firstCall.args[0][0]; - const queryKeys = Object.keys(generatedFilter.query.match_phrase); - expect(filterManagerAddStub.calledOnce).to.be(true); - expect(queryKeys[0]).to.eql('FIELD_NAME'); - expect(generatedFilter.query.match_phrase[queryKeys[0]]).to.eql('FIELD_VALUE'); - }); - - it('should pass the index pattern id to the filterManager', function() { - const state = createStateStub(); - const filterManagerAddStub = npStart.plugins.data.query.filterManager.addFilters; - - addFilter(state)('FIELD_NAME', 'FIELD_VALUE', 'FILTER_OPERATION'); - - const generatedFilter = filterManagerAddStub.firstCall.args[0][0]; - expect(generatedFilter.meta.index).to.eql('INDEX_PATTERN_ID'); - }); - }); -}); diff --git a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_predecessor_count.js b/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_predecessor_count.js deleted file mode 100644 index 9ba425bb0e489ee..000000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_predecessor_count.js +++ /dev/null @@ -1,64 +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 { pluginInstance } from 'plugins/kibana/discover/legacy'; -import { createStateStub } from './_utils'; -import { getQueryParameterActions } from '../../np_ready/angular/context/query_parameters/actions'; - -describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(() => pluginInstance.initializeServices()); - beforeEach(ngMock.module('app/discover')); - - describe('action setPredecessorCount', function() { - let setPredecessorCount; - - beforeEach( - ngMock.inject(function createPrivateStubs() { - setPredecessorCount = getQueryParameterActions().setPredecessorCount; - }) - ); - - it('should set the predecessorCount to the given value', function() { - const state = createStateStub(); - - setPredecessorCount(state)(20); - - expect(state.queryParameters.predecessorCount).to.equal(20); - }); - - it('should limit the predecessorCount to 0 as a lower bound', function() { - const state = createStateStub(); - - setPredecessorCount(state)(-1); - - expect(state.queryParameters.predecessorCount).to.equal(0); - }); - - it('should limit the predecessorCount to 10000 as an upper bound', function() { - const state = createStateStub(); - - setPredecessorCount(state)(20000); - - expect(state.queryParameters.predecessorCount).to.equal(10000); - }); - }); -}); diff --git a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_query_parameters.js b/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_query_parameters.js deleted file mode 100644 index 39dde2d8bb7cf0a..000000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_query_parameters.js +++ /dev/null @@ -1,82 +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 { pluginInstance } from 'plugins/kibana/discover/legacy'; - -import { createStateStub } from './_utils'; -import { getQueryParameterActions } from '../../np_ready/angular/context/query_parameters/actions'; - -describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(() => pluginInstance.initializeServices()); - beforeEach(ngMock.module('app/discover')); - - describe('action setQueryParameters', function() { - let setQueryParameters; - - beforeEach( - ngMock.inject(function createPrivateStubs() { - setQueryParameters = getQueryParameterActions().setQueryParameters; - }) - ); - - it('should update the queryParameters with valid properties from the given object', function() { - const state = createStateStub({ - queryParameters: { - additionalParameter: 'ADDITIONAL_PARAMETER', - }, - }); - - setQueryParameters(state)({ - anchorId: 'ANCHOR_ID', - columns: ['column'], - defaultStepSize: 3, - filters: ['filter'], - indexPatternId: 'INDEX_PATTERN', - predecessorCount: 100, - successorCount: 100, - sort: ['field'], - }); - - expect(state.queryParameters).to.eql({ - additionalParameter: 'ADDITIONAL_PARAMETER', - anchorId: 'ANCHOR_ID', - columns: ['column'], - defaultStepSize: 3, - filters: ['filter'], - indexPatternId: 'INDEX_PATTERN', - predecessorCount: 100, - successorCount: 100, - sort: ['field'], - }); - }); - - it('should ignore invalid properties', function() { - const state = createStateStub(); - - setQueryParameters(state)({ - additionalParameter: 'ADDITIONAL_PARAMETER', - }); - - expect(state.queryParameters).to.eql(createStateStub().queryParameters); - }); - }); -}); diff --git a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_successor_count.js b/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_successor_count.js deleted file mode 100644 index c05f5b4aff3bc73..000000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/__tests__/query_parameters/action_set_successor_count.js +++ /dev/null @@ -1,65 +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 { pluginInstance } from 'plugins/kibana/discover/legacy'; - -import { createStateStub } from './_utils'; -import { getQueryParameterActions } from '../../np_ready/angular/context/query_parameters/actions'; - -describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(() => pluginInstance.initializeServices()); - beforeEach(ngMock.module('app/discover')); - - describe('action setSuccessorCount', function() { - let setSuccessorCount; - - beforeEach( - ngMock.inject(function createPrivateStubs() { - setSuccessorCount = getQueryParameterActions().setSuccessorCount; - }) - ); - - it('should set the successorCount to the given value', function() { - const state = createStateStub(); - - setSuccessorCount(state)(20); - - expect(state.queryParameters.successorCount).to.equal(20); - }); - - it('should limit the successorCount to 0 as a lower bound', function() { - const state = createStateStub(); - - setSuccessorCount(state)(-1); - - expect(state.queryParameters.successorCount).to.equal(0); - }); - - it('should limit the successorCount to 10000 as an upper bound', function() { - const state = createStateStub(); - - setSuccessorCount(state)(20000); - - expect(state.queryParameters.successorCount).to.equal(10000); - }); - }); -}); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/_stubs.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/_stubs.js similarity index 97% rename from src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/_stubs.js rename to src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/_stubs.js index 53be4e5bd0f2d0f..f6ed570be2c3734 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/_stubs.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/_stubs.js @@ -19,7 +19,7 @@ import sinon from 'sinon'; import moment from 'moment'; -import { SearchSource } from '../../../../../kibana_services'; +import { SearchSource } from '../../../../../../../../../plugins/data/public'; export function createIndexPatternsStub() { return { diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/anchor.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/anchor.test.js similarity index 73% rename from src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/anchor.js rename to src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/anchor.test.js index 63834fb750e214d..0bc2cbacc1eeec4 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/anchor.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/anchor.test.js @@ -17,28 +17,19 @@ * under the License. */ -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import { pluginInstance } from 'plugins/kibana/discover/legacy'; - import { createIndexPatternsStub, createSearchSourceStub } from './_stubs'; -import { fetchAnchorProvider } from '../anchor'; +import { fetchAnchorProvider } from './anchor'; describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(ngMock.module('app/discover')); - describe('function fetchAnchor', function() { let fetchAnchor; let searchSourceStub; - beforeEach( - ngMock.inject(function createPrivateStubs() { - searchSourceStub = createSearchSourceStub([{ _id: 'hit1' }]); - fetchAnchor = fetchAnchorProvider(createIndexPatternsStub(), searchSourceStub); - }) - ); + beforeEach(() => { + searchSourceStub = createSearchSourceStub([{ _id: 'hit1' }]); + fetchAnchor = fetchAnchorProvider(createIndexPatternsStub(), searchSourceStub); + }); afterEach(() => { searchSourceStub._restore(); @@ -49,7 +40,7 @@ describe('context app', function() { { '@timestamp': 'desc' }, { _doc: 'desc' }, ]).then(() => { - expect(searchSourceStub.fetch.calledOnce).to.be(true); + expect(searchSourceStub.fetch.calledOnce).toBe(true); }); }); @@ -59,8 +50,8 @@ describe('context app', function() { { _doc: 'desc' }, ]).then(() => { const setParentSpy = searchSourceStub.setParent; - expect(setParentSpy.calledOnce).to.be(true); - expect(setParentSpy.firstCall.args[0]).to.be(undefined); + expect(setParentSpy.calledOnce).toBe(true); + expect(setParentSpy.firstCall.args[0]).toBe(undefined); }); }); @@ -70,7 +61,7 @@ describe('context app', function() { { _doc: 'desc' }, ]).then(() => { const setFieldSpy = searchSourceStub.setField; - expect(setFieldSpy.firstCall.args[1].id).to.eql('INDEX_PATTERN_ID'); + expect(setFieldSpy.firstCall.args[1].id).toEqual('INDEX_PATTERN_ID'); }); }); @@ -80,8 +71,8 @@ describe('context app', function() { { _doc: 'desc' }, ]).then(() => { const setVersionSpy = searchSourceStub.setField.withArgs('version'); - expect(setVersionSpy.calledOnce).to.be(true); - expect(setVersionSpy.firstCall.args[1]).to.eql(true); + expect(setVersionSpy.calledOnce).toBe(true); + expect(setVersionSpy.firstCall.args[1]).toEqual(true); }); }); @@ -91,8 +82,8 @@ describe('context app', function() { { _doc: 'desc' }, ]).then(() => { const setSizeSpy = searchSourceStub.setField.withArgs('size'); - expect(setSizeSpy.calledOnce).to.be(true); - expect(setSizeSpy.firstCall.args[1]).to.eql(1); + expect(setSizeSpy.calledOnce).toBe(true); + expect(setSizeSpy.firstCall.args[1]).toEqual(1); }); }); @@ -102,8 +93,8 @@ describe('context app', function() { { _doc: 'desc' }, ]).then(() => { const setQuerySpy = searchSourceStub.setField.withArgs('query'); - expect(setQuerySpy.calledOnce).to.be(true); - expect(setQuerySpy.firstCall.args[1]).to.eql({ + expect(setQuerySpy.calledOnce).toBe(true); + expect(setQuerySpy.firstCall.args[1]).toEqual({ query: { constant_score: { filter: { @@ -124,8 +115,8 @@ describe('context app', function() { { _doc: 'desc' }, ]).then(() => { const setSortSpy = searchSourceStub.setField.withArgs('sort'); - expect(setSortSpy.calledOnce).to.be(true); - expect(setSortSpy.firstCall.args[1]).to.eql([{ '@timestamp': 'desc' }, { _doc: 'desc' }]); + expect(setSortSpy.calledOnce).toBe(true); + expect(setSortSpy.firstCall.args[1]).toEqual([{ '@timestamp': 'desc' }, { _doc: 'desc' }]); }); }); @@ -140,7 +131,7 @@ describe('context app', function() { expect().fail('expected the promise to be rejected'); }, error => { - expect(error).to.be.an(Error); + expect(error).toBeInstanceOf(Error); } ); }); @@ -152,8 +143,8 @@ describe('context app', function() { { '@timestamp': 'desc' }, { _doc: 'desc' }, ]).then(anchorDocument => { - expect(anchorDocument).to.have.property('property1', 'value1'); - expect(anchorDocument).to.have.property('$$_isAnchor', true); + expect(anchorDocument).toHaveProperty('property1', 'value1'); + expect(anchorDocument).toHaveProperty('$$_isAnchor', true); }); }); }); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/predecessors.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.predecessors.test.js similarity index 74% rename from src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/predecessors.js rename to src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.predecessors.test.js index 02d998e8f4529ff..d6e91e57b22a8cb 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/predecessors.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.predecessors.test.js @@ -17,15 +17,10 @@ * under the License. */ -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; import moment from 'moment'; import * as _ from 'lodash'; -import { pluginInstance } from 'plugins/kibana/discover/legacy'; - import { createIndexPatternsStub, createContextSearchSourceStub } from './_stubs'; - -import { fetchContextProvider } from '../context'; +import { fetchContextProvider } from './context'; const MS_PER_DAY = 24 * 60 * 60 * 1000; const ANCHOR_TIMESTAMP = new Date(MS_PER_DAY).toJSON(); @@ -34,46 +29,41 @@ const ANCHOR_TIMESTAMP_1000 = new Date(MS_PER_DAY * 1000).toJSON(); const ANCHOR_TIMESTAMP_3000 = new Date(MS_PER_DAY * 3000).toJSON(); describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(ngMock.module('app/discover')); - describe('function fetchPredecessors', function() { let fetchPredecessors; let searchSourceStub; - beforeEach( - ngMock.inject(function createPrivateStubs() { - searchSourceStub = createContextSearchSourceStub([], '@timestamp', MS_PER_DAY * 8); - fetchPredecessors = ( + beforeEach(() => { + searchSourceStub = createContextSearchSourceStub([], '@timestamp', MS_PER_DAY * 8); + fetchPredecessors = ( + indexPatternId, + timeField, + sortDir, + timeValIso, + timeValNr, + tieBreakerField, + tieBreakerValue, + size + ) => { + const anchor = { + _source: { + [timeField]: timeValIso, + }, + sort: [timeValNr, tieBreakerValue], + }; + + return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs( + 'predecessors', indexPatternId, + anchor, timeField, - sortDir, - timeValIso, - timeValNr, tieBreakerField, - tieBreakerValue, - size - ) => { - const anchor = { - _source: { - [timeField]: timeValIso, - }, - sort: [timeValNr, tieBreakerValue], - }; - - return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs( - 'predecessors', - indexPatternId, - anchor, - timeField, - tieBreakerField, - sortDir, - size, - [] - ); - }; - }) - ); + sortDir, + size, + [] + ); + }; + }); afterEach(() => { searchSourceStub._restore(); @@ -99,8 +89,8 @@ describe('context app', function() { 3, [] ).then(hits => { - expect(searchSourceStub.fetch.calledOnce).to.be(true); - expect(hits).to.eql(searchSourceStub._stubHits.slice(0, 3)); + expect(searchSourceStub.fetch.calledOnce).toBe(true); + expect(hits).toEqual(searchSourceStub._stubHits.slice(0, 3)); }); }); @@ -132,14 +122,14 @@ describe('context app', function() { expect( intervals.every(({ gte, lte }) => (gte && lte ? moment(gte).isBefore(lte) : true)) - ).to.be(true); + ).toBe(true); // should have started at the given time - expect(intervals[0].gte).to.eql(moment(MS_PER_DAY * 3000).toISOString()); + expect(intervals[0].gte).toEqual(moment(MS_PER_DAY * 3000).toISOString()); // should have ended with a half-open interval - expect(_.last(intervals)).to.only.have.keys('gte', 'format'); - expect(intervals.length).to.be.greaterThan(1); + expect(Object.keys(_.last(intervals))).toEqual(['format', 'gte']); + expect(intervals.length).toBeGreaterThan(1); - expect(hits).to.eql(searchSourceStub._stubHits.slice(0, 3)); + expect(hits).toEqual(searchSourceStub._stubHits.slice(0, 3)); }); }); @@ -169,11 +159,11 @@ describe('context app', function() { ); // should have started at the given time - expect(intervals[0].gte).to.eql(moment(MS_PER_DAY * 1000).toISOString()); + expect(intervals[0].gte).toEqual(moment(MS_PER_DAY * 1000).toISOString()); // should have stopped before reaching MS_PER_DAY * 1700 - expect(moment(_.last(intervals).lte).valueOf()).to.be.lessThan(MS_PER_DAY * 1700); - expect(intervals.length).to.be.greaterThan(1); - expect(hits).to.eql(searchSourceStub._stubHits.slice(-3)); + expect(moment(_.last(intervals).lte).valueOf()).toBeLessThan(MS_PER_DAY * 1700); + expect(intervals.length).toBeGreaterThan(1); + expect(hits).toEqual(searchSourceStub._stubHits.slice(-3)); }); }); @@ -189,7 +179,7 @@ describe('context app', function() { 3, [] ).then(hits => { - expect(hits).to.eql([]); + expect(hits).toEqual([]); }); }); @@ -206,8 +196,8 @@ describe('context app', function() { [] ).then(() => { const setParentSpy = searchSourceStub.setParent; - expect(setParentSpy.alwaysCalledWith(undefined)).to.be(true); - expect(setParentSpy.called).to.be(true); + expect(setParentSpy.alwaysCalledWith(undefined)).toBe(true); + expect(setParentSpy.called).toBe(true); }); }); @@ -225,7 +215,7 @@ describe('context app', function() { ).then(() => { expect( searchSourceStub.setField.calledWith('sort', [{ '@timestamp': 'asc' }, { _doc: 'asc' }]) - ).to.be(true); + ).toBe(true); }); }); }); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/successors.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.successors.test.js similarity index 74% rename from src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/successors.js rename to src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.successors.test.js index d4c00930c938390..cc2b6d31cb43b55 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/__tests__/successors.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.successors.test.js @@ -17,15 +17,12 @@ * under the License. */ -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; import moment from 'moment'; import * as _ from 'lodash'; -import { pluginInstance } from 'plugins/kibana/discover/legacy'; import { createIndexPatternsStub, createContextSearchSourceStub } from './_stubs'; -import { fetchContextProvider } from '../context'; +import { fetchContextProvider } from './context'; const MS_PER_DAY = 24 * 60 * 60 * 1000; const ANCHOR_TIMESTAMP = new Date(MS_PER_DAY).toJSON(); @@ -33,47 +30,42 @@ const ANCHOR_TIMESTAMP_3 = new Date(MS_PER_DAY * 3).toJSON(); const ANCHOR_TIMESTAMP_3000 = new Date(MS_PER_DAY * 3000).toJSON(); describe('context app', function() { - beforeEach(() => pluginInstance.initializeInnerAngular()); - beforeEach(ngMock.module('app/discover')); - describe('function fetchSuccessors', function() { let fetchSuccessors; let searchSourceStub; - beforeEach( - ngMock.inject(function createPrivateStubs() { - searchSourceStub = createContextSearchSourceStub([], '@timestamp'); + beforeEach(() => { + searchSourceStub = createContextSearchSourceStub([], '@timestamp'); + + fetchSuccessors = ( + indexPatternId, + timeField, + sortDir, + timeValIso, + timeValNr, + tieBreakerField, + tieBreakerValue, + size + ) => { + const anchor = { + _source: { + [timeField]: timeValIso, + }, + sort: [timeValNr, tieBreakerValue], + }; - fetchSuccessors = ( + return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs( + 'successors', indexPatternId, + anchor, timeField, - sortDir, - timeValIso, - timeValNr, tieBreakerField, - tieBreakerValue, - size - ) => { - const anchor = { - _source: { - [timeField]: timeValIso, - }, - sort: [timeValNr, tieBreakerValue], - }; - - return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs( - 'successors', - indexPatternId, - anchor, - timeField, - tieBreakerField, - sortDir, - size, - [] - ); - }; - }) - ); + sortDir, + size, + [] + ); + }; + }); afterEach(() => { searchSourceStub._restore(); @@ -99,8 +91,8 @@ describe('context app', function() { 3, [] ).then(hits => { - expect(searchSourceStub.fetch.calledOnce).to.be(true); - expect(hits).to.eql(searchSourceStub._stubHits.slice(-3)); + expect(searchSourceStub.fetch.calledOnce).toBe(true); + expect(hits).toEqual(searchSourceStub._stubHits.slice(-3)); }); }); @@ -132,14 +124,14 @@ describe('context app', function() { expect( intervals.every(({ gte, lte }) => (gte && lte ? moment(gte).isBefore(lte) : true)) - ).to.be(true); + ).toBe(true); // should have started at the given time - expect(intervals[0].lte).to.eql(moment(MS_PER_DAY * 3000).toISOString()); + expect(intervals[0].lte).toEqual(moment(MS_PER_DAY * 3000).toISOString()); // should have ended with a half-open interval - expect(_.last(intervals)).to.only.have.keys('lte', 'format'); - expect(intervals.length).to.be.greaterThan(1); + expect(Object.keys(_.last(intervals))).toEqual(['format', 'lte']); + expect(intervals.length).toBeGreaterThan(1); - expect(hits).to.eql(searchSourceStub._stubHits.slice(-3)); + expect(hits).toEqual(searchSourceStub._stubHits.slice(-3)); }); }); @@ -171,12 +163,12 @@ describe('context app', function() { ); // should have started at the given time - expect(intervals[0].lte).to.eql(moment(MS_PER_DAY * 3000).toISOString()); + expect(intervals[0].lte).toEqual(moment(MS_PER_DAY * 3000).toISOString()); // should have stopped before reaching MS_PER_DAY * 2200 - expect(moment(_.last(intervals).gte).valueOf()).to.be.greaterThan(MS_PER_DAY * 2200); - expect(intervals.length).to.be.greaterThan(1); + expect(moment(_.last(intervals).gte).valueOf()).toBeGreaterThan(MS_PER_DAY * 2200); + expect(intervals.length).toBeGreaterThan(1); - expect(hits).to.eql(searchSourceStub._stubHits.slice(0, 4)); + expect(hits).toEqual(searchSourceStub._stubHits.slice(0, 4)); }); }); @@ -192,7 +184,7 @@ describe('context app', function() { 3, [] ).then(hits => { - expect(hits).to.eql([]); + expect(hits).toEqual([]); }); }); @@ -209,8 +201,8 @@ describe('context app', function() { [] ).then(() => { const setParentSpy = searchSourceStub.setParent; - expect(setParentSpy.alwaysCalledWith(undefined)).to.be(true); - expect(setParentSpy.called).to.be(true); + expect(setParentSpy.alwaysCalledWith(undefined)).toBe(true); + expect(setParentSpy.called).toBe(true); }); }); @@ -228,7 +220,7 @@ describe('context app', function() { ).then(() => { expect( searchSourceStub.setField.calledWith('sort', [{ '@timestamp': 'desc' }, { _doc: 'desc' }]) - ).to.be(true); + ).toBe(true); }); }); }); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.ts index b91ef5a6b79fbbf..507f927c608e1b4 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/context.ts @@ -17,14 +17,18 @@ * under the License. */ -import { IndexPattern, SearchSource } from '../../../../kibana_services'; import { reverseSortDir, SortDirection } from './utils/sorting'; import { extractNanos, convertIsoToMillis } from './utils/date_conversion'; import { fetchHitsInInterval } from './utils/fetch_hits_in_interval'; import { generateIntervals } from './utils/generate_intervals'; import { getEsQuerySearchAfter } from './utils/get_es_query_search_after'; import { getEsQuerySort } from './utils/get_es_query_sort'; -import { Filter, IndexPatternsContract } from '../../../../../../../../../plugins/data/public'; +import { + Filter, + IndexPatternsContract, + IndexPattern, + SearchSource, +} from '../../../../../../../../../plugins/data/public'; export type SurrDocType = 'successors' | 'predecessors'; export interface EsHitRecord { diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/fetch_hits_in_interval.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/fetch_hits_in_interval.ts index e7df44e6fe61c7d..8eed5d33ab004b7 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/fetch_hits_in_interval.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/fetch_hits_in_interval.ts @@ -16,7 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -import { EsQuerySortValue, SortDirection, ISearchSource } from '../../../../../kibana_services'; +import { + ISearchSource, + EsQuerySortValue, + SortDirection, +} from '../../../../../../../../../../plugins/data/public'; import { convertTimeValueToIso } from './date_conversion'; import { EsHitRecordList } from '../context'; import { IntervalValue } from './generate_intervals'; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/generate_intervals.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/generate_intervals.ts index 373dc37e56f6f3a..b14180d32b4f24a 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/generate_intervals.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/api/utils/generate_intervals.ts @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import { SortDirection } from '../../../../../kibana_services'; +import { SortDirection } from '../../../../../../../../../../plugins/data/public'; export type IntervalValue = number | null; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query/actions.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query/actions.js index 1cebb88cbda5a00..674f40d0186e544 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query/actions.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query/actions.js @@ -29,9 +29,13 @@ import { FAILURE_REASONS, LOADING_STATUS } from './constants'; import { MarkdownSimple } from '../../../../../../../kibana_react/public'; export function QueryActionsProvider(Promise) { - const fetchAnchor = fetchAnchorProvider(getServices().indexPatterns, new SearchSource()); - const { fetchSurroundingDocs } = fetchContextProvider(getServices().indexPatterns); - const { setPredecessorCount, setQueryParameters, setSuccessorCount } = getQueryParameterActions(); + const { filterManager, indexPatterns } = getServices(); + const fetchAnchor = fetchAnchorProvider(indexPatterns, new SearchSource()); + const { fetchSurroundingDocs } = fetchContextProvider(indexPatterns); + const { setPredecessorCount, setQueryParameters, setSuccessorCount } = getQueryParameterActions( + filterManager, + indexPatterns + ); const setFailedStatus = state => (subject, details = {}) => (state.loadingStatus[subject] = { diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.js index 5be1179a9ae09c5..5c1700e7763610a 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.js @@ -18,14 +18,11 @@ */ import _ from 'lodash'; -import { getServices } from '../../../../kibana_services'; import { esFilters } from '../../../../../../../../../plugins/data/public'; import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE, QUERY_PARAMETER_KEYS } from './constants'; -export function getQueryParameterActions() { - const filterManager = getServices().filterManager; - +export function getQueryParameterActions(filterManager, indexPatterns) { const setPredecessorCount = state => predecessorCount => (state.queryParameters.predecessorCount = clamp( MIN_CONTEXT_SIZE, @@ -57,8 +54,10 @@ export function getQueryParameterActions() { indexPatternId ); filterManager.addFilters(newFilters); - const indexPattern = await getServices().indexPatterns.get(indexPatternId); - indexPattern.popularizeField(field.name, 1); + if (indexPatterns) { + const indexPattern = await indexPatterns.get(indexPatternId); + indexPattern.popularizeField(field.name, 1); + } }; return { diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.test.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.test.ts new file mode 100644 index 000000000000000..35fbd33fb4bc973 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context/query_parameters/actions.test.ts @@ -0,0 +1,157 @@ +/* + * 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. + */ + +// @ts-ignore +import { getQueryParameterActions } from './actions'; +import { FilterManager } from '../../../../../../../../../plugins/data/public'; +import { coreMock } from '../../../../../../../../../core/public/mocks'; +const setupMock = coreMock.createSetup(); + +let state: { + queryParameters: { + defaultStepSize: number; + indexPatternId: string; + predecessorCount: number; + successorCount: number; + }; +}; +let filterManager: FilterManager; +let filterManagerSpy: jest.SpyInstance; + +beforeEach(() => { + filterManager = new FilterManager(setupMock.uiSettings); + filterManagerSpy = jest.spyOn(filterManager, 'addFilters'); + + state = { + queryParameters: { + defaultStepSize: 3, + indexPatternId: 'INDEX_PATTERN_ID', + predecessorCount: 10, + successorCount: 10, + }, + }; +}); + +describe('context query_parameter actions', function() { + describe('action addFilter', () => { + it('should pass the given arguments to the filterManager', () => { + const { addFilter } = getQueryParameterActions(filterManager); + + addFilter(state)('FIELD_NAME', 'FIELD_VALUE', 'FILTER_OPERATION'); + + // get the generated filter + const generatedFilter = filterManagerSpy.mock.calls[0][0][0]; + const queryKeys = Object.keys(generatedFilter.query.match_phrase); + expect(filterManagerSpy.mock.calls.length).toBe(1); + expect(queryKeys[0]).toBe('FIELD_NAME'); + expect(generatedFilter.query.match_phrase[queryKeys[0]]).toBe('FIELD_VALUE'); + }); + + it('should pass the index pattern id to the filterManager', () => { + const { addFilter } = getQueryParameterActions(filterManager); + addFilter(state)('FIELD_NAME', 'FIELD_VALUE', 'FILTER_OPERATION'); + const generatedFilter = filterManagerSpy.mock.calls[0][0][0]; + expect(generatedFilter.meta.index).toBe('INDEX_PATTERN_ID'); + }); + }); + describe('action setPredecessorCount', () => { + it('should set the predecessorCount to the given value', () => { + const { setPredecessorCount } = getQueryParameterActions(filterManager); + setPredecessorCount(state)(20); + expect(state.queryParameters.predecessorCount).toBe(20); + }); + + it('should limit the predecessorCount to 0 as a lower bound', () => { + const { setPredecessorCount } = getQueryParameterActions(filterManager); + setPredecessorCount(state)(-1); + expect(state.queryParameters.predecessorCount).toBe(0); + }); + + it('should limit the predecessorCount to 10000 as an upper bound', () => { + const { setPredecessorCount } = getQueryParameterActions(filterManager); + setPredecessorCount(state)(20000); + expect(state.queryParameters.predecessorCount).toBe(10000); + }); + }); + describe('action setSuccessorCount', () => { + it('should set the successorCount to the given value', function() { + const { setSuccessorCount } = getQueryParameterActions(filterManager); + setSuccessorCount(state)(20); + + expect(state.queryParameters.successorCount).toBe(20); + }); + + it('should limit the successorCount to 0 as a lower bound', () => { + const { setSuccessorCount } = getQueryParameterActions(filterManager); + setSuccessorCount(state)(-1); + expect(state.queryParameters.successorCount).toBe(0); + }); + + it('should limit the successorCount to 10000 as an upper bound', () => { + const { setSuccessorCount } = getQueryParameterActions(filterManager); + setSuccessorCount(state)(20000); + expect(state.queryParameters.successorCount).toBe(10000); + }); + }); + describe('action setQueryParameters', function() { + const { setQueryParameters } = getQueryParameterActions(filterManager); + + it('should update the queryParameters with valid properties from the given object', function() { + const newState = { + ...state, + queryParameters: { + additionalParameter: 'ADDITIONAL_PARAMETER', + }, + }; + + const actualState = setQueryParameters(newState)({ + anchorId: 'ANCHOR_ID', + columns: ['column'], + defaultStepSize: 3, + filters: ['filter'], + indexPatternId: 'INDEX_PATTERN', + predecessorCount: 100, + successorCount: 100, + sort: ['field'], + }); + + expect(actualState).toEqual({ + additionalParameter: 'ADDITIONAL_PARAMETER', + anchorId: 'ANCHOR_ID', + columns: ['column'], + defaultStepSize: 3, + filters: ['filter'], + indexPatternId: 'INDEX_PATTERN', + predecessorCount: 100, + successorCount: 100, + sort: ['field'], + }); + }); + + it('should ignore invalid properties', function() { + const newState = { ...state }; + + setQueryParameters(newState)({ + additionalParameter: 'ADDITIONAL_PARAMETER', + }); + + expect(state.queryParameters).toEqual(newState.queryParameters); + }); + }); +}); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context_app.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context_app.js index b5ba2844e8b06e3..345717cafee9af0 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context_app.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/context_app.js @@ -18,7 +18,7 @@ */ import _ from 'lodash'; -import { getAngularModule } from '../../kibana_services'; +import { getAngularModule, getServices } from '../../kibana_services'; import contextAppTemplate from './context_app.html'; import './context/components/action_bar'; import { getFirstSortableField } from './context/api/utils/sorting'; @@ -58,7 +58,8 @@ module.directive('contextApp', function ContextApp() { }); function ContextAppController($scope, config, Private) { - const queryParameterActions = getQueryParameterActions(); + const { filterManager, indexpatterns } = getServices(); + const queryParameterActions = getQueryParameterActions(filterManager, indexpatterns); const queryActions = Private(QueryActionsProvider); this.state = createInitialState( parseInt(config.get('context:step'), 10),