diff --git a/src/plugins/data/public/query/filter_manager/filter_manager.test.ts b/src/plugins/data/public/query/filter_manager/filter_manager.test.ts index 1ade48b3537c8..8c790ac2ddc46 100644 --- a/src/plugins/data/public/query/filter_manager/filter_manager.test.ts +++ b/src/plugins/data/public/query/filter_manager/filter_manager.test.ts @@ -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; @@ -224,6 +231,44 @@ describe('filter_manager', () => { expect(newGlobalFilters).toHaveLength(2); expect(newAppFilters).toHaveLength(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', () => { diff --git a/src/plugins/data/public/query/filter_manager/filter_manager.ts b/src/plugins/data/public/query/filter_manager/filter_manager.ts index 6c5cdbaffce5e..aa77f10d89f63 100644 --- a/src/plugins/data/public/query/filter_manager/filter_manager.ts +++ b/src/plugins/data/public/query/filter_manager/filter_manager.ts @@ -124,7 +124,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]; } @@ -133,12 +136,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; @@ -157,7 +154,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);