Skip to content

Commit

Permalink
Fix setting filters without $store value (#56304) (#56476)
Browse files Browse the repository at this point in the history
Follow up on: #56213

AddFilters method had logic which ensured that $store exists in filter object, but setFilters was missing that.
  • Loading branch information
Dosant authored Jan 31, 2020
1 parent 1b6c706 commit 7f9564e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ import { esFilters } from '../../../common';
import { coreMock } from '../../../../../core/public/mocks';
const setupMock = coreMock.createSetup();

setupMock.uiSettings.get.mockImplementation((key: string) => {
return true;
});
const uiSettingsMock = (pinnedByDefault: boolean) => (key: string) => {
switch (key) {
case 'filters:pinnedByDefault':
return pinnedByDefault;
default:
throw new Error(`Unexpected uiSettings key in FilterManager mock: ${key}`);
}
};

setupMock.uiSettings.get.mockImplementation(uiSettingsMock(true));

describe('filter_manager', () => {
let updateSubscription: Subscription | undefined;
Expand Down Expand Up @@ -170,6 +177,44 @@ describe('filter_manager', () => {
expect(fetchStub).toBeCalledTimes(0);
expect(updateStub).toBeCalledTimes(1);
});

test('set filter with no state, and force pin', async () => {
const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38);
f1.$state = undefined;

filterManager.setFilters([f1], true);
expect(filterManager.getGlobalFilters()).toHaveLength(1);
expect(filterManager.getAppFilters()).toHaveLength(0);
});

test('set filter with no state, and no pin', async () => {
const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38);
f1.$state = undefined;

filterManager.setFilters([f1], false);
expect(filterManager.getGlobalFilters()).toHaveLength(0);
expect(filterManager.getAppFilters()).toHaveLength(1);
});

test('set filters with default pin', async () => {
const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38);
f1.$state = undefined;
setupMock.uiSettings.get.mockImplementationOnce(uiSettingsMock(true));

filterManager.setFilters([f1]);
expect(filterManager.getGlobalFilters()).toHaveLength(1);
expect(filterManager.getAppFilters()).toHaveLength(0);
});

test('set filters without default pin', async () => {
const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38);
f1.$state = undefined;

setupMock.uiSettings.get.mockImplementationOnce(uiSettingsMock(false));
filterManager.setFilters([f1]);
expect(filterManager.getGlobalFilters()).toHaveLength(0);
expect(filterManager.getAppFilters()).toHaveLength(1);
});
});

describe('add filters', () => {
Expand Down
22 changes: 14 additions & 8 deletions src/plugins/data/public/query/filter_manager/filter_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export class FilterManager {

/* Setters */

public addFilters(filters: esFilters.Filter[] | esFilters.Filter, pinFilterStatus?: boolean) {
public addFilters(
filters: esFilters.Filter[] | esFilters.Filter,
pinFilterStatus: boolean = this.uiSettings.get('filters:pinnedByDefault')
) {
if (!Array.isArray(filters)) {
filters = [filters];
}
Expand All @@ -130,12 +133,6 @@ export class FilterManager {
return;
}

if (pinFilterStatus === undefined) {
pinFilterStatus = this.uiSettings.get('filters:pinnedByDefault');
}

// Set the store of all filters. For now.
// In the future, all filters should come in with filter state store already set.
const store = pinFilterStatus
? esFilters.FilterStateStore.GLOBAL_STATE
: esFilters.FilterStateStore.APP_STATE;
Expand All @@ -154,7 +151,16 @@ export class FilterManager {
this.handleStateUpdate(newFilters);
}

public setFilters(newFilters: esFilters.Filter[]) {
public setFilters(
newFilters: esFilters.Filter[],
pinFilterStatus: boolean = this.uiSettings.get('filters:pinnedByDefault')
) {
const store = pinFilterStatus
? esFilters.FilterStateStore.GLOBAL_STATE
: esFilters.FilterStateStore.APP_STATE;

FilterManager.setFiltersStore(newFilters, store);

const mappedFilters = mapAndFlattenFilters(newFilters);
const newPartitionedFilters = FilterManager.partitionFilters(mappedFilters);
const mergedFilters = this.mergeIncomingFilters(newPartitionedFilters);
Expand Down

0 comments on commit 7f9564e

Please sign in to comment.