Skip to content

Commit 399df4d

Browse files
author
Liza Katz
authored
backport disco-filters (#50122)
1 parent f661af1 commit 399df4d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.test.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,15 @@ describe('filter_manager', () => {
228228
expect(appStateStub.filters.length).toBe(1);
229229
});
230230

231-
test('app state should accept array', async () => {
231+
test('app state should accept array and preserve order', async () => {
232232
const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34);
233233
const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'female');
234+
234235
filterManager.addFilters([f1]);
235236
filterManager.addFilters([f2]);
236-
expect(filterManager.getAppFilters()).toHaveLength(2);
237+
const appFilters = filterManager.getAppFilters();
238+
expect(appFilters).toHaveLength(2);
239+
expect(appFilters).toEqual([f1, f2]);
237240
expect(filterManager.getGlobalFilters()).toHaveLength(0);
238241
expect(appStateStub.filters.length).toBe(2);
239242
});
@@ -248,13 +251,33 @@ describe('filter_manager', () => {
248251
expect(globalStateStub.filters.length).toBe(1);
249252
});
250253

251-
test('global state should be accept array', async () => {
254+
test('global state should be accept array and preserve order', async () => {
252255
const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34);
253256
const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'gender', 'female');
257+
254258
filterManager.addFilters([f1, f2]);
255259
expect(filterManager.getAppFilters()).toHaveLength(0);
256-
expect(filterManager.getGlobalFilters()).toHaveLength(2);
257-
expect(globalStateStub.filters.length).toBe(2);
260+
const globalFilters = filterManager.getGlobalFilters();
261+
expect(globalFilters).toHaveLength(2);
262+
expect(globalFilters).toEqual([f1, f2]);
263+
});
264+
265+
test('mixed filters: global filters should stay in the beginning', async () => {
266+
const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34);
267+
const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'female');
268+
filterManager.addFilters([f1, f2]);
269+
const filters = filterManager.getFilters();
270+
expect(filters).toHaveLength(2);
271+
expect(filters).toEqual([f1, f2]);
272+
});
273+
274+
test('mixed filters: global filters should move to the beginning', async () => {
275+
const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34);
276+
const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'gender', 'female');
277+
filterManager.addFilters([f1, f2]);
278+
const filters = filterManager.getFilters();
279+
expect(filters).toHaveLength(2);
280+
expect(filters).toEqual([f2, f1]);
258281
});
259282

260283
test('add multiple filters at once', async () => {

src/legacy/core_plugins/data/public/filter/filter_manager/filter_manager.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ export class FilterManager {
7676
private handleStateUpdate(newFilters: Filter[]) {
7777
// global filters should always be first
7878
newFilters.sort(({ $state: a }: Filter, { $state: b }: Filter): number => {
79-
return a!.store === FilterStateStore.GLOBAL_STATE &&
80-
b!.store !== FilterStateStore.GLOBAL_STATE
81-
? -1
82-
: 1;
79+
if (a!.store === b!.store) {
80+
return 0;
81+
} else {
82+
return a!.store === FilterStateStore.GLOBAL_STATE &&
83+
b!.store !== FilterStateStore.GLOBAL_STATE
84+
? -1
85+
: 1;
86+
}
8387
});
8488

8589
const filtersUpdated = !_.isEqual(this.filters, newFilters);

0 commit comments

Comments
 (0)