From 7558ab8aae4de6d98c8f67bef7488c8efd3bfd07 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Thu, 26 Aug 2021 13:18:23 +0200 Subject: [PATCH] removed unused params and added jest test --- src/plugins/visualize/public/locator.test.ts | 105 +++++++++++++++++++ src/plugins/visualize/public/locator.ts | 28 ++--- src/plugins/visualize/public/plugin.ts | 6 +- 3 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 src/plugins/visualize/public/locator.test.ts diff --git a/src/plugins/visualize/public/locator.test.ts b/src/plugins/visualize/public/locator.test.ts new file mode 100644 index 0000000000000..9f3a761a08a82 --- /dev/null +++ b/src/plugins/visualize/public/locator.test.ts @@ -0,0 +1,105 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { VisualizeLocatorDefinition } from './locator'; +import { esFilters } from '../../data/public'; + +describe('visualize locator', () => { + let definition: VisualizeLocatorDefinition; + + beforeEach(() => { + definition = new VisualizeLocatorDefinition(); + }); + + it('returns a location for "create" path', async () => { + const location = await definition.getLocation({}); + + expect(location.app).toMatchInlineSnapshot(`"visualize"`); + expect(location.path).toMatchInlineSnapshot(`"#/create?_g=()&_a=()"`); + expect(location.state).toMatchInlineSnapshot(`Object {}`); + }); + + it('returns a location for "edit" path', async () => { + const location = await definition.getLocation({ visId: 'test', type: 'test' }); + + expect(location.app).toMatchInlineSnapshot(`"visualize"`); + expect(location.path).toMatchInlineSnapshot(`"#/edit/test?type=test&_g=()&_a=()"`); + expect(location.state).toMatchInlineSnapshot(`Object {}`); + }); + + it('creates a location with query, filters (global and app), refresh interval and time range', async () => { + const location = await definition.getLocation({ + visId: '123', + type: 'test', + timeRange: { to: 'now', from: 'now-15m', mode: 'relative' }, + refreshInterval: { pause: false, value: 300 }, + filters: [ + { + meta: { + alias: null, + disabled: false, + negate: false, + }, + query: { query: 'hi' }, + }, + { + meta: { + alias: null, + disabled: false, + negate: false, + }, + query: { query: 'hi' }, + $state: { + store: esFilters.FilterStateStore.GLOBAL_STATE, + }, + }, + ], + query: { query: 'bye', language: 'kuery' }, + }); + + expect(location.app).toMatchInlineSnapshot(`"visualize"`); + + expect(location.path.match(/filters:/g)?.length).toBe(2); + expect(location.path.match(/refreshInterval:/g)?.length).toBe(1); + expect(location.path.match(/time:/g)?.length).toBe(1); + expect(location.path).toMatchInlineSnapshot( + `"#/edit/123?type=test&_g=(filters:!(('$state':(store:globalState),meta:(alias:!n,disabled:!f,negate:!f),query:(query:hi))),refreshInterval:(pause:!f,value:300),time:(from:now-15m,mode:relative,to:now))&_a=(filters:!((meta:(alias:!n,disabled:!f,negate:!f),query:(query:hi))),query:(language:kuery,query:bye))"` + ); + + expect(location.state).toMatchInlineSnapshot(`Object {}`); + }); + + it('creates a location with all values provided', async () => { + const location = await definition.getLocation({ + visId: '123', + type: 'test', + timeRange: { to: 'now', from: 'now-15m', mode: 'relative' }, + refreshInterval: { pause: false, value: 300 }, + filters: [ + { + meta: { + alias: null, + disabled: false, + negate: false, + }, + query: { query: 'hi' }, + }, + ], + query: { query: 'bye', language: 'kuery' }, + linked: true, + uiState: { fakeUIState: 'fakeUIState' }, + vis: { fakeVis: 'fakeVis' }, + }); + + expect(location.app).toMatchInlineSnapshot(`"visualize"`); + expect(location.path).toMatchInlineSnapshot( + `"#/edit/123?type=test&_g=(filters:!(),refreshInterval:(pause:!f,value:300),time:(from:now-15m,mode:relative,to:now))&_a=(filters:!((meta:(alias:!n,disabled:!f,negate:!f),query:(query:hi))),linked:!t,query:(language:kuery,query:bye),uiState:(fakeUIState:fakeUIState),vis:(fakeVis:fakeVis))"` + ); + expect(location.state).toMatchInlineSnapshot(`Object {}`); + }); +}); diff --git a/src/plugins/visualize/public/locator.ts b/src/plugins/visualize/public/locator.ts index 37b52743518e9..5f9ac6d1ec704 100644 --- a/src/plugins/visualize/public/locator.ts +++ b/src/plugins/visualize/public/locator.ts @@ -34,10 +34,6 @@ export type VisualizeLocatorParams = { vis?: SerializableRecord; - forceNow?: string; - - searchSourceFields?: SerializableRecord; - linked?: boolean; }; @@ -46,7 +42,7 @@ export type VisualizeAppLocator = LocatorPublic; export const VISUALIZE_APP_LOCATOR = 'VISUALIZE_APP_LOCATOR'; export class VisualizeLocatorDefinition implements LocatorDefinition { - constructor(private readonly deps: { useHash: boolean }) {} + constructor() {} id = VISUALIZE_APP_LOCATOR; @@ -58,34 +54,32 @@ export class VisualizeLocatorDefinition implements LocatorDefinition !esFilters.isFilterPinned(f)), - uiState: params.uiState, - query: params.query, - vis: params.vis, + filters: params.filters?.filter((f) => esFilters.isFilterPinned(f)), refreshInterval: params.refreshInterval, }, (v) => v == null ), - { useHash: this.deps.useHash }, + { useHash: false }, path ); path = setStateToKbnUrl( - '_g', + '_a', omitBy( { - time: params.timeRange, - filters: params.filters?.filter((f) => esFilters.isFilterPinned(f)), - refreshInterval: params.refreshInterval, + linked: params.linked, + filters: params.filters?.filter((f) => !esFilters.isFilterPinned(f)), + uiState: params.uiState, + query: params.query, + vis: params.vis, }, (v) => v == null ), - { useHash: this.deps.useHash }, + { useHash: false }, path ); diff --git a/src/plugins/visualize/public/plugin.ts b/src/plugins/visualize/public/plugin.ts index fb97b6827f0a1..c3855d321c933 100644 --- a/src/plugins/visualize/public/plugin.ts +++ b/src/plugins/visualize/public/plugin.ts @@ -244,11 +244,7 @@ export class VisualizePlugin } if (share) { - share.url.locators.create( - new VisualizeLocatorDefinition({ - useHash: core.uiSettings.get('state:storeInSessionStorage'), - }) - ); + share.url.locators.create(new VisualizeLocatorDefinition()); } return {