2020-09-01 02:00:51 -04:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2020-09-18 15:28:18 -04:00
|
|
|
import { getNumberFormatter, getTimeFormatter } from '@superset-ui/core';
|
|
|
|
import {
|
2021-04-09 02:55:03 -04:00
|
|
|
dedupSeries,
|
2020-09-18 15:28:18 -04:00
|
|
|
extractGroupbyLabel,
|
|
|
|
extractTimeseriesSeries,
|
|
|
|
formatSeriesName,
|
2021-01-20 13:15:55 -05:00
|
|
|
getChartPadding,
|
|
|
|
getLegendProps,
|
2021-07-02 07:45:40 -04:00
|
|
|
sanitizeHtml,
|
2020-09-18 15:28:18 -04:00
|
|
|
} from '../../src/utils/series';
|
2021-01-20 13:15:55 -05:00
|
|
|
import { LegendOrientation, LegendType } from '../../src/types';
|
|
|
|
import { defaultLegendPadding } from '../../src/defaults';
|
2021-11-09 05:34:07 -05:00
|
|
|
import { NULL_STRING } from '../../src/constants';
|
2020-09-01 02:00:51 -04:00
|
|
|
|
|
|
|
describe('extractTimeseriesSeries', () => {
|
|
|
|
it('should generate a valid ECharts timeseries series object', () => {
|
|
|
|
const data = [
|
|
|
|
{
|
|
|
|
__timestamp: '2000-01-01',
|
|
|
|
Hulk: null,
|
|
|
|
abc: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-02-01',
|
|
|
|
Hulk: 2,
|
|
|
|
abc: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-03-01',
|
|
|
|
Hulk: 1,
|
|
|
|
abc: 5,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
expect(extractTimeseriesSeries(data)).toEqual([
|
|
|
|
{
|
2020-11-10 13:45:42 -05:00
|
|
|
id: 'Hulk',
|
2020-09-01 02:00:51 -04:00
|
|
|
name: 'Hulk',
|
|
|
|
data: [
|
|
|
|
[new Date('2000-01-01'), null],
|
|
|
|
[new Date('2000-02-01'), 2],
|
|
|
|
[new Date('2000-03-01'), 1],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2020-11-10 13:45:42 -05:00
|
|
|
id: 'abc',
|
2020-09-01 02:00:51 -04:00
|
|
|
name: 'abc',
|
|
|
|
data: [
|
|
|
|
[new Date('2000-01-01'), 2],
|
|
|
|
[new Date('2000-02-01'), 10],
|
|
|
|
[new Date('2000-03-01'), 5],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2021-03-19 12:42:55 -04:00
|
|
|
|
|
|
|
it('should do missing value imputation', () => {
|
|
|
|
const data = [
|
|
|
|
{
|
|
|
|
__timestamp: '2000-01-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-02-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-03-01',
|
|
|
|
abc: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-04-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-05-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-06-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-07-01',
|
|
|
|
abc: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-08-01',
|
|
|
|
abc: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-09-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: '2000-10-01',
|
|
|
|
abc: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
expect(extractTimeseriesSeries(data, { fillNeighborValue: 0 })).toEqual([
|
|
|
|
{
|
|
|
|
id: 'abc',
|
|
|
|
name: 'abc',
|
|
|
|
data: [
|
|
|
|
[new Date('2000-01-01'), null],
|
|
|
|
[new Date('2000-02-01'), 0],
|
|
|
|
[new Date('2000-03-01'), 1],
|
|
|
|
[new Date('2000-04-01'), 0],
|
|
|
|
[new Date('2000-05-01'), null],
|
|
|
|
[new Date('2000-06-01'), 0],
|
|
|
|
[new Date('2000-07-01'), 2],
|
|
|
|
[new Date('2000-08-01'), 3],
|
|
|
|
[new Date('2000-09-01'), 0],
|
|
|
|
[new Date('2000-10-01'), null],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2020-09-01 02:00:51 -04:00
|
|
|
});
|
2020-09-10 17:56:38 -04:00
|
|
|
|
|
|
|
describe('extractGroupbyLabel', () => {
|
|
|
|
it('should join together multiple groupby labels', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
extractGroupbyLabel({
|
|
|
|
datum: { a: 'abc', b: 'qwerty' },
|
|
|
|
groupby: ['a', 'b'],
|
|
|
|
}),
|
|
|
|
).toEqual('abc, qwerty');
|
2020-09-10 17:56:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle a single groupby', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
extractGroupbyLabel({ datum: { xyz: 'qqq' }, groupby: ['xyz'] }),
|
|
|
|
).toEqual('qqq');
|
2020-09-10 17:56:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle mixed types', () => {
|
|
|
|
expect(
|
2020-09-18 15:28:18 -04:00
|
|
|
extractGroupbyLabel({
|
|
|
|
datum: { strcol: 'abc', intcol: 123, floatcol: 0.123, boolcol: true },
|
|
|
|
groupby: ['strcol', 'intcol', 'floatcol', 'boolcol'],
|
|
|
|
}),
|
|
|
|
).toEqual('abc, 123, 0.123, true');
|
|
|
|
});
|
2020-10-31 14:41:15 -04:00
|
|
|
|
|
|
|
it('should handle null and undefined groupby', () => {
|
|
|
|
expect(
|
|
|
|
extractGroupbyLabel({
|
|
|
|
datum: { strcol: 'abc', intcol: 123, floatcol: 0.123, boolcol: true },
|
|
|
|
groupby: null,
|
|
|
|
}),
|
|
|
|
).toEqual('');
|
|
|
|
expect(extractGroupbyLabel({})).toEqual('');
|
|
|
|
});
|
2020-09-18 15:28:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('formatSeriesName', () => {
|
|
|
|
const numberFormatter = getNumberFormatter();
|
|
|
|
const timeFormatter = getTimeFormatter();
|
|
|
|
it('should handle missing values properly', () => {
|
|
|
|
expect(formatSeriesName(undefined)).toEqual('<NULL>');
|
|
|
|
expect(formatSeriesName(null)).toEqual('<NULL>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle string values properly', () => {
|
|
|
|
expect(formatSeriesName('abc XYZ!')).toEqual('abc XYZ!');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle boolean values properly', () => {
|
|
|
|
expect(formatSeriesName(true)).toEqual('true');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use default formatting for numeric values without formatter', () => {
|
|
|
|
expect(formatSeriesName(12345678.9)).toEqual('12345678.9');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use numberFormatter for numeric values when formatter is provided', () => {
|
|
|
|
expect(formatSeriesName(12345678.9, { numberFormatter })).toEqual('12.3M');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use default formatting for for date values without formatter', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(formatSeriesName(new Date('2020-09-11'))).toEqual(
|
|
|
|
'2020-09-11T00:00:00.000Z',
|
|
|
|
);
|
2020-09-18 15:28:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should use timeFormatter for date values when formatter is provided', () => {
|
|
|
|
expect(formatSeriesName(new Date('2020-09-11'), { timeFormatter })).toEqual(
|
|
|
|
'2020-09-11 00:00:00',
|
|
|
|
);
|
2020-09-10 17:56:38 -04:00
|
|
|
});
|
2021-01-20 13:15:55 -05:00
|
|
|
|
|
|
|
describe('getLegendProps', () => {
|
2021-02-16 04:47:02 -05:00
|
|
|
it('should return the correct props for scroll type with top orientation without zoom', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
getLegendProps(LegendType.Scroll, LegendOrientation.Top, true, false),
|
|
|
|
).toEqual({
|
2021-01-20 13:15:55 -05:00
|
|
|
show: true,
|
|
|
|
top: 0,
|
2021-02-16 04:47:02 -05:00
|
|
|
right: 0,
|
|
|
|
orient: 'horizontal',
|
|
|
|
type: 'scroll',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct props for scroll type with top orientation with zoom', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
getLegendProps(LegendType.Scroll, LegendOrientation.Top, true, true),
|
|
|
|
).toEqual({
|
2021-02-16 04:47:02 -05:00
|
|
|
show: true,
|
|
|
|
top: 0,
|
|
|
|
right: 55,
|
2021-01-20 13:15:55 -05:00
|
|
|
orient: 'horizontal',
|
|
|
|
type: 'scroll',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct props for plain type with left orientation', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
getLegendProps(LegendType.Plain, LegendOrientation.Left, true),
|
|
|
|
).toEqual({
|
2021-01-20 13:15:55 -05:00
|
|
|
show: true,
|
|
|
|
left: 0,
|
|
|
|
orient: 'vertical',
|
|
|
|
type: 'plain',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-16 04:47:02 -05:00
|
|
|
it('should return the correct props for plain type with right orientation without zoom', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
getLegendProps(LegendType.Plain, LegendOrientation.Right, false, false),
|
|
|
|
).toEqual({
|
2021-02-16 04:47:02 -05:00
|
|
|
show: false,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
orient: 'vertical',
|
|
|
|
type: 'plain',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct props for plain type with right orientation with zoom', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
getLegendProps(LegendType.Plain, LegendOrientation.Right, false, true),
|
|
|
|
).toEqual({
|
2021-01-20 13:15:55 -05:00
|
|
|
show: false,
|
|
|
|
right: 0,
|
2021-02-16 04:47:02 -05:00
|
|
|
top: 30,
|
2021-01-20 13:15:55 -05:00
|
|
|
orient: 'vertical',
|
|
|
|
type: 'plain',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct props for plain type with bottom orientation', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
getLegendProps(LegendType.Plain, LegendOrientation.Bottom, false),
|
|
|
|
).toEqual({
|
2021-01-20 13:15:55 -05:00
|
|
|
show: false,
|
|
|
|
bottom: 0,
|
|
|
|
orient: 'horizontal',
|
|
|
|
type: 'plain',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getChartPadding', () => {
|
|
|
|
it('should handle top default', () => {
|
|
|
|
expect(getChartPadding(true, LegendOrientation.Top)).toEqual({
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: defaultLegendPadding[LegendOrientation.Top],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle left default', () => {
|
|
|
|
expect(getChartPadding(true, LegendOrientation.Left)).toEqual({
|
|
|
|
bottom: 0,
|
|
|
|
left: defaultLegendPadding[LegendOrientation.Left],
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the default padding when show is false', () => {
|
|
|
|
expect(
|
|
|
|
getChartPadding(false, LegendOrientation.Left, 100, {
|
|
|
|
top: 10,
|
|
|
|
bottom: 20,
|
|
|
|
left: 30,
|
|
|
|
right: 40,
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
bottom: 20,
|
|
|
|
left: 30,
|
|
|
|
right: 40,
|
|
|
|
top: 10,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct padding for left orientation', () => {
|
|
|
|
expect(getChartPadding(true, LegendOrientation.Left, 100)).toEqual({
|
|
|
|
bottom: 0,
|
|
|
|
left: 100,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct padding for right orientation', () => {
|
|
|
|
expect(getChartPadding(true, LegendOrientation.Right, 50)).toEqual({
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 50,
|
|
|
|
top: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct padding for top orientation', () => {
|
|
|
|
expect(getChartPadding(true, LegendOrientation.Top, 20)).toEqual({
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 20,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the correct padding for bottom orientation', () => {
|
|
|
|
expect(getChartPadding(true, LegendOrientation.Bottom, 10)).toEqual({
|
|
|
|
bottom: 10,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-04-09 02:55:03 -04:00
|
|
|
|
|
|
|
describe('dedupSeries', () => {
|
|
|
|
it('should deduplicate ids in series', () => {
|
|
|
|
expect(
|
|
|
|
dedupSeries([
|
|
|
|
{
|
|
|
|
id: 'foo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'bar',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'foo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'foo',
|
|
|
|
},
|
|
|
|
]),
|
2021-11-09 07:42:28 -05:00
|
|
|
).toEqual([
|
|
|
|
{ id: 'foo' },
|
|
|
|
{ id: 'bar' },
|
|
|
|
{ id: 'foo (1)' },
|
|
|
|
{ id: 'foo (2)' },
|
|
|
|
]);
|
2021-04-09 02:55:03 -04:00
|
|
|
});
|
|
|
|
});
|
2021-07-02 07:45:40 -04:00
|
|
|
|
|
|
|
describe('sanitizeHtml', () => {
|
|
|
|
it('should remove html tags from series name', () => {
|
|
|
|
expect(sanitizeHtml(NULL_STRING)).toEqual('<NULL>');
|
|
|
|
});
|
|
|
|
});
|
2020-09-10 17:56:38 -04:00
|
|
|
});
|