2020-03-17 14:57:52 +01: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 { Ast } from '@kbn/interpreter/target/common';
|
|
|
|
import { Position } from '@elastic/charts';
|
2020-11-04 11:27:52 +01:00
|
|
|
import { chartPluginMock } from '../../../../../src/plugins/charts/public/mocks';
|
|
|
|
import { getXyVisualization } from './xy_visualization';
|
2020-03-17 14:57:52 +01:00
|
|
|
import { Operation } from '../types';
|
|
|
|
import { createMockDatasource, createMockFramePublicAPI } from '../editor_frame_service/mocks';
|
2020-11-17 10:19:13 +01:00
|
|
|
import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';
|
2020-03-17 14:57:52 +01:00
|
|
|
|
|
|
|
describe('#toExpression', () => {
|
2020-11-04 11:27:52 +01:00
|
|
|
const xyVisualization = getXyVisualization({
|
|
|
|
paletteService: chartPluginMock.createPaletteRegistry(),
|
2020-11-17 10:19:13 +01:00
|
|
|
data: dataPluginMock.createStartContract(),
|
2020-11-04 11:27:52 +01:00
|
|
|
});
|
2020-03-17 14:57:52 +01:00
|
|
|
let mockDatasource: ReturnType<typeof createMockDatasource>;
|
|
|
|
let frame: ReturnType<typeof createMockFramePublicAPI>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
frame = createMockFramePublicAPI();
|
|
|
|
mockDatasource = createMockDatasource('testDatasource');
|
|
|
|
|
|
|
|
mockDatasource.publicAPIMock.getTableSpec.mockReturnValue([
|
|
|
|
{ columnId: 'd' },
|
|
|
|
{ columnId: 'a' },
|
|
|
|
{ columnId: 'b' },
|
|
|
|
{ columnId: 'c' },
|
|
|
|
]);
|
|
|
|
|
2020-05-22 09:08:58 +02:00
|
|
|
mockDatasource.publicAPIMock.getOperationForColumnId.mockImplementation((col) => {
|
2020-03-17 14:57:52 +01:00
|
|
|
return { label: `col_${col}`, dataType: 'number' } as Operation;
|
|
|
|
});
|
|
|
|
|
|
|
|
frame.datasourceLayers = {
|
|
|
|
first: mockDatasource.publicAPIMock,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should map to a valid AST', () => {
|
|
|
|
expect(
|
|
|
|
xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-03-17 14:57:52 +01:00
|
|
|
preferredSeriesType: 'bar',
|
2020-07-03 16:15:06 +02:00
|
|
|
fittingFunction: 'Carry',
|
2020-09-16 10:30:19 +02:00
|
|
|
tickLabelsVisibilitySettings: { x: false, yLeft: true, yRight: true },
|
|
|
|
gridlinesVisibilitySettings: { x: false, yLeft: true, yRight: true },
|
2020-03-17 14:57:52 +01:00
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-03-17 14:57:52 +01:00
|
|
|
)
|
|
|
|
).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2020-07-03 16:15:06 +02:00
|
|
|
it('should default the fitting function to None', () => {
|
|
|
|
expect(
|
|
|
|
(xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-07-03 16:15:06 +02:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-07-03 16:15:06 +02:00
|
|
|
) as Ast).chain[0].arguments.fittingFunction[0]
|
|
|
|
).toEqual('None');
|
|
|
|
});
|
|
|
|
|
2020-09-16 10:30:19 +02:00
|
|
|
it('should default the axisTitles visibility settings to true', () => {
|
2020-08-11 19:10:35 +02:00
|
|
|
const expression = xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-08-11 19:10:35 +02:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-08-11 19:10:35 +02:00
|
|
|
) as Ast;
|
2020-09-16 10:30:19 +02:00
|
|
|
expect(
|
|
|
|
(expression.chain[0].arguments.axisTitlesVisibilitySettings[0] as Ast).chain[0].arguments
|
|
|
|
).toEqual({
|
|
|
|
x: [true],
|
|
|
|
yLeft: [true],
|
|
|
|
yRight: [true],
|
|
|
|
});
|
2020-08-11 19:10:35 +02:00
|
|
|
});
|
|
|
|
|
2020-09-15 11:24:02 +02:00
|
|
|
it('should generate an expression without x accessor', () => {
|
|
|
|
const expression = xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-09-15 11:24:02 +02:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: undefined,
|
|
|
|
xAccessor: undefined,
|
|
|
|
accessors: ['a'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
frame.datasourceLayers
|
|
|
|
) as Ast;
|
|
|
|
expect((expression.chain[0].arguments.layers[0] as Ast).chain[0].arguments.xAccessor).toEqual(
|
|
|
|
[]
|
|
|
|
);
|
2020-03-17 14:57:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not generate an expression when missing y', () => {
|
|
|
|
expect(
|
|
|
|
xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-03-17 14:57:52 +01:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: undefined,
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-03-17 14:57:52 +01:00
|
|
|
)
|
|
|
|
).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should default to labeling all columns with their column label', () => {
|
|
|
|
const expression = xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-03-17 14:57:52 +01:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-03-17 14:57:52 +01:00
|
|
|
)! as Ast;
|
|
|
|
|
|
|
|
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('b');
|
|
|
|
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('c');
|
|
|
|
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('d');
|
2020-08-11 19:10:35 +02:00
|
|
|
expect(expression.chain[0].arguments.xTitle).toEqual(['']);
|
|
|
|
expect(expression.chain[0].arguments.yTitle).toEqual(['']);
|
2020-09-16 10:30:19 +02:00
|
|
|
expect(expression.chain[0].arguments.yRightTitle).toEqual(['']);
|
2020-03-17 14:57:52 +01:00
|
|
|
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',
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
2020-08-11 19:10:35 +02:00
|
|
|
|
|
|
|
it('should default the tick labels visibility settings to true', () => {
|
|
|
|
const expression = xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-08-11 19:10:35 +02:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-08-11 19:10:35 +02:00
|
|
|
) as Ast;
|
|
|
|
expect(
|
|
|
|
(expression.chain[0].arguments.tickLabelsVisibilitySettings[0] as Ast).chain[0].arguments
|
|
|
|
).toEqual({
|
|
|
|
x: [true],
|
2020-09-16 10:30:19 +02:00
|
|
|
yLeft: [true],
|
|
|
|
yRight: [true],
|
2020-08-11 19:10:35 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should default the gridlines visibility settings to true', () => {
|
|
|
|
const expression = xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
2020-11-06 16:34:30 +01:00
|
|
|
valueLabels: 'hide',
|
2020-08-11 19:10:35 +02:00
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
frame.datasourceLayers
|
2020-08-11 19:10:35 +02:00
|
|
|
) as Ast;
|
|
|
|
expect(
|
|
|
|
(expression.chain[0].arguments.gridlinesVisibilitySettings[0] as Ast).chain[0].arguments
|
|
|
|
).toEqual({
|
|
|
|
x: [true],
|
2020-09-16 10:30:19 +02:00
|
|
|
yLeft: [true],
|
|
|
|
yRight: [true],
|
2020-08-11 19:10:35 +02:00
|
|
|
});
|
|
|
|
});
|
2020-11-06 16:34:30 +01:00
|
|
|
|
|
|
|
it('should correctly report the valueLabels visibility settings', () => {
|
|
|
|
const expression = xyVisualization.toExpression(
|
|
|
|
{
|
|
|
|
legend: { position: Position.Bottom, isVisible: true },
|
|
|
|
valueLabels: 'inside',
|
|
|
|
preferredSeriesType: 'bar',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
layerId: 'first',
|
|
|
|
seriesType: 'area',
|
|
|
|
splitAccessor: 'd',
|
|
|
|
xAccessor: 'a',
|
|
|
|
accessors: ['b', 'c'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
frame.datasourceLayers
|
|
|
|
) as Ast;
|
|
|
|
expect(expression.chain[0].arguments.valueLabels[0] as Ast).toEqual('inside');
|
|
|
|
});
|
2020-03-17 14:57:52 +01:00
|
|
|
});
|