Files
superset2/superset-frontend/plugins/plugin-chart-echarts/test/utils/formatters.test.ts
Evan Rusackas 6cb9819d10 fix(echarts): Apply D3 locale formatting to adaptive datetime x-axis
Fixes #31790

When the "Adaptive" datetime format option was selected for x-axis in ECharts
visualizations, the custom D3 locale settings (like localized month names) were
being ignored. This was because getXAxisFormatter() was returning undefined for
SMART_DATE_ID, causing ECharts to use its default formatter instead of the
configured D3 locale formatter.

The fix ensures that when SMART_DATE_ID is selected, the smart date formatter
with proper locale support is returned, allowing custom D3 time formats with
localized month and day names to work correctly with the adaptive option.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-05 21:55:13 -08:00

84 lines
3.0 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 {
NumberFormats,
SMART_DATE_ID,
getTimeFormatter,
} from '@superset-ui/core';
import {
getPercentFormatter,
getXAxisFormatter,
getSmartDateFormatter,
} from '../../src/utils/formatters';
describe('getPercentFormatter', () => {
const value = 0.6;
it('should format as percent if no format is specified', () => {
expect(getPercentFormatter().format(value)).toEqual('60%');
});
it('should format as percent if SMART_NUMBER is specified', () => {
expect(
getPercentFormatter(NumberFormats.SMART_NUMBER).format(value),
).toEqual('60%');
});
it('should format using a provided format', () => {
expect(
getPercentFormatter(NumberFormats.PERCENT_2_POINT).format(value),
).toEqual('60.00%');
});
});
describe('getXAxisFormatter', () => {
it('should return smart date formatter when SMART_DATE_ID is specified', () => {
const formatter = getXAxisFormatter(SMART_DATE_ID);
expect(formatter).toBeDefined();
expect(formatter).toBeInstanceOf(Function);
// The formatter should be the same as getSmartDateFormatter()
const smartDateFormatter = getSmartDateFormatter();
expect(formatter).toEqual(smartDateFormatter);
});
it('should return undefined when format is not specified', () => {
const formatter = getXAxisFormatter();
expect(formatter).toBeUndefined();
});
it('should return undefined when format is empty string', () => {
const formatter = getXAxisFormatter('');
expect(formatter).toBeUndefined();
});
it('should return the appropriate time formatter for a specific format', () => {
const format = '%Y-%m-%d';
const formatter = getXAxisFormatter(format);
expect(formatter).toBeDefined();
expect(formatter).toBeInstanceOf(Function);
// Should return the same formatter as getTimeFormatter
const expectedFormatter = getTimeFormatter(format);
expect(formatter).toEqual(expectedFormatter);
});
it('should return String constructor as fallback', () => {
// This test verifies that the String constructor is returned
// Note: the current logic may not reach this case, but keeping for completeness
const formatter = getXAxisFormatter(null as any);
expect(formatter).toBeUndefined();
});
});