From 4fff8e83fbc51895280f328eb754905c403c3aa4 Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Thu, 6 Feb 2020 15:01:30 -0500 Subject: [PATCH] Update --- .../lens/public/app_plugin/app.test.tsx | 85 +++++++++++++++++-- .../plugins/lens/public/app_plugin/app.tsx | 16 +++- 2 files changed, 91 insertions(+), 10 deletions(-) diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx index 794128832461b..d7e565aee4924 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx @@ -46,7 +46,18 @@ function createMockFilterManager() { const unsubscribe = jest.fn(); let subscriber: () => void; - let filters: unknown = []; + let filters: esFilters.Filter[] = [ + { + meta: { alias: null, negate: false, disabled: false }, + query: { match_phrase: { global: 'yes' } }, + $state: { store: esFilters.FilterStateStore.GLOBAL_STATE }, + }, + { + meta: { alias: null, negate: false, disabled: false }, + query: { match_phrase: { test: 'yes' } }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, + }, + ]; return { getUpdates$: () => ({ @@ -55,11 +66,15 @@ function createMockFilterManager() { return unsubscribe; }, }), - setFilters: (newFilters: unknown[]) => { + setFilters: jest.fn((newFilters: esFilters.Filter[]) => { filters = newFilters; - subscriber(); - }, + if (subscriber) subscriber(); + }), getFilters: () => filters, + getAppFilters: () => + filters.filter(filter => filter.$state?.store === esFilters.FilterStateStore.APP_STATE), + getGlobalFilters: () => + filters.filter(filter => filter.$state?.store === esFilters.FilterStateStore.GLOBAL_STATE), removeAll: () => { filters = []; subscriber(); @@ -166,7 +181,38 @@ describe('Lens App', () => { "toDate": "now", }, "doc": undefined, - "filters": Array [], + "filters": Array [ + Object { + "$state": Object { + "store": "globalState", + }, + "meta": Object { + "alias": null, + "disabled": false, + "negate": false, + }, + "query": Object { + "match_phrase": Object { + "global": "yes", + }, + }, + }, + Object { + "$state": Object { + "store": "appState", + }, + "meta": Object { + "alias": null, + "disabled": false, + "negate": false, + }, + "query": Object { + "match_phrase": Object { + "test": "yes", + }, + }, + }, + ], "onChange": [Function], "onError": [Function], "query": Object { @@ -180,6 +226,15 @@ describe('Lens App', () => { `); }); + it('clears app filters on load', () => { + const defaultArgs = makeDefaultArgs(); + mount(); + + expect(defaultArgs.data.query.filterManager.setFilters).toHaveBeenCalledWith( + defaultArgs.data.query.filterManager.getGlobalFilters() + ); + }); + it('sets breadcrumbs when the document title changes', async () => { const defaultArgs = makeDefaultArgs(); const instance = mount(); @@ -217,14 +272,20 @@ describe('Lens App', () => { expect(args.docStorage.load).not.toHaveBeenCalled(); }); - it('loads a document and uses query if there is a document id', async () => { + it('loads a document and uses query and filters if there is a document id', async () => { const args = makeDefaultArgs(); args.editorFrame = frame; + const savedFilter = { + query: { match_phrase: { src: 'test' } }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, + meta: { disabled: false, negate: false, alias: null }, + }; (args.docStorage.load as jest.Mock).mockResolvedValue({ id: '1234', expression: 'valid expression', state: { query: 'fake query', + filters: [savedFilter], datasourceMetaData: { filterableIndexPatterns: [{ id: '1', title: 'saved' }] }, }, }); @@ -236,6 +297,9 @@ describe('Lens App', () => { expect(args.docStorage.load).toHaveBeenCalledWith('1234'); expect(args.data.indexPatterns.get).toHaveBeenCalledWith('1'); + expect(args.data.query.filterManager.setFilters).toHaveBeenCalledWith( + args.data.query.filterManager.getGlobalFilters().concat([savedFilter]) + ); expect(TopNavMenu).toHaveBeenCalledWith( expect.objectContaining({ query: 'fake query', @@ -251,6 +315,7 @@ describe('Lens App', () => { expression: 'valid expression', state: { query: 'fake query', + filters: [savedFilter], datasourceMetaData: { filterableIndexPatterns: [{ id: '1', title: 'saved' }] }, }, }, @@ -703,14 +768,18 @@ describe('Lens App', () => { const indexPattern = ({ id: 'index1' } as unknown) as IIndexPattern; const field = ({ name: 'myfield' } as unknown) as IFieldType; - args.data.query.filterManager.setFilters([esFilters.buildExistsFilter(field, indexPattern)]); + const filter = { + ...esFilters.buildExistsFilter(field, indexPattern), + $state: { store: esFilters.FilterStateStore.APP_STATE }, + }; + args.data.query.filterManager.setFilters([filter]); instance.update(); expect(frame.mount).toHaveBeenCalledWith( expect.any(Element), expect.objectContaining({ - filters: [esFilters.buildExistsFilter(field, indexPattern)], + filters: [filter], }) ); }); diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx index f33cd41f46a11..53c0720219e31 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx @@ -76,13 +76,16 @@ export function App({ fromDate: currentRange.from, toDate: currentRange.to, }, - filters: [], + filters: data.query.filterManager.getFilters(), }; }); const { lastKnownDoc } = state; useEffect(() => { + if (data.query.filterManager.getAppFilters().length > 0) { + data.query.filterManager.setFilters(data.query.filterManager.getGlobalFilters()); + } const filterSubscription = data.query.filterManager.getUpdates$().subscribe({ next: () => { setState(s => ({ ...s, filters: data.query.filterManager.getFilters() })); @@ -123,13 +126,22 @@ export function App({ core.notifications ) .then(indexPatterns => { + data.query.filterManager.setFilters( + data.query.filterManager + .getGlobalFilters() + .concat( + (doc.state.filters || []).filter( + filter => filter.$state?.store === esFilters.FilterStateStore.APP_STATE + ) + ) + ); + setState(s => ({ ...s, isLoading: false, persistedDoc: doc, lastKnownDoc: doc, query: doc.state.query, - filters: doc.state.filters, indexPatternsForTopNav: indexPatterns, })); })