Files
superset2/superset-frontend/plugins/plugin-chart-echarts/test/Radar/utils.test.ts
2026-07-30 10:28:40 -07:00

74 lines
2.5 KiB
TypeScript

/**
* 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.
*/
import { getNumberFormatter } from '@superset-ui/core';
import { renderNormalizedTooltip } from '../../src/Radar/utils';
describe('renderNormalizedTooltip', () => {
const mockGetDenormalizedValue = jest.fn((_, value) => Number(value));
const metrics = ['metric1', 'metric2'];
const params = {
color: 'red',
name: 'series1',
value: [100, 200],
};
const metricsWithCustomBounds = new Set<string>();
test('should render tooltip with formatted values when formatter is provided', () => {
const formatter = getNumberFormatter(',.2f');
const tooltip = renderNormalizedTooltip(
params,
metrics,
mockGetDenormalizedValue,
metricsWithCustomBounds,
formatter,
);
expect(tooltip).toContain(formatter(100));
expect(tooltip).toContain(formatter(200));
});
test('should render tooltip with raw values when formatter is not provided', () => {
const tooltip = renderNormalizedTooltip(
params,
metrics,
mockGetDenormalizedValue,
metricsWithCustomBounds,
);
expect(tooltip).toContain('100');
expect(tooltip).toContain('200');
});
test('should render a missing metric as N/A instead of NaN', () => {
// A missing metric is denormalized to `null` (see #30270); the tooltip
// must not run it through the formatter, which would print "NaN".
const getDenormalizedValueWithGap = jest.fn((_, value) =>
value === 'null' ? null : Number(value),
);
const formatter = getNumberFormatter(',.2f');
const tooltip = renderNormalizedTooltip(
{ ...params, value: [100, null] },
metrics,
getDenormalizedValueWithGap,
metricsWithCustomBounds,
formatter,
);
expect(tooltip).toContain('N/A');
expect(tooltip).not.toContain('NaN');
});
});