mirror of
https://github.com/apache/superset.git
synced 2026-04-20 16:44:46 +00:00
Use @superset-ui/number-format and @superset-ui/time-format for formatting. (#6470)
refactor: proxy all d3 number and time formatting calls
This commit is contained in:
committed by
GitHub
parent
cc3a625a4b
commit
fcec748b62
@@ -1,60 +1,10 @@
|
||||
import {
|
||||
tickMultiFormat,
|
||||
formatDate,
|
||||
formatDateVerbose,
|
||||
fDuration,
|
||||
now,
|
||||
epochTimeXHoursAgo,
|
||||
epochTimeXDaysAgo,
|
||||
epochTimeXYearsAgo,
|
||||
} from '../../../src/modules/dates';
|
||||
|
||||
describe('tickMultiFormat', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof tickMultiFormat).toBe('function');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDate', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof formatDate).toBe('function');
|
||||
});
|
||||
|
||||
it('shows only year when 1st day of the year', () => {
|
||||
expect(formatDate(new Date('2020-01-01'))).toBe('2020');
|
||||
});
|
||||
|
||||
it('shows only month when 1st of month', () => {
|
||||
expect(formatDate(new Date('2020-03-01'))).toBe('March');
|
||||
});
|
||||
|
||||
it('does not show day of week when it is Sunday', () => {
|
||||
expect(formatDate(new Date('2020-03-15'))).toBe('Mar 15');
|
||||
});
|
||||
|
||||
it('shows weekday when it is not Sunday (and no ms/sec/min/hr)', () => {
|
||||
expect(formatDate(new Date('2020-03-03'))).toBe('Tue 03');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDateVerbose', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof formatDateVerbose).toBe('function');
|
||||
});
|
||||
|
||||
it('shows only year when 1st day of the year', () => {
|
||||
expect(formatDateVerbose(new Date('2020-01-01'))).toBe('2020');
|
||||
});
|
||||
|
||||
it('shows month and year when 1st of month', () => {
|
||||
expect(formatDateVerbose(new Date('2020-03-01'))).toBe('Mar 2020');
|
||||
});
|
||||
|
||||
it('shows weekday when any day of the month', () => {
|
||||
expect(formatDateVerbose(new Date('2020-03-03'))).toBe('Tue Mar 3');
|
||||
expect(formatDateVerbose(new Date('2020-03-15'))).toBe('Sun Mar 15');
|
||||
});
|
||||
});
|
||||
} from '../../../src/modules/dates';
|
||||
|
||||
describe('fDuration', () => {
|
||||
it('is a function', () => {
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import {
|
||||
formatSelectOptionsForRange,
|
||||
d3format,
|
||||
d3FormatPreset,
|
||||
d3TimeFormatPreset,
|
||||
defaultNumberFormatter,
|
||||
mainMetric,
|
||||
roundDecimal,
|
||||
} from '../../../src/modules/utils';
|
||||
@@ -25,54 +21,6 @@ describe('utils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('d3format', () => {
|
||||
it('returns a string formatted number as specified', () => {
|
||||
expect(d3format('.3s', 1234)).toBe('1.23k');
|
||||
expect(d3format('.3s', 1237)).toBe('1.24k');
|
||||
expect(d3format('', 1237)).toBe('1.24k');
|
||||
expect(d3format('.2efd2.ef.2.e', 1237)).toBe('1237 (Invalid format: .2efd2.ef.2.e)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('d3FormatPreset', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof d3FormatPreset).toBe('function');
|
||||
});
|
||||
it('returns a working formatter', () => {
|
||||
expect(d3FormatPreset('.3s')(3000000)).toBe('3.00M');
|
||||
});
|
||||
});
|
||||
|
||||
describe('d3TimeFormatPreset', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof d3TimeFormatPreset).toBe('function');
|
||||
});
|
||||
it('returns a working formatter', () => {
|
||||
expect(d3FormatPreset('smart_date')(0)).toBe('1970');
|
||||
expect(d3FormatPreset('%%GIBBERISH')(0)).toBe('0 (Invalid format: %%GIBBERISH)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('defaultNumberFormatter', () => {
|
||||
expect(defaultNumberFormatter(10)).toBe('10');
|
||||
expect(defaultNumberFormatter(1)).toBe('1');
|
||||
expect(defaultNumberFormatter(1.0)).toBe('1');
|
||||
expect(defaultNumberFormatter(10.0)).toBe('10');
|
||||
expect(defaultNumberFormatter(10001)).toBe('10.0k');
|
||||
expect(defaultNumberFormatter(10100)).toBe('10.1k');
|
||||
expect(defaultNumberFormatter(111000000)).toBe('111M');
|
||||
expect(defaultNumberFormatter(0.23)).toBe('230m');
|
||||
|
||||
expect(defaultNumberFormatter(-10)).toBe('-10');
|
||||
expect(defaultNumberFormatter(-1)).toBe('-1');
|
||||
expect(defaultNumberFormatter(-1.0)).toBe('-1');
|
||||
expect(defaultNumberFormatter(-10.0)).toBe('-10');
|
||||
expect(defaultNumberFormatter(-10001)).toBe('-10.0k');
|
||||
expect(defaultNumberFormatter(-10101)).toBe('-10.1k');
|
||||
expect(defaultNumberFormatter(-111000000)).toBe('-111M');
|
||||
expect(defaultNumberFormatter(-0.23)).toBe('-230m');
|
||||
});
|
||||
|
||||
describe('mainMetric', () => {
|
||||
it('is null when no options', () => {
|
||||
expect(mainMetric([])).toBeUndefined();
|
||||
|
||||
Reference in New Issue
Block a user