add tests for pinning filters, fix force and pinAll bugs

This commit is contained in:
Joe Fleming 2015-04-30 16:00:24 -07:00
parent 42248393d9
commit c79f2c12a8
3 changed files with 154 additions and 4 deletions

View file

@ -153,10 +153,10 @@ define(function (require) {
var globalIndex = _.indexOf(globalState.filters, filter);
if (appIndex === -1 && globalIndex === -1) return;
if (appIndex !== -1) {
if (appIndex !== -1 && force !== false) {
appState.filters.splice(appIndex, 1);
globalState.filters.push(filter);
} else if (globalIndex !== -1) {
} else if (globalIndex !== -1 && force !== true) {
globalState.filters.splice(globalIndex, 1);
appState.filters.push(filter);
}
@ -220,8 +220,13 @@ define(function (require) {
// helper to run a function on all filters in all states
function executeOnFilters(fn) {
var appState = getAppState();
appState.filters.forEach(fn);
globalState.filters.forEach(fn);
var appFilters;
if (appState && appState.filters) {
appFilters = appState.filters;
} else {
appFilters = [];
}
globalState.filters.concat(appFilters).forEach(fn);
}
function mergeAndMutateFilters(globalFilters, appFilters, compareOptions) {

View file

@ -0,0 +1,144 @@
define(function (require) {
return ['pin filters', function () {
var _ = require('lodash');
var MockState = require('fixtures/mock_state');
var storeNames = {
app: 'appState',
global: 'globalState'
};
var filters;
var queryFilter;
var $rootScope, appState, globalState;
beforeEach(module('kibana'));
beforeEach(function () {
appState = new MockState({ filters: [] });
globalState = new MockState({ filters: [] });
filters = [
{
query: { match: { extension: { query: 'gif', type: 'phrase' } } },
meta: { negate: false, disabled: false }
},
{
query: { match: { extension: { query: 'jpg', type: 'phrase' } } },
meta: { negate: true, disabled: false }
},
{
query: { match: { extension: { query: 'png', type: 'phrase' } } },
meta: { negate: true, disabled: true }
},
{
query: { match: { '@tags': { query: 'info', type: 'phrase' } } },
meta: { negate: false, disabled: false }
},
{
query: { match: { '@tags': { query: 'success', type: 'phrase' } } },
meta: { negate: false, disabled: false }
},
{
query: { match: { '@tags': { query: 'security', type: 'phrase' } } },
meta: { negate: true, disabled: false }
},
{
query: { match: { '_type': { query: 'nginx', type: 'phrase' } } },
meta: { negate: false, disabled: false }
},
{
query: { match: { '_type': { query: 'apache', type: 'phrase' } } },
meta: { negate: true, disabled: true }
}
];
});
beforeEach(function () {
module('kibana/global_state', function ($provide) {
$provide.service('getAppState', function () {
return function () {
return appState;
};
});
$provide.service('globalState', function () {
return globalState;
});
});
});
beforeEach(function () {
inject(function (_$rootScope_, Private) {
$rootScope = _$rootScope_;
queryFilter = Private(require('components/filter_bar/query_filter'));
});
});
describe('pin a filter', function () {
beforeEach(function () {
globalState.filters = _.filter(filters, function (filter) {
return !!filter.query.match._type;
});
appState.filters = _.filter(filters, function (filter) {
return !filter.query.match._type;
});
expect(globalState.filters).to.have.length(2);
expect(appState.filters).to.have.length(6);
});
it('should move filter from appState to globalState', function () {
var filter = appState.filters[1];
queryFilter.pinFilter(filter);
expect(globalState.filters).to.contain(filter);
expect(globalState.filters).to.have.length(3);
expect(appState.filters).to.have.length(5);
});
it('should move filter from globalState to appState', function () {
var filter = globalState.filters[1];
queryFilter.pinFilter(filter);
expect(appState.filters).to.contain(filter);
expect(globalState.filters).to.have.length(1);
expect(appState.filters).to.have.length(7);
});
});
describe('bulk pinning', function () {
beforeEach(function () {
globalState.filters = _.filter(filters, function (filter) {
return !!filter.query.match.extension;
});
appState.filters = _.filter(filters, function (filter) {
return !filter.query.match.extension;
});
expect(globalState.filters).to.have.length(3);
expect(appState.filters).to.have.length(5);
});
it('should swap the filters in both states', function () {
var appSample = _.sample(appState.filters);
var globalSample = _.sample(globalState.filters);
queryFilter.pinAll();
expect(globalState.filters).to.have.length(5);
expect(appState.filters).to.have.length(3);
expect(globalState.filters).to.contain(appSample);
expect(appState.filters).to.contain(globalSample);
});
it('should move all filters to globalState', function () {
queryFilter.pinAll(true);
expect(globalState.filters).to.have.length(8);
expect(appState.filters).to.have.length(0);
});
it('should move all filters to appState', function () {
queryFilter.pinAll(false);
expect(globalState.filters).to.have.length(0);
expect(appState.filters).to.have.length(8);
});
});
}];
});

View file

@ -50,6 +50,7 @@ define(function (require) {
require('specs/components/filter_bar/_removeFilters'),
require('specs/components/filter_bar/_toggleFilters'),
require('specs/components/filter_bar/_invertFilters'),
require('specs/components/filter_bar/_pinFilters'),
].forEach(function (s) {
describe(s[0], s[1]);
});