Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmud committed May 13, 2020
1 parent 0cc5288 commit 8439215
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/superset-ui-time-format/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export type TimeGranularity =
| 'PT1H'
| 'P1D'
| 'P1W'
| 'P1M'
| 'P0.25Y'
| 'P1Y'
| '1969-12-28T00:00:00Z/P1W'
| '1969-12-29T00:00:00Z/P1W'
| 'P1W/1970-01-03T00:00:00Z';
| 'P1W/1970-01-03T00:00:00Z'
| 'P1W/1970-01-04T00:00:00Z';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import getFormatter from '../../src/factories/getTimeFormatterForGranularity';
import smartDateVerbose from '../../src/formatters/smartDateVerbose';

describe('getTimeFormatterForGranularity()', () => {
it('use smartDate when granularity unknown or undefined', () => {
expect(getFormatter(undefined)).toBe(smartDateVerbose);
// @ts-ignore
expect(getFormatter('random-string')).toBe(smartDateVerbose);
});

it('format time for known granularities', () => {
// JS Date constructor month is zero-based
const date = new Date(2020, 4, 10, 11, 10, 1); // May 10, 2020 is Sunday
expect(getFormatter('date')(date)).toBe('2020-05-10');
expect(getFormatter('PT1S')(date)).toBe('2020-05-10 11:10:01');
expect(getFormatter('PT1M')(date)).toBe('2020-05-10 11:10');
expect(getFormatter('PT5M')(date)).toBe('2020-05-10 11:10');
expect(getFormatter('PT10M')(date)).toBe('2020-05-10 11:10');
expect(getFormatter('PT15M')(date)).toBe('2020-05-10 11:10');
expect(getFormatter('PT0.5H')(date)).toBe('2020-05-10 11:10');
expect(getFormatter('PT1H')(date)).toBe('2020-05-10 11:00');
expect(getFormatter('P1D')(date)).toBe('2020-05-10');
expect(getFormatter('P1W')(date)).toBe('2020-05-10 W19');
expect(getFormatter('P1M')(date)).toBe('2020-05');
expect(getFormatter('P0.25Y')(date)).toBe('2020 Q2');
expect(getFormatter('P1Y')(date)).toBe('2020');
// sunday based week
expect(getFormatter('1969-12-28T00:00:00Z/P1W')(date)).toBe('2020-05-10 W19');
expect(getFormatter('P1W/1970-01-03T00:00:00Z')(date)).toBe('2020-05-10 W19');
// monday based week
expect(getFormatter('1969-12-29T00:00:00Z/P1W')(date)).toBe('2020-05-10 W18');
expect(getFormatter('P1W/1970-01-04T00:00:00Z')(date)).toBe('2020-05-10 W18');
});
});

0 comments on commit 8439215

Please sign in to comment.