Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Checks to Dashboard Context #13182

Merged
merged 4 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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: []
}
});
});

});

});


7 changes: 4 additions & 3 deletions src/core_plugins/kibana/public/dashboard/dashboard_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/ui/public/courier/saved_object/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
};

/**
Expand Down