mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
refactor remove filter code, update tests
allow removal by matching, not only by exact object passing
This commit is contained in:
parent
32b03634ee
commit
a5bf6036db
2 changed files with 50 additions and 28 deletions
|
@ -59,14 +59,27 @@ define(function (require) {
|
|||
/**
|
||||
* Removes the filter from the proper state
|
||||
* @param {object} matchFilter The filter to remove
|
||||
* @returns {object} Resulting new filter list
|
||||
*/
|
||||
queryFilter.removeFilter = function (matchFilter) {
|
||||
var state = getStateByFilter(matchFilter);
|
||||
if (!state) return;
|
||||
var appState = getAppState();
|
||||
var filter = _.omit(matchFilter, ['$$hashKey']);
|
||||
var state;
|
||||
var index;
|
||||
|
||||
_.pull(state.filters, matchFilter);
|
||||
return saveState();
|
||||
// check for filter in appState
|
||||
if (appState) {
|
||||
index = _.findIndex(appState.filters, filter);
|
||||
if (index !== -1) state = appState;
|
||||
}
|
||||
|
||||
// if not found, check for filter in globalState
|
||||
if (!state) {
|
||||
index = _.findIndex(globalState.filters, filter);
|
||||
if (index !== -1) state = globalState;
|
||||
else return; // not found in either state, do nothing
|
||||
}
|
||||
|
||||
state.filters.splice(index, 1);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -202,20 +215,6 @@ define(function (require) {
|
|||
};
|
||||
}
|
||||
|
||||
// get state (app or global) or the filter passed in
|
||||
function getStateByFilter(filter) {
|
||||
var appState = getAppState();
|
||||
if (appState) {
|
||||
var appIndex = _.indexOf(appState.filters, filter);
|
||||
if (appIndex !== -1) return appState;
|
||||
}
|
||||
|
||||
var globalIndex = _.indexOf(globalState.filters, filter);
|
||||
if (globalIndex !== -1) return globalState;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// helper to run a function on all filters in all states
|
||||
function executeOnFilters(fn) {
|
||||
var appState = getAppState();
|
||||
|
|
|
@ -9,7 +9,7 @@ define(function (require) {
|
|||
};
|
||||
var filters;
|
||||
var queryFilter;
|
||||
var $rootScope, appState, globalState;
|
||||
var $rootScope, getIndexPatternStub, appState, globalState;
|
||||
|
||||
beforeEach(module('kibana'));
|
||||
|
||||
|
@ -34,7 +34,13 @@ define(function (require) {
|
|||
});
|
||||
|
||||
beforeEach(function () {
|
||||
|
||||
module('kibana/global_state', function ($provide) {
|
||||
$provide.service('courier', function () {
|
||||
var courier = { indexPatterns: { get: getIndexPatternStub } };
|
||||
return courier;
|
||||
});
|
||||
|
||||
$provide.service('getAppState', function () {
|
||||
return function () {
|
||||
return appState;
|
||||
|
@ -48,7 +54,11 @@ define(function (require) {
|
|||
});
|
||||
|
||||
beforeEach(function () {
|
||||
inject(function (_$rootScope_, Private) {
|
||||
getIndexPatternStub = sinon.stub();
|
||||
|
||||
inject(function (_$rootScope_, Private, Promise) {
|
||||
var indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern'));
|
||||
getIndexPatternStub.returns(Promise.resolve(indexPattern));
|
||||
$rootScope = _$rootScope_;
|
||||
queryFilter = Private(require('components/filter_bar/query_filter'));
|
||||
});
|
||||
|
@ -72,11 +82,9 @@ define(function (require) {
|
|||
it('should fire the update and fetch events', function () {
|
||||
var emitSpy = sinon.spy(queryFilter, 'emit');
|
||||
appState.filters = filters;
|
||||
|
||||
// set up the watchers
|
||||
$rootScope.$digest();
|
||||
|
||||
queryFilter.removeFilter(filters[0]);
|
||||
// trigger the digest loop to fire the watchers
|
||||
$rootScope.$digest();
|
||||
|
||||
expect(emitSpy.callCount).to.be(2);
|
||||
|
@ -84,18 +92,33 @@ define(function (require) {
|
|||
expect(emitSpy.secondCall.args[0]).to.be('fetch');
|
||||
});
|
||||
|
||||
it('should only remove matching instances', function () {
|
||||
it('should remove matching filters', function () {
|
||||
globalState.filters.push(filters[0]);
|
||||
globalState.filters.push(filters[1]);
|
||||
appState.filters.push(filters[2]);
|
||||
$rootScope.$digest();
|
||||
|
||||
queryFilter.removeFilter(filters[0]);
|
||||
$rootScope.$digest();
|
||||
expect(globalState.filters).to.have.length(1);
|
||||
expect(appState.filters).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should remove matching filters by comparison', function () {
|
||||
globalState.filters.push(filters[0]);
|
||||
globalState.filters.push(filters[1]);
|
||||
appState.filters.push(filters[2]);
|
||||
$rootScope.$digest();
|
||||
|
||||
queryFilter.removeFilter(_.cloneDeep(filters[0]));
|
||||
expect(globalState.filters).to.have.length(2);
|
||||
$rootScope.$digest();
|
||||
expect(globalState.filters).to.have.length(1);
|
||||
expect(appState.filters).to.have.length(1);
|
||||
|
||||
queryFilter.removeFilter(_.cloneDeep(filters[2]));
|
||||
expect(globalState.filters).to.have.length(2);
|
||||
expect(appState.filters).to.have.length(1);
|
||||
$rootScope.$digest();
|
||||
expect(globalState.filters).to.have.length(1);
|
||||
expect(appState.filters).to.have.length(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue