Skip to content

Commit

Permalink
test: 💍 add getPanelVariables() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 10, 2020
1 parent ff5e414 commit 919aa56
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import {
getEventScope,
ValueClickTriggerEventScope,
getEventVariableList,
getPanelVariables,
} from './url_drilldown_scope';
import { DatatableColumnType } from '../../../../../../src/plugins/expressions/common';
import {
RowClickContext,
ROW_CLICK_TRIGGER,
} from '../../../../../../src/plugins/ui_actions/public';
import {
Embeddable,
EmbeddableInput,
EmbeddableOutput,
} from '../../../../../../src/plugins/embeddable/public';

const createPoint = ({
field,
Expand Down Expand Up @@ -247,3 +253,207 @@ describe('ROW_CLICK_TRIGGER', () => {
});
});
});

interface TestInput extends EmbeddableInput {
savedObjectId?: string;
}

interface TestOutput extends EmbeddableOutput {
indexPatterns?: Array<{ id: string }>;
}

class TestEmbeddable extends Embeddable<TestInput, TestOutput> {
type = 'test';

destroy() {}
reload() {}
}

describe('getPanelVariables()', () => {
test('returns only ID for empty embeddable', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
},
{}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
});
});

test('returns title as specified in input', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
title: 'title1',
},
{}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
title: 'title1',
});
});

test('returns output title if input and output titles are specified', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
title: 'title1',
},
{
title: 'title2',
}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
title: 'title2',
});
});

test('returns title from output if title in input is missing', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
},
{
title: 'title2',
}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
title: 'title2',
});
});

test('returns saved object ID from output', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
savedObjectId: '5678',
},
{
savedObjectId: '1234',
}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
savedObjectId: '1234',
});
});

test('returns saved object ID from input if it is not set on output', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
savedObjectId: '5678',
},
{}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
savedObjectId: '5678',
});
});

test('returns query, timeRange and filters from input', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
query: {
language: 'C++',
query: 'std::cout << 123;',
},
timeRange: {
from: 'FROM',
to: 'TO',
},
filters: [
{
meta: {
alias: 'asdf',
disabled: false,
negate: false,
},
},
],
},
{}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
query: {
language: 'C++',
query: 'std::cout << 123;',
},
timeRange: {
from: 'FROM',
to: 'TO',
},
filters: [
{
meta: {
alias: 'asdf',
disabled: false,
negate: false,
},
},
],
});
});

test('returns a single index pattern from output', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
},
{
indexPatterns: [{ id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }],
}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
indexPatternId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
});
});

test('returns multiple index patterns from output', () => {
const embeddable = new TestEmbeddable(
{
id: 'test',
},
{
indexPatterns: [
{ id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' },
{ id: 'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy' },
],
}
);
const vars = getPanelVariables({ embeddable });

expect(vars).toEqual({
id: 'test',
indexPatternIds: [
'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy',
],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ interface EmbeddableUrlDrilldownContextScope {
savedObjectId?: string;
}

export function getPanelVariables(
contextScopeInput: ContextScopeInput
): EmbeddableUrlDrilldownContextScope {
export function getPanelVariables(contextScopeInput: {
embeddable?: IEmbeddable;
}): EmbeddableUrlDrilldownContextScope {
function hasEmbeddable(val: unknown): val is { embeddable: IEmbeddable } {
if (val && typeof val === 'object' && 'embeddable' in val) return true;
return false;
Expand All @@ -72,8 +72,8 @@ export function getPanelVariables(
throw new Error(
"UrlDrilldown [getContextScope] can't build scope because embeddable object is missing in context"
);

const embeddable = contextScopeInput.embeddable;

return getEmbeddableVariables(embeddable);
}

Expand Down

0 comments on commit 919aa56

Please sign in to comment.