diff --git a/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js b/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js new file mode 100644 index 0000000000000..6081ceb5020cb --- /dev/null +++ b/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js @@ -0,0 +1,131 @@ +import sinon from 'sinon'; +import { expect } from 'chai'; +import { dashboardContextProvider } from '../dashboard_context'; + +describe('Dashboard Context', () => { + + describe('with query bar', () => { + let Private; + let getAppState; + let getDashboardContext; + beforeEach(() => { + Private = sinon.stub().returns({ + getFilters() { + return []; + } + }); + }); + + it('should return an empty must and must not when there are no filters or queries', () => { + getAppState = sinon.stub().returns({ + query: { + language: 'lucene', + query: null + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [], + must_not: [] + } + }); + }); + + it('should add a valid query to must', () => { + getAppState = sinon.stub().returns({ + query: { + language: 'lucene', + query: '*' + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [ + { + query_string: { + query: '*' + } + } + ], + must_not: [] + } + }); + }); + + }); + + describe('with filter bar', () => { + let Private; + let getAppState; + let getDashboardContext; + beforeEach(() => { + getAppState = sinon.stub().returns({ query: { language: 'something-else' } }); + }); + + afterEach(() => { + getAppState.reset(); + }); + + it('should add a valid filter to must', () => { + Private = sinon.stub().returns({ + getFilters() { + return [ + { meta: { negate: false }, term: { foo: 'bar' } } + ]; + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [{ term: { foo: 'bar' } }], + must_not: [] + } + }); + }); + + it('should add a valid filter to must_not', () => { + Private = sinon.stub().returns({ + getFilters() { + return [ + { meta: { negate: true }, term: { foo: 'bar' } } + ]; + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [], + must_not: [{ term: { foo: 'bar' } }] + } + }); + }); + + it('should not add a disabled filter', () => { + Private = sinon.stub().returns({ + getFilters() { + return [ + { meta: { negate: true, disabled: true }, term: { foo: 'bar' } } + ]; + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [], + must_not: [] + } + }); + }); + + }); + +}); + + diff --git a/src/core_plugins/kibana/public/dashboard/dashboard_context.js b/src/core_plugins/kibana/public/dashboard/dashboard_context.js index 5788905f58ed6..1824ba3f21bd0 100644 --- a/src/core_plugins/kibana/public/dashboard/dashboard_context.js +++ b/src/core_plugins/kibana/public/dashboard/dashboard_context.js @@ -13,7 +13,8 @@ export function dashboardContextProvider(Private, getAppState) { if (queryBarQuery.language === 'lucene') { // Add the query bar filter, its handled differently. - bool.must.push(luceneStringToDsl(queryBarQuery.query)); + const query = luceneStringToDsl(queryBarQuery.query); + if (query) bool.must.push(query); } // Add each of the filter bar filters @@ -26,10 +27,10 @@ export function dashboardContextProvider(Private, getAppState) { if (filter.meta.disabled) return; if (filter.meta.negate) { bool.must_not = bool.must_not || []; - bool.must_not.push(esFilter.query || esFilter); + if (esFilter.query || esFilter) bool.must_not.push(esFilter.query || esFilter); } else { bool.must = bool.must || []; - bool.must.push(esFilter.query || esFilter); + if (esFilter.query || esFilter) bool.must.push(esFilter.query || esFilter); } }); diff --git a/src/ui/public/courier/saved_object/saved_object.js b/src/ui/public/courier/saved_object/saved_object.js index 050f36439c048..45770af5fb4e5 100644 --- a/src/ui/public/courier/saved_object/saved_object.js +++ b/src/ui/public/courier/saved_object/saved_object.js @@ -107,7 +107,9 @@ export function SavedObjectProvider(Promise, Private, Notifier, confirmModalProm this.searchSource.set(_.defaults(state, fnProps)); - this.searchSource.set('query', migrateLegacyQuery(this.searchSource.getOwn('query'))); + if (!_.isUndefined(this.searchSource.getOwn('query'))) { + this.searchSource.set('query', migrateLegacyQuery(this.searchSource.getOwn('query'))); + } }; /**