Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Fix setting filters without $store value (#56304) #56475

Merged
merged 1 commit into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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', () => {
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 @@ -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];
}
Expand All @@ -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;
Expand All @@ -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);
Expand Down