restructure queryFilter tests, add tests

added tests around adding filters, moved mutate test to adding filters tests
This commit is contained in:
Joe Fleming 2015-04-30 09:01:12 -07:00
parent d31a37e1fc
commit 158fd830a8
3 changed files with 155 additions and 56 deletions

View file

@ -0,0 +1,115 @@
define(function (require) {
return ['add 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: 'jpg', type: 'phrase' } } },
meta: { negate: false, disabled: false }
},
{
query: { match: { '@tags': { query: 'info', type: 'phrase' } } },
meta: { negate: false, disabled: false }
},
{
query: { match: { '_type': { query: 'nginx', type: 'phrase' } } },
meta: { negate: false, disabled: false }
}
];
});
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('adding filters', function () {
it('should add filters to appState', function () {
queryFilter.addFilters(filters);
expect(appState.filters.length).to.be(3);
expect(globalState.filters.length).to.be(0);
});
it('should accept a single filter', function () {
queryFilter.addFilters(filters[0]);
expect(appState.filters.length).to.be(1);
expect(globalState.filters.length).to.be(0);
});
it('should add filters to globalState', function () {
queryFilter.addFilters(filters, true);
expect(appState.filters.length).to.be(0);
expect(globalState.filters.length).to.be(3);
});
});
describe('filter reconciliation', function () {
it('should de-dupe appState filters being added', function () {
var newFilter = _.cloneDeep(filters[1]);
appState.filters = filters;
expect(appState.filters.length).to.be(3);
queryFilter.addFilters(newFilter);
$rootScope.$digest();
expect(appState.filters.length).to.be(3);
});
it('should de-dupe globalState filters being added', function () {
var newFilter = _.cloneDeep(filters[1]);
globalState.filters = filters;
expect(globalState.filters.length).to.be(3);
queryFilter.addFilters(newFilter, true);
$rootScope.$digest();
expect(globalState.filters.length).to.be(3);
});
it('should mutate global filters on appState filter changes', function () {
var idx = 1;
globalState.filters = filters;
var appFilter = _.cloneDeep(filters[idx]);
appFilter.meta.negate = true;
// use addFilters here, so custom adding logic can be applied
queryFilter.addFilters(appFilter);
var res = queryFilter.getFilters();
expect(res).to.have.length(3);
_.each(res, function (filter, i) {
expect(filter.$state.store).to.be('globalState');
// make sure global filter actually mutated
expect(filter.meta.negate).to.be(i === idx);
});
});
});
}];
});

View file

@ -125,24 +125,6 @@ define(function (require) {
}).length).to.be(1);
});
it('should mutate global filters on appState filter changes', function () {
var idx = 1;
globalState.filters = filters;
var appFilter = _.cloneDeep(filters[idx]);
appFilter.meta.negate = true;
$rootScope.$digest();
appState.filters.push(appFilter);
$rootScope.$digest();
var res = queryFilter.getFilters();
expect(res).to.have.length(3);
_.each(res, function (filter, i) {
expect(filter.$state.store).to.be('globalState');
// make sure global filter actually mutated
expect(filter.meta.negate).to.be(i === idx);
});
});
it('should not affect disabled filters', function () {
// test adding to globalState
globalState.filters = _.map(filters, function (filter) {

View file

@ -4,50 +4,52 @@ define(function (require) {
var EventEmitter;
var $rootScope;
describe('Query Filter Module', function () {
describe('Query Filter', function () {
describe('Module', function () {
beforeEach(module('kibana'));
beforeEach(module('kibana'));
beforeEach(function () {
inject(function (_$rootScope_, Private) {
$rootScope = _$rootScope_;
queryFilter = Private(require('components/filter_bar/query_filter'));
EventEmitter = Private(require('factories/events'));
});
});
beforeEach(function () {
inject(function (_$rootScope_, Private) {
$rootScope = _$rootScope_;
queryFilter = Private(require('components/filter_bar/query_filter'));
EventEmitter = Private(require('factories/events'));
describe('module instance', function () {
it('should be an event emitter', function () {
expect(queryFilter).to.be.an(EventEmitter);
});
});
describe('module methods', function () {
it('should have methods for getting filters', function () {
expect(queryFilter.getFilters).to.be.a('function');
expect(queryFilter.getAppFilters).to.be.a('function');
expect(queryFilter.getGlobalFilters).to.be.a('function');
});
it('should have methods for modifying filters', function () {
expect(queryFilter.addFilters).to.be.a('function');
expect(queryFilter.toggleFilter).to.be.a('function');
expect(queryFilter.toggleAll).to.be.a('function');
expect(queryFilter.removeFilter).to.be.a('function');
expect(queryFilter.removeAll).to.be.a('function');
expect(queryFilter.invertFilter).to.be.a('function');
expect(queryFilter.invertAll).to.be.a('function');
expect(queryFilter.pinFilter).to.be.a('function');
expect(queryFilter.pinAll).to.be.a('function');
});
});
});
describe('module instance', function () {
it('should be an event emitter', function () {
expect(queryFilter).to.be.an(EventEmitter);
describe('Actions', function () {
var childSuites = [
require('specs/components/filter_bar/_getFilters'),
require('specs/components/filter_bar/_addFilters'),
].forEach(function (s) {
describe(s[0], s[1]);
});
});
describe('module methods', function () {
it('should have methods for getting filters', function () {
expect(queryFilter.getFilters).to.be.a('function');
expect(queryFilter.getAppFilters).to.be.a('function');
expect(queryFilter.getGlobalFilters).to.be.a('function');
});
it('should have methods for modifying filters', function () {
expect(queryFilter.addFilters).to.be.a('function');
expect(queryFilter.toggleFilter).to.be.a('function');
expect(queryFilter.toggleAll).to.be.a('function');
expect(queryFilter.removeFilter).to.be.a('function');
expect(queryFilter.removeAll).to.be.a('function');
expect(queryFilter.invertFilter).to.be.a('function');
expect(queryFilter.invertAll).to.be.a('function');
expect(queryFilter.pinFilter).to.be.a('function');
expect(queryFilter.pinAll).to.be.a('function');
});
});
});
describe('Query Filter Actions', function () {
var childSuites = [
require('specs/components/filter_bar/_getFilters'),
].forEach(function (s) {
describe(s[0], s[1]);
});
});
});