Update tests for smart date formatters

This commit is contained in:
Matheus Batista 2023-05-31 16:21:55 -03:00
parent 4884d71a4e
commit 06d0dcc6dc
3 changed files with 281 additions and 54 deletions

View File

@ -17,26 +17,98 @@
* under the License.
*/
import { TimeFormatter, smartDateFormatter } from '@superset-ui/core';
import { TimeLocaleDefinition } from 'd3-time-format';
import { TimeFormatter, createSmartDateFormatter } from '@superset-ui/core';
describe('smartDateFormatter', () => {
it('is a function', () => {
expect(smartDateFormatter).toBeInstanceOf(TimeFormatter);
describe('createSmartDateFormatter', () => {
describe('when locale is default', () => {
const smartDateFormatter = createSmartDateFormatter();
it('is a function', () => {
expect(smartDateFormatter).toBeInstanceOf(TimeFormatter);
});
it('shows only year when 1st day of the year', () => {
expect(smartDateFormatter(new Date('2020-01-01'))).toBe('2020');
});
it('shows only month when 1st of month', () => {
expect(smartDateFormatter(new Date('2020-03-01'))).toBe('March');
});
it('does not show day of week when it is Sunday', () => {
expect(smartDateFormatter(new Date('2020-03-15'))).toBe('Mar 15');
});
it('shows weekday when it is not Sunday (and no ms/sec/min/hr)', () => {
expect(smartDateFormatter(new Date('2020-03-03'))).toBe('Tue 03');
});
});
describe('when different locale is not default', () => {
const locale: TimeLocaleDefinition = {
dateTime: '%A, %e de %B de %Y. %X',
date: '%d/%m/%Y',
time: '%H:%M:%S',
periods: ['AM', 'PM'],
days: [
'Domingo',
'Segunda',
'Terça',
'Quarta',
'Quinta',
'Sexta',
'Sábado',
],
shortDays: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
months: [
'Janeiro',
'Fevereiro',
'Março',
'Abril',
'Maio',
'Junho',
'Julho',
'Agosto',
'Setembro',
'Outubro',
'Novembro',
'Dezembro',
],
shortMonths: [
'Jan',
'Fev',
'Mar',
'Abr',
'Mai',
'Jun',
'Jul',
'Ago',
'Set',
'Out',
'Nov',
'Dez',
],
};
const smartDateFormatter = createSmartDateFormatter(locale);
it('shows only year when 1st day of the year', () => {
expect(smartDateFormatter(new Date('2020-01-01'))).toBe('2020');
});
it('is a function', () => {
expect(smartDateFormatter).toBeInstanceOf(TimeFormatter);
});
it('shows only month when 1st of month', () => {
expect(smartDateFormatter(new Date('2020-03-01'))).toBe('March');
});
it('shows only year when 1st day of the year', () => {
expect(smartDateFormatter(new Date('2020-01-01'))).toBe('2020');
});
it('does not show day of week when it is Sunday', () => {
expect(smartDateFormatter(new Date('2020-03-15'))).toBe('Mar 15');
});
it('shows only month when 1st of month', () => {
expect(smartDateFormatter(new Date('2020-03-01'))).toBe('Março');
});
it('shows weekday when it is not Sunday (and no ms/sec/min/hr)', () => {
expect(smartDateFormatter(new Date('2020-03-03'))).toBe('Tue 03');
it('does not show day of week when it is Sunday', () => {
expect(smartDateFormatter(new Date('2023-10-15'))).toBe('Out 15');
});
it('shows weekday when it is not Sunday (and no ms/sec/min/hr)', () => {
expect(smartDateFormatter(new Date('2020-03-03'))).toBe('Ter 03');
});
});
});

View File

@ -17,40 +17,125 @@
* under the License.
*/
import { TimeFormatter, smartDateDetailedFormatter } from '@superset-ui/core';
import { TimeLocaleDefinition } from 'd3-time-format';
import {
TimeFormatter,
createSmartDateDetailedFormatter
} from '@superset-ui/core';
describe('smartDateDetailedFormatter', () => {
const formatter = smartDateDetailedFormatter;
describe('when locale is default', () => {
const formatter = createSmartDateDetailedFormatter();
it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
});
it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01T00:00:00.000+00:00'))).toBe('2020');
});
it('shows full date when a regular date', () => {
expect(formatter(new Date('2020-03-01T00:00:00.000+00:00'))).toBe(
'2020-03-01',
);
});
it('shows full date including time of day without seconds when hour precision', () => {
expect(formatter(new Date('2020-03-01T13:00:00.000+00:00'))).toBe(
'2020-03-01 13:00',
);
});
it('shows full date including time of day when minute precision', () => {
expect(formatter(new Date('2020-03-10T13:10:00.000+00:00'))).toBe(
'2020-03-10 13:10',
);
});
it('shows full date including time of day when subsecond precision', () => {
expect(formatter(new Date('2020-03-10T13:10:00.100+00:00'))).toBe(
'2020-03-10 13:10:00.100',
);
});
});
describe('when locale is specified', () => {
const locale: TimeLocaleDefinition = {
dateTime: '%A, %e de %B de %Y. %X',
date: '%d/%m/%Y',
time: '%H:%M:%S',
periods: ['AM', 'PM'],
days: [
'Domingo',
'Segunda',
'Terça',
'Quarta',
'Quinta',
'Sexta',
'Sábado',
],
shortDays: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
months: [
'Janeiro',
'Fevereiro',
'Março',
'Abril',
'Maio',
'Junho',
'Julho',
'Agosto',
'Setembro',
'Outubro',
'Novembro',
'Dezembro',
],
shortMonths: [
'Jan',
'Fev',
'Mar',
'Abr',
'Mai',
'Jun',
'Jul',
'Ago',
'Set',
'Out',
'Nov',
'Dez',
],
};
const formatter = createSmartDateDetailedFormatter(locale);
it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01T00:00:00.000+00:00'))).toBe('2020');
});
it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
});
it('shows full date when a regular date', () => {
expect(formatter(new Date('2020-03-01T00:00:00.000+00:00'))).toBe(
'2020-03-01',
);
});
it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01T00:00:00.000+00:00'))).toBe('2020');
});
it('shows full date including time of day without seconds when hour precision', () => {
expect(formatter(new Date('2020-03-01T13:00:00.000+00:00'))).toBe(
'2020-03-01 13:00',
);
});
it('shows full date when a regular date', () => {
expect(formatter(new Date('2020-03-01T00:00:00.000+00:00'))).toBe(
'2020-03-01',
);
});
it('shows full date including time of day when minute precision', () => {
expect(formatter(new Date('2020-03-10T13:10:00.000+00:00'))).toBe(
'2020-03-10 13:10',
);
});
it('shows full date including time of day without seconds when hour precision', () => {
expect(formatter(new Date('2020-03-01T13:00:00.000+00:00'))).toBe(
'2020-03-01 13:00',
);
});
it('shows full date including time of day when subsecond precision', () => {
expect(formatter(new Date('2020-03-10T13:10:00.100+00:00'))).toBe(
'2020-03-10 13:10:00.100',
);
it('shows full date including time of day when minute precision', () => {
expect(formatter(new Date('2020-03-10T13:10:00.000+00:00'))).toBe(
'2020-03-10 13:10',
);
});
it('shows full date including time of day when subsecond precision', () => {
expect(formatter(new Date('2020-03-10T13:10:00.100+00:00'))).toBe(
'2020-03-10 13:10:00.100',
);
});
});
});

View File

@ -17,25 +17,95 @@
* under the License.
*/
import { TimeFormatter, smartDateVerboseFormatter } from '@superset-ui/core';
import { TimeLocaleDefinition } from 'd3-time-format';
import {
TimeFormatter,
createSmartDateVerboseFormatter
} from '@superset-ui/core';
describe('smartDateVerboseFormatter', () => {
const formatter = smartDateVerboseFormatter;
describe('when locale is default', () => {
const formatter = createSmartDateVerboseFormatter();
it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
});
it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01'))).toBe('2020');
});
it('shows month and year when 1st of month', () => {
expect(formatter(new Date('2020-03-01'))).toBe('Mar 2020');
});
it('shows weekday when any day of the month', () => {
expect(formatter(new Date('2020-03-03'))).toBe('Tue Mar 3');
expect(formatter(new Date('2020-03-15'))).toBe('Sun Mar 15');
});
});
describe('when locale is not default', () => {
const locale: TimeLocaleDefinition = {
dateTime: '%A, %e de %B de %Y. %X',
date: '%d/%m/%Y',
time: '%H:%M:%S',
periods: ['AM', 'PM'],
days: [
'Domingo',
'Segunda',
'Terça',
'Quarta',
'Quinta',
'Sexta',
'Sábado',
],
shortDays: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
months: [
'Janeiro',
'Fevereiro',
'Março',
'Abril',
'Maio',
'Junho',
'Julho',
'Agosto',
'Setembro',
'Outubro',
'Novembro',
'Dezembro',
],
shortMonths: [
'Jan',
'Fev',
'Mar',
'Abr',
'Mai',
'Jun',
'Jul',
'Ago',
'Set',
'Out',
'Nov',
'Dez',
],
};
const formatter = createSmartDateVerboseFormatter(locale);
it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01'))).toBe('2020');
});
it('is a function', () => {
expect(formatter).toBeInstanceOf(TimeFormatter);
});
it('shows month and year when 1st of month', () => {
expect(formatter(new Date('2020-03-01'))).toBe('Mar 2020');
});
it('shows only year when 1st day of the year', () => {
expect(formatter(new Date('2020-01-01'))).toBe('2020');
});
it('shows weekday when any day of the month', () => {
expect(formatter(new Date('2020-03-03'))).toBe('Tue Mar 3');
expect(formatter(new Date('2020-03-15'))).toBe('Sun Mar 15');
it('shows month and year when 1st of month', () => {
expect(formatter(new Date('2020-04-01'))).toBe('Abr 2020');
});
it('shows weekday when any day of the month', () => {
expect(formatter(new Date('2020-03-03'))).toBe('Ter Mar 3');
expect(formatter(new Date('2020-03-15'))).toBe('Dom Mar 15');
});
});
});