Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Jul 7, 2022
1 parent 8e9ba98 commit cac1728
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Tooltip', () => {
splitSeriesAccessors: {
[splitAccessors[0] as string]: {
format: { id: 'date' },
formatter: {} as FieldFormat,
formatter: { convert: (value) => `formatted-date-${value}` } as FieldFormat,
},
},
splitRowAccessors: { [splitRowAccessor]: { id: 'number' } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import { getColorAssignments } from './color_assignment';
import type { DataLayerConfig } from '../../common';
import type { FormatFactory } from '../types';
import { LayerTypes } from '../../common/constants';
import { Datatable } from '@kbn/expressions-plugin';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
import { LayersFieldFormats } from './layers';

describe('color_assignment', () => {
const tables: Record<string, Datatable> = {
Expand Down Expand Up @@ -83,23 +84,55 @@ describe('color_assignment', () => {
},
];

const formatFactory = (() =>
({
convert(x: unknown) {
return x;
const fieldFormats = {
first: {
splitSeriesAccessors: {
split1: {
format: { id: 'string' },
formatter: {
convert: (x) => x,
} as FieldFormat,
},
},
},
second: {
splitSeriesAccessors: {
split2: {
format: { id: 'string' },
formatter: {
convert: (x) => x,
} as FieldFormat,
},
},
} as unknown)) as FormatFactory;
},
} as unknown as LayersFieldFormats;

const formattedDatatables = {
first: {
table: tables['1'],
formattedColumns: {},
},
second: {
table: tables['2'],
formattedColumns: {},
},
};

describe('totalSeriesCount', () => {
it('should calculate total number of series per palette', () => {
const assignments = getColorAssignments(layers, formatFactory, {
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
const assignments = getColorAssignments(
layers,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
},
});
fieldFormats,
formattedDatatables
);
// two y accessors, with 3 splitted series
expect(assignments.palette1.totalSeriesCount).toEqual(2 * 3);
expect(assignments.palette2.totalSeriesCount).toEqual(2 * 3);
Expand All @@ -108,15 +141,16 @@ describe('color_assignment', () => {
it('should calculate total number of series spanning multible layers', () => {
const assignments = getColorAssignments(
[layers[0], { ...layers[1], palette: layers[0].palette }],
formatFactory,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
}
},
fieldFormats,
formattedDatatables
);
// two y accessors, with 3 splitted series, two times
expect(assignments.palette1.totalSeriesCount).toEqual(2 * 3 + 2 * 3);
Expand All @@ -126,15 +160,16 @@ describe('color_assignment', () => {
it('should calculate total number of series for non split series', () => {
const assignments = getColorAssignments(
[layers[0], { ...layers[1], palette: layers[0].palette, splitAccessors: undefined }],
formatFactory,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
}
},
fieldFormats,
formattedDatatables
);
// two y accessors, with 3 splitted series for the first layer, 2 non splitted y accessors for the second layer
expect(assignments.palette1.totalSeriesCount).toEqual(2 * 3 + 2);
Expand All @@ -151,37 +186,61 @@ describe('color_assignment', () => {
},
layers[1],
];
fieldFormats.first.splitSeriesAccessors.split1.formatter.convert = formatMock;
const newFormattedDatatables = {
first: {
formattedColumns: formattedDatatables.first.formattedColumns,
table: {
...formattedDatatables.first.table,
rows: [{ split1: complexObject }, { split1: 'abc' }],
},
},
second: formattedDatatables.second,
};

const assignments = getColorAssignments(
newLayers,
(() =>
({
convert: formatMock,
} as unknown)) as FormatFactory,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
}
},
fieldFormats,
newFormattedDatatables
);

fieldFormats.first.splitSeriesAccessors.split1.formatter.convert = (x) => x as string;
expect(formatMock).toHaveBeenCalledWith(complexObject);
expect(assignments.palette1.totalSeriesCount).toEqual(2 * 2);
expect(assignments.palette2.totalSeriesCount).toEqual(2 * 3);
expect(formatMock).toHaveBeenCalledWith(complexObject);
});

it('should handle missing columns', () => {
const newLayers = [{ ...layers[0], table: { ...tables['1'], columns: [] } }, layers[1]];
const assignments = getColorAssignments(newLayers, formatFactory, {
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
const newFormattedDatatables = {
first: {
formattedColumns: formattedDatatables.first.formattedColumns,
table: { ...formattedDatatables.first.table, columns: [] },
},
second: formattedDatatables.second,
};
const assignments = getColorAssignments(
newLayers,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
},
});
fieldFormats,
newFormattedDatatables
);

// if the split column is missing, just assume a single split
expect(assignments.palette1.totalSeriesCount).toEqual(2);
Expand All @@ -190,14 +249,19 @@ describe('color_assignment', () => {

describe('getRank', () => {
it('should return the correct rank for a series key', () => {
const assignments = getColorAssignments(layers, formatFactory, {
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
const assignments = getColorAssignments(
layers,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
},
});
fieldFormats,
formattedDatatables
);
// 3 series in front of 2/y2 - 1/y1, 1/y2 and 2/y1
expect(assignments.palette1.getRank(layers[0], '2 - test2')).toEqual(3);
// 1 series in front of 1/y4 - 1/y3
Expand All @@ -206,14 +270,19 @@ describe('color_assignment', () => {

it('should return the correct rank for a series key spanning multiple layers', () => {
const newLayers = [layers[0], { ...layers[1], palette: layers[0].palette }];
const assignments = getColorAssignments(newLayers, formatFactory, {
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
const assignments = getColorAssignments(
newLayers,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
},
});
fieldFormats,
formattedDatatables
);
// 3 series in front of 2/y2 - 1/y1, 1/y2 and 2/y1
expect(assignments.palette1.getRank(newLayers[0], '2 - test2')).toEqual(3);
// 2 series in front for the current layer (1/y3, 1/y4), plus all 6 series from the first layer
Expand All @@ -225,14 +294,19 @@ describe('color_assignment', () => {
layers[0],
{ ...layers[1], palette: layers[0].palette, splitAccessors: undefined },
];
const assignments = getColorAssignments(newLayers, formatFactory, {
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
const assignments = getColorAssignments(
newLayers,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
},
});
fieldFormats,
formattedDatatables
);
// 3 series in front of 2/y2 - 1/y1, 1/y2 and 2/y1
expect(assignments.palette1.getRank(newLayers[0], '2 - test2')).toEqual(3);
// 1 series in front for the current layer (y3), plus all 6 series from the first layer
Expand All @@ -247,37 +321,61 @@ describe('color_assignment', () => {
},
layers[1],
];
fieldFormats.first.splitSeriesAccessors.split1.formatter.convert = (value: unknown) =>
(typeof value === 'object' ? 'formatted' : value) as string;
const newFormattedDatatables = {
first: {
formattedColumns: formattedDatatables.first.formattedColumns,
table: {
...formattedDatatables.first.table,
rows: [{ split1: 'abc' }, { split1: { aProp: 123 } }],
},
},
second: formattedDatatables.second,
};

const assignments = getColorAssignments(
newLayers,
(() =>
({
convert: (value: unknown) => (typeof value === 'object' ? 'formatted' : value),
} as unknown)) as FormatFactory,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
}
},
fieldFormats,
newFormattedDatatables
);

fieldFormats.first.splitSeriesAccessors.split1.formatter.convert = (x) => x as string;
// 3 series in front of (complex object)/y1 - abc/y1, abc/y2
expect(assignments.palette1.getRank(layers[0], 'formatted - test1')).toEqual(2);
});

it('should handle missing columns', () => {
const newLayers = [{ ...layers[0], table: { ...tables['1'], columns: [] } }, layers[1]];
const newFormattedDatatables = {
first: {
formattedColumns: formattedDatatables.first.formattedColumns,
table: { ...formattedDatatables.first.table, columns: [] },
},
second: formattedDatatables.second,
};

const assignments = getColorAssignments(newLayers, formatFactory, {
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
const assignments = getColorAssignments(
newLayers,
{
yTitles: {
y1: 'test1',
y2: 'test2',
y3: 'test3',
y4: 'test4',
},
},
});
fieldFormats,
newFormattedDatatables
);

// if the split column is missing, assume it is the first splitted series. One series in front - 0/y1
expect(assignments.palette1.getRank(layers[0], 'test2')).toEqual(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function getSplitName(
fieldFormats: LayerFieldFormats
) {
return splitAccessors.reduce<string>((splitName, accessor) => {
if (!formattedDatatable.table.columns.length) return;
const splitAccessor = getAccessorByDimension(accessor, formattedDatatable.table.columns);
const splitFormatterObj = fieldFormats.splitSeriesAccessors[splitAccessor];
const name = formattedDatatable.formattedColumns[splitAccessor]
Expand Down

0 comments on commit cac1728

Please sign in to comment.