Skip to content

Commit

Permalink
removed unused params and added jest test
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Aug 26, 2021
1 parent 4050ae1 commit 7558ab8
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 22 deletions.
105 changes: 105 additions & 0 deletions src/plugins/visualize/public/locator.test.ts
Original file line number Diff line number Diff line change
@@ -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 {}`);
});
});
28 changes: 11 additions & 17 deletions src/plugins/visualize/public/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ export type VisualizeLocatorParams = {

vis?: SerializableRecord;

forceNow?: string;

searchSourceFields?: SerializableRecord;

linked?: boolean;
};

Expand All @@ -46,7 +42,7 @@ export type VisualizeAppLocator = LocatorPublic<VisualizeLocatorParams>;
export const VISUALIZE_APP_LOCATOR = 'VISUALIZE_APP_LOCATOR';

export class VisualizeLocatorDefinition implements LocatorDefinition<VisualizeLocatorParams> {
constructor(private readonly deps: { useHash: boolean }) {}
constructor() {}

id = VISUALIZE_APP_LOCATOR;

Expand All @@ -58,34 +54,32 @@ export class VisualizeLocatorDefinition implements LocatorDefinition<VisualizeLo
path = params.type ? `${path}?type=${params.type}` : path;

path = setStateToKbnUrl(
'_a',
'_g',
omitBy(
{
linked: params.linked,
time: params.timeRange,
filters: params.filters?.filter((f) => !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
);

Expand Down
6 changes: 1 addition & 5 deletions src/plugins/visualize/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7558ab8

Please sign in to comment.