Skip to content

Commit

Permalink
fix: 🐛 improve getVariableList() and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 10, 2020
1 parent b373527 commit e4ac69d
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 72 deletions.
255 changes: 189 additions & 66 deletions x-pack/plugins/drilldowns/url_drilldown/public/lib/url_drilldown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import { UrlDrilldown, ActionContext, Config } from './url_drilldown';
import { IEmbeddable } from '../../../../../../src/plugins/embeddable/public/lib/embeddables';
import { DatatableColumnType } from '../../../../../../src/plugins/expressions/common';
import { rowClickData, TestEmbeddable } from './test/data';
import { createPoint, rowClickData, TestEmbeddable } from './test/data';
import {
VALUE_CLICK_TRIGGER,
ROW_CLICK_TRIGGER,
} from '../../../../../../src/plugins/ui_actions/public';

const mockDataPoints = [
{
Expand Down Expand Up @@ -171,78 +175,197 @@ describe('UrlDrilldown', () => {
});
});

describe('getRuntimeVariables()', () => {
test('builds runtime variables VALUE_CLICK_TRIGGER trigger', () => {
const embeddable = new TestEmbeddable(
{
id: 'the-id',
query: {
language: 'C++',
query: 'std::cout << 123;',
},
timeRange: {
from: 'FROM',
to: 'TO',
},
filters: [
{
meta: {
alias: 'asdf',
disabled: false,
negate: false,
},
},
],
savedObjectId: 'SAVED_OBJECT_ID',
describe('variables', () => {
const embeddable1 = new TestEmbeddable(
{
id: 'test',
title: 'The Title',
savedObjectId: 'SAVED_OBJECT_IDxx',
},
{
indexPatterns: [{ id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }],
}
);
const data: any = {
data: [
createPoint({ field: 'field0', value: 'value0' }),
createPoint({ field: 'field1', value: 'value1' }),
createPoint({ field: 'field2', value: 'value2' }),
],
};

const embeddable2 = new TestEmbeddable(
{
id: 'the-id',
query: {
language: 'C++',
query: 'std::cout << 123;',
},
{
title: 'The Title',
indexPatterns: [
{ id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' },
{ id: 'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy' },
],
}
);
const variables = urlDrilldown.getRuntimeVariables({ embeddable, data: rowClickData as any });

expect(variables).toMatchObject({
kibanaUrl: 'http://localhost:5601/',
context: {
panel: {
id: 'the-id',
title: 'The Title',
savedObjectId: 'SAVED_OBJECT_ID',
query: {
language: 'C++',
query: 'std::cout << 123;',
timeRange: {
from: 'FROM',
to: 'TO',
},
filters: [
{
meta: {
alias: 'asdf',
disabled: false,
negate: false,
},
timeRange: {
from: 'FROM',
to: 'TO',
},
],
savedObjectId: 'SAVED_OBJECT_ID',
},
{
title: 'The Title',
indexPatterns: [
{ id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' },
{ id: 'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy' },
],
}
);

describe('getRuntimeVariables()', () => {
test('builds runtime variables for VALUE_CLICK_TRIGGER trigger', () => {
const variables = urlDrilldown.getRuntimeVariables({
embeddable: embeddable1,
data,
});

expect(variables).toMatchObject({
kibanaUrl: 'http://localhost:5601/',
context: {
panel: {
id: 'test',
title: 'The Title',
savedObjectId: 'SAVED_OBJECT_IDxx',
indexPatternId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
},
filters: [
},
event: {
key: 'field0',
value: 'value0',
negate: false,
points: [
{
meta: {
alias: 'asdf',
disabled: false,
negate: false,
},
value: 'value0',
key: 'field0',
},
{
value: 'value1',
key: 'field1',
},
{
value: 'value2',
key: 'field2',
},
],
},
},
event: {
rowIndex: 1,
values: ['IT', '2.25', 3, 0, 2],
keys: ['DestCountry', 'FlightTimeHour', '', 'DistanceMiles', 'OriginAirportID'],
columnNames: [
'Top values of DestCountry',
'Top values of FlightTimeHour',
'Count of records',
'Average of DistanceMiles',
'Unique count of OriginAirportID',
],
},
});
});

test('builds runtime variables for ROW_CLICK_TRIGGER trigger', () => {
const variables = urlDrilldown.getRuntimeVariables({
embeddable: embeddable2,
data: rowClickData as any,
});

expect(variables).toMatchObject({
kibanaUrl: 'http://localhost:5601/',
context: {
panel: {
id: 'the-id',
title: 'The Title',
savedObjectId: 'SAVED_OBJECT_ID',
query: {
language: 'C++',
query: 'std::cout << 123;',
},
timeRange: {
from: 'FROM',
to: 'TO',
},
filters: [
{
meta: {
alias: 'asdf',
disabled: false,
negate: false,
},
},
],
},
},
event: {
rowIndex: 1,
values: ['IT', '2.25', 3, 0, 2],
keys: ['DestCountry', 'FlightTimeHour', '', 'DistanceMiles', 'OriginAirportID'],
columnNames: [
'Top values of DestCountry',
'Top values of FlightTimeHour',
'Count of records',
'Average of DistanceMiles',
'Unique count of OriginAirportID',
],
},
});
});
});

describe('getVariableList()', () => {
test('builds variable list for VALUE_CLICK_TRIGGER trigger', () => {
const list = urlDrilldown.getVariableList({
triggers: [VALUE_CLICK_TRIGGER],
embeddable: embeddable1,
});

const expectedList = [
'event.key',
'event.value',
'event.negate',
'event.points',

'context.panel.id',
'context.panel.title',
'context.panel.indexPatternId',
'context.panel.savedObjectId',

'kibanaUrl',
];

for (const expectedItem of expectedList) {
expect(list.includes(expectedItem)).toBe(true);
}
});

test('builds variable list for ROW_CLICK_TRIGGER trigger', () => {
const list = urlDrilldown.getVariableList({
triggers: [ROW_CLICK_TRIGGER],
embeddable: embeddable2,
});

const expectedList = [
'event.columnNames',
'event.keys',
'event.rowIndex',
'event.values',

'context.panel.id',
'context.panel.title',
'context.panel.filters',
'context.panel.query.language',
'context.panel.query.query',
'context.panel.indexPatternIds',
'context.panel.savedObjectId',
'context.panel.timeRange.from',
'context.panel.timeRange.to',

'kibanaUrl',
];

for (const expectedItem of expectedList) {
expect(list.includes(expectedItem)).toBe(true);
}
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ export class UrlDrilldown implements Drilldown<Config, UrlTrigger, ActionFactory
};
};

private readonly getVariableList = (context: ActionFactoryContext): string[] => {
return [
...getEventVariableList(context).sort(),
...Object.keys(getFlattenedObject(getPanelVariables(context))).sort(),
...Object.keys(getFlattenedObject(this.deps.getGlobalScope())).sort(),
];
public readonly getVariableList = (context: ActionFactoryContext): string[] => {
const eventVariables = getEventVariableList(context);
const contextVariables = Object.keys(getFlattenedObject(getPanelVariables(context))).map(
(key) => 'context.panel.' + key
);
const globalVariables = Object.keys(getFlattenedObject(this.deps.getGlobalScope()));

return [...eventVariables.sort(), ...contextVariables.sort(), ...globalVariables.sort()];
};
}

0 comments on commit e4ac69d

Please sign in to comment.