2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { xyVisualization } from './xy_visualization';
|
|
|
|
import { Position } from '@elastic/charts';
|
|
|
|
import { Operation } from '../types';
|
2019-10-02 19:21:39 +02:00
|
|
|
import { State, SeriesType } from './types';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { createMockDatasource, createMockFramePublicAPI } from '../editor_frame_plugin/mocks';
|
|
|
|
import { generateId } from '../id_generator';
|
|
|
|
import { Ast } from '@kbn/interpreter/target/common';
|
|
|
|
|
|
|
|
jest.mock('../id_generator');
|
|
|
|
|
|
|
|
function exampleState(): State {
|
|
|
|
return {
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('xy_visualization', () => {
|
2019-10-02 19:21:39 +02:00
|
|
|
describe('getDescription', () => {
|
|
|
|
function mixedState(...types: SeriesType[]) {
|
|
|
|
const state = exampleState();
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
layers: types.map((t, i) => ({
|
|
|
|
...state.layers[0],
|
|
|
|
layerId: `layer_${i}`,
|
|
|
|
seriesType: t,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should show mixed xy chart when multilple series types', () => {
|
|
|
|
const desc = xyVisualization.getDescription(mixedState('bar', 'line'));
|
|
|
|
|
|
|
|
expect(desc.label).toEqual('Mixed XY Chart');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show mixed horizontal bar chart when multiple horizontal bar types', () => {
|
|
|
|
const desc = xyVisualization.getDescription(
|
|
|
|
mixedState('bar_horizontal', 'bar_horizontal_stacked')
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(desc.label).toEqual('Mixed Horizontal Bar Chart');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show bar chart when bar only', () => {
|
|
|
|
const desc = xyVisualization.getDescription(mixedState('bar_horizontal', 'bar_horizontal'));
|
|
|
|
|
|
|
|
expect(desc.label).toEqual('Horizontal Bar Chart');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show the chart description if not mixed', () => {
|
|
|
|
expect(xyVisualization.getDescription(mixedState('area')).label).toEqual('Area Chart');
|
|
|
|
expect(xyVisualization.getDescription(mixedState('line')).label).toEqual('Line Chart');
|
|
|
|
expect(xyVisualization.getDescription(mixedState('area_stacked')).label).toEqual(
|
|
|
|
'Stacked Area Chart'
|
|
|
|
);
|
|
|
|
expect(xyVisualization.getDescription(mixedState('bar_horizontal_stacked')).label).toEqual(
|
|
|
|
'Stacked Horizontal Bar Chart'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
describe('#initialize', () => {
|
|
|
|
it('loads default state', () => {
|
|
|
|
(generateId as jest.Mock)
|
|
|
|
.mockReturnValueOnce('test-id1')
|
|
|
|
.mockReturnValueOnce('test-id2')
|
|
|
|
.mockReturnValue('test-id3');
|
|
|
|
const mockFrame = createMockFramePublicAPI();
|
|
|
|
const initialState = xyVisualization.initialize(mockFrame);
|
|
|
|
|
|
|
|
expect(initialState.layers).toHaveLength(1);
|
|
|
|
expect(initialState.layers[0].xAccessor).toBeDefined();
|
|
|
|
expect(initialState.layers[0].accessors[0]).toBeDefined();
|
|
|
|
expect(initialState.layers[0].xAccessor).not.toEqual(initialState.layers[0].accessors[0]);
|
|
|
|
|
|
|
|
expect(initialState).toMatchInlineSnapshot(`
|
|
|
|
Object {
|
|
|
|
"layers": Array [
|
|
|
|
Object {
|
|
|
|
"accessors": Array [
|
|
|
|
"test-id1",
|
|
|
|
],
|
|
|
|
"layerId": "",
|
|
|
|
"position": "top",
|
2019-10-01 21:04:00 +02:00
|
|
|
"seriesType": "bar_stacked",
|
2019-09-17 20:57:53 +02:00
|
|
|
"showGridlines": false,
|
|
|
|
"splitAccessor": "test-id2",
|
|
|
|
"xAccessor": "test-id3",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"legend": Object {
|
|
|
|
"isVisible": true,
|
|
|
|
"position": "right",
|
|
|
|
},
|
2019-10-01 21:04:00 +02:00
|
|
|
"preferredSeriesType": "bar_stacked",
|
2019-09-17 20:57:53 +02:00
|
|
|
"title": "Empty XY Chart",
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('loads from persisted state', () => {
|
|
|
|
expect(xyVisualization.initialize(createMockFramePublicAPI(), exampleState())).toEqual(
|
|
|
|
exampleState()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#getPersistableState', () => {
|
|
|
|
it('persists the state as given', () => {
|
|
|
|
expect(xyVisualization.getPersistableState(exampleState())).toEqual(exampleState());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#toExpression', () => {
|
|
|
|
let mockDatasource: ReturnType<typeof createMockDatasource>;
|
|
|
|
let frame: ReturnType<typeof createMockFramePublicAPI>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
frame = createMockFramePublicAPI();
|
|
|
|
mockDatasource = createMockDatasource();
|
|
|
|
|
|
|
|
mockDatasource.publicAPIMock.getTableSpec.mockReturnValue([
|
|
|
|
{ columnId: 'd' },
|
|
|
|
{ columnId: 'a' },
|
|
|
|
{ columnId: 'b' },
|
|
|
|
{ columnId: 'c' },
|
|
|
|
]);
|
|
|
|
|
|
|
|
mockDatasource.publicAPIMock.getOperationForColumnId.mockImplementation(col => {
|
|
|
|
return { label: `col_${col}`, dataType: 'number' } as Operation;
|
|
|
|
});
|
|
|
|
|
|
|
|
frame.datasourceLayers = {
|
|
|
|
first: mockDatasource.publicAPIMock,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should map to a valid AST', () => {
|
|
|
|
expect(xyVisualization.toExpression(exampleState(), frame)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should default to labeling all columns with their column label', () => {
|
|
|
|
const expression = xyVisualization.toExpression(exampleState(), frame)! as Ast;
|
|
|
|
|
|
|
|
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('b');
|
|
|
|
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('c');
|
|
|
|
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('d');
|
|
|
|
expect(expression.chain[0].arguments.xTitle).toEqual(['col_a']);
|
|
|
|
expect(expression.chain[0].arguments.yTitle).toEqual(['col_b']);
|
|
|
|
expect(
|
|
|
|
(expression.chain[0].arguments.layers[0] as Ast).chain[0].arguments.columnToLabel
|
|
|
|
).toEqual([
|
|
|
|
JSON.stringify({
|
|
|
|
b: 'col_b',
|
|
|
|
c: 'col_c',
|
|
|
|
d: 'col_d',
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|