2020-04-01 21:53:41 -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.
|
|
|
|
*/
|
2022-05-04 08:55:22 -04:00
|
|
|
import {
|
|
|
|
DatasourceType,
|
|
|
|
supersetTheme,
|
|
|
|
TimeGranularity,
|
|
|
|
} from '@superset-ui/core';
|
2021-12-15 04:15:14 -05:00
|
|
|
import transformProps from '../../src/BigNumber/BigNumberWithTrendline/transformProps';
|
|
|
|
import {
|
2020-04-20 21:03:10 -04:00
|
|
|
BigNumberDatum,
|
2021-12-15 04:15:14 -05:00
|
|
|
BigNumberWithTrendlineChartProps,
|
|
|
|
} from '../../src/BigNumber/types';
|
2020-04-01 21:53:41 -04:00
|
|
|
|
|
|
|
const formData = {
|
|
|
|
metric: 'value',
|
|
|
|
colorPicker: {
|
|
|
|
r: 0,
|
|
|
|
g: 122,
|
|
|
|
b: 135,
|
|
|
|
a: 1,
|
|
|
|
},
|
|
|
|
compareLag: 1,
|
2021-10-12 22:19:12 -04:00
|
|
|
timeGrainSqla: 'P3M' as TimeGranularity,
|
2020-04-01 21:53:41 -04:00
|
|
|
compareSuffix: 'over last quarter',
|
2021-12-15 04:15:14 -05:00
|
|
|
viz_type: 'big_number',
|
2020-04-01 21:53:41 -04:00
|
|
|
yAxisFormat: '.3s',
|
2021-12-15 04:15:14 -05:00
|
|
|
datasource: 'test_datasource',
|
2020-04-01 21:53:41 -04:00
|
|
|
};
|
|
|
|
|
2021-05-12 06:16:03 -04:00
|
|
|
const rawFormData = {
|
|
|
|
metric: 'value',
|
|
|
|
color_picker: {
|
|
|
|
r: 0,
|
|
|
|
g: 122,
|
|
|
|
b: 135,
|
|
|
|
a: 1,
|
|
|
|
},
|
|
|
|
compare_lag: 1,
|
2021-10-12 22:19:12 -04:00
|
|
|
time_grain_sqla: 'P3M' as TimeGranularity,
|
2021-05-12 06:16:03 -04:00
|
|
|
compare_suffix: 'over last quarter',
|
|
|
|
viz_type: 'big_number',
|
|
|
|
y_axis_format: '.3s',
|
|
|
|
};
|
|
|
|
|
2020-04-20 21:03:10 -04:00
|
|
|
function generateProps(
|
|
|
|
data: BigNumberDatum[],
|
|
|
|
extraFormData = {},
|
2022-03-27 22:20:06 -04:00
|
|
|
extraQueryData: any = {},
|
2021-12-15 04:15:14 -05:00
|
|
|
): BigNumberWithTrendlineChartProps {
|
2020-04-01 21:53:41 -04:00
|
|
|
return {
|
|
|
|
width: 200,
|
|
|
|
height: 500,
|
|
|
|
annotationData: {},
|
|
|
|
datasource: {
|
2020-04-20 21:03:10 -04:00
|
|
|
id: 0,
|
|
|
|
name: '',
|
|
|
|
type: DatasourceType.Table,
|
|
|
|
columns: [],
|
|
|
|
metrics: [],
|
2020-04-01 21:53:41 -04:00
|
|
|
columnFormats: {},
|
|
|
|
verboseMap: {},
|
|
|
|
},
|
|
|
|
rawDatasource: {},
|
2021-05-12 06:16:03 -04:00
|
|
|
rawFormData,
|
2020-04-01 21:53:41 -04:00
|
|
|
hooks: {},
|
|
|
|
initialValues: {},
|
|
|
|
formData: {
|
|
|
|
...formData,
|
|
|
|
...extraFormData,
|
|
|
|
},
|
2020-12-29 05:10:39 -05:00
|
|
|
queriesData: [
|
|
|
|
{
|
|
|
|
data,
|
|
|
|
...extraQueryData,
|
|
|
|
},
|
|
|
|
],
|
2021-12-15 04:15:14 -05:00
|
|
|
ownState: {},
|
|
|
|
filterState: {},
|
|
|
|
behaviors: [],
|
2022-05-04 08:55:22 -04:00
|
|
|
theme: supersetTheme,
|
2020-04-01 21:53:41 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-15 04:15:14 -05:00
|
|
|
describe('BigNumberWithTrendline', () => {
|
2020-04-01 21:53:41 -04:00
|
|
|
const props = generateProps(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
__timestamp: 0,
|
|
|
|
value: 1.2345,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__timestamp: 100,
|
|
|
|
value: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ showTrendLine: true },
|
|
|
|
);
|
|
|
|
|
|
|
|
describe('transformProps()', () => {
|
|
|
|
it('should fallback and format time', () => {
|
|
|
|
const transformed = transformProps(props);
|
|
|
|
// the first item is the last item sorted by __timestamp
|
|
|
|
const lastDatum = transformed.trendLineData?.pop();
|
|
|
|
|
|
|
|
// should use last available value
|
2021-12-15 04:15:14 -05:00
|
|
|
expect(lastDatum?.[0]).toStrictEqual(100);
|
|
|
|
expect(lastDatum?.[1]).toBeNull();
|
2020-04-01 21:53:41 -04:00
|
|
|
|
|
|
|
// should note this is a fallback
|
|
|
|
expect(transformed.bigNumber).toStrictEqual(1.2345);
|
|
|
|
expect(transformed.bigNumberFallback).not.toBeNull();
|
|
|
|
|
|
|
|
// should successfully formatTime by ganularity
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(transformed.formatTime(new Date('2020-01-01'))).toStrictEqual(
|
|
|
|
'2020-01-01 00:00:00',
|
|
|
|
);
|
2020-04-01 21:53:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should respect datasource d3 format', () => {
|
|
|
|
const propsWithDatasource = {
|
|
|
|
...props,
|
|
|
|
datasource: {
|
2020-04-20 21:03:10 -04:00
|
|
|
...props.datasource,
|
2020-04-01 21:53:41 -04:00
|
|
|
metrics: [
|
|
|
|
{
|
2020-04-20 21:03:10 -04:00
|
|
|
label: 'value',
|
2020-04-01 21:53:41 -04:00
|
|
|
metric_name: 'value',
|
|
|
|
d3format: '.2f',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const transformed = transformProps(propsWithDatasource);
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(transformed.headerFormatter(transformed.bigNumber)).toStrictEqual(
|
|
|
|
'1.23',
|
|
|
|
);
|
2020-04-01 21:53:41 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|