mirror of
https://github.com/apache/superset.git
synced 2026-07-09 00:05:36 +00:00
Co-authored-by: Superset Dev <dev@superset.apache.org> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
246 lines
7.3 KiB
TypeScript
246 lines
7.3 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 { CurrencyFormatter, getNumberFormatter } from '@superset-ui/core';
|
|
import { GenericDataType } from '@apache-superset/core/common';
|
|
import { formatColumnValue } from '../../src/utils/formatValue';
|
|
import { DataColumnMeta } from '../../src/types';
|
|
|
|
test('formatColumnValue with CurrencyFormatter AUTO mode uses row context', () => {
|
|
const formatter = new CurrencyFormatter({
|
|
d3Format: ',.2f',
|
|
currency: { symbol: 'AUTO', symbolPosition: 'prefix' },
|
|
});
|
|
|
|
const column: DataColumnMeta = {
|
|
key: 'revenue',
|
|
label: 'Revenue',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
currencyCodeColumn: 'currency_code',
|
|
};
|
|
|
|
const rowData = { revenue: 1000, currency_code: 'EUR' };
|
|
const [isHtml, result] = formatColumnValue(column, 1000, rowData);
|
|
|
|
expect(isHtml).toBe(false);
|
|
expect(result).toContain('€');
|
|
expect(result).toContain('1,000.00');
|
|
});
|
|
|
|
test('formatColumnValue with CurrencyFormatter AUTO mode returns neutral format without row context', () => {
|
|
const formatter = new CurrencyFormatter({
|
|
d3Format: ',.2f',
|
|
currency: { symbol: 'AUTO', symbolPosition: 'prefix' },
|
|
});
|
|
|
|
const column: DataColumnMeta = {
|
|
key: 'revenue',
|
|
label: 'Revenue',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
currencyCodeColumn: 'currency_code',
|
|
};
|
|
|
|
// No row data provided
|
|
const [isHtml, result] = formatColumnValue(column, 1000);
|
|
|
|
expect(isHtml).toBe(false);
|
|
expect(result).toBe('1,000.00');
|
|
expect(result).not.toContain('$');
|
|
expect(result).not.toContain('€');
|
|
});
|
|
|
|
test('formatColumnValue with static CurrencyFormatter ignores row context', () => {
|
|
const formatter = new CurrencyFormatter({
|
|
d3Format: ',.2f',
|
|
currency: { symbol: 'USD', symbolPosition: 'prefix' },
|
|
});
|
|
|
|
const column: DataColumnMeta = {
|
|
key: 'revenue',
|
|
label: 'Revenue',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
};
|
|
|
|
// Row has EUR but static mode should show $
|
|
const rowData = { revenue: 1000, currency_code: 'EUR' };
|
|
const [isHtml, result] = formatColumnValue(column, 1000, rowData);
|
|
|
|
expect(isHtml).toBe(false);
|
|
expect(result).toContain('$');
|
|
expect(result).not.toContain('€');
|
|
});
|
|
|
|
test('formatColumnValue with AUTO mode normalizes currency codes', () => {
|
|
const formatter = new CurrencyFormatter({
|
|
d3Format: ',.2f',
|
|
currency: { symbol: 'AUTO', symbolPosition: 'prefix' },
|
|
});
|
|
|
|
const column: DataColumnMeta = {
|
|
key: 'revenue',
|
|
label: 'Revenue',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
currencyCodeColumn: 'currency_code',
|
|
};
|
|
|
|
// Test lowercase currency code
|
|
const rowData1 = { revenue: 500, currency_code: 'usd' };
|
|
const [, result1] = formatColumnValue(column, 500, rowData1);
|
|
expect(result1).toContain('$');
|
|
|
|
// Test uppercase currency code (GBP -> £)
|
|
const rowData2 = { revenue: 750, currency_code: 'GBP' };
|
|
const [, result2] = formatColumnValue(column, 750, rowData2);
|
|
expect(result2).toContain('£');
|
|
});
|
|
|
|
test('formatColumnValue handles null values', () => {
|
|
const column: DataColumnMeta = {
|
|
key: 'revenue',
|
|
label: 'Revenue',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter: getNumberFormatter(',.2f'),
|
|
isNumeric: true,
|
|
};
|
|
|
|
const [, nullResult] = formatColumnValue(column, null);
|
|
expect(nullResult).toBe('N/A');
|
|
});
|
|
|
|
test('formatColumnValue preserves percentage format for small numbers when d3SmallNumberFormat is null', () => {
|
|
const formatter = getNumberFormatter('.8%');
|
|
const column: DataColumnMeta = {
|
|
key: 'pct',
|
|
label: 'Percentage',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: { d3SmallNumberFormat: null },
|
|
};
|
|
|
|
const [, result] = formatColumnValue(column, -0.00001229);
|
|
expect(result).toBe('-0.00122900%');
|
|
});
|
|
|
|
test('formatColumnValue preserves percentage format for small numbers when d3SmallNumberFormat is empty string', () => {
|
|
const formatter = getNumberFormatter('.8%');
|
|
const column: DataColumnMeta = {
|
|
key: 'pct',
|
|
label: 'Percentage',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: { d3SmallNumberFormat: '' },
|
|
};
|
|
|
|
const [, result] = formatColumnValue(column, -0.00001229);
|
|
expect(result).toBe('-0.00122900%');
|
|
});
|
|
|
|
test('formatColumnValue preserves percentage format for small numbers when config has no d3SmallNumberFormat', () => {
|
|
const formatter = getNumberFormatter('.8%');
|
|
const column: DataColumnMeta = {
|
|
key: 'pct',
|
|
label: 'Percentage',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: {},
|
|
};
|
|
|
|
const [, result] = formatColumnValue(column, -0.00001229);
|
|
expect(result).toBe('-0.00122900%');
|
|
});
|
|
|
|
test('formatColumnValue uses default formatter for value exactly 1 (boundary)', () => {
|
|
const formatter = getNumberFormatter(',.2f');
|
|
const column: DataColumnMeta = {
|
|
key: 'val',
|
|
label: 'Value',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: { d3SmallNumberFormat: null },
|
|
};
|
|
|
|
const [, result] = formatColumnValue(column, 1);
|
|
expect(result).toBe('1.00');
|
|
});
|
|
|
|
test('formatColumnValue uses default formatter for value exactly -1 (boundary)', () => {
|
|
const formatter = getNumberFormatter(',.2f');
|
|
const column: DataColumnMeta = {
|
|
key: 'val',
|
|
label: 'Value',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: { d3SmallNumberFormat: null },
|
|
};
|
|
|
|
const [, result] = formatColumnValue(column, -1);
|
|
expect(result).toBe('-1.00');
|
|
});
|
|
|
|
test('formatColumnValue uses small number formatter for value 0', () => {
|
|
const formatter = getNumberFormatter('.8%');
|
|
const column: DataColumnMeta = {
|
|
key: 'pct',
|
|
label: 'Percentage',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: { d3SmallNumberFormat: null },
|
|
};
|
|
|
|
const [, result] = formatColumnValue(column, 0);
|
|
expect(result).toBe('0.00000000%');
|
|
});
|
|
|
|
test('formatColumnValue with small number format and currency', () => {
|
|
const formatter = new CurrencyFormatter({
|
|
d3Format: ',.2f',
|
|
currency: { symbol: 'EUR', symbolPosition: 'prefix' },
|
|
});
|
|
|
|
const column: DataColumnMeta = {
|
|
key: 'revenue',
|
|
label: 'Revenue',
|
|
dataType: GenericDataType.Numeric,
|
|
formatter,
|
|
isNumeric: true,
|
|
config: {
|
|
d3SmallNumberFormat: ',.4f',
|
|
currencyFormat: { symbol: 'EUR', symbolPosition: 'prefix' },
|
|
},
|
|
};
|
|
|
|
// Small number should use small number format
|
|
const [, result] = formatColumnValue(column, 0.5);
|
|
expect(result).toContain('€');
|
|
expect(result).toContain('0.5000');
|
|
});
|