2021-10-28 06:27:35 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-01-07 20:16:24 -05:00
|
|
|
import { TimeFormatter, PREVIEW_TIME } from '@superset-ui/core';
|
2018-11-29 16:01:57 -05:00
|
|
|
|
|
|
|
describe('TimeFormatter', () => {
|
|
|
|
describe('new TimeFormatter(config)', () => {
|
|
|
|
it('requires config.id', () => {
|
2019-01-25 16:28:26 -05:00
|
|
|
expect(
|
|
|
|
() =>
|
|
|
|
// @ts-ignore -- intentionally pass invalid input
|
|
|
|
new TimeFormatter({
|
|
|
|
formatFunc: () => 'test',
|
|
|
|
}),
|
|
|
|
).toThrow();
|
2018-11-29 16:01:57 -05:00
|
|
|
});
|
|
|
|
it('requires config.formatFunc', () => {
|
|
|
|
expect(
|
|
|
|
() =>
|
2019-01-25 16:28:26 -05:00
|
|
|
// @ts-ignore -- intentionally pass invalid input
|
2018-11-29 16:01:57 -05:00
|
|
|
new TimeFormatter({
|
|
|
|
id: 'my_format',
|
|
|
|
}),
|
|
|
|
).toThrow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('formatter is also a format function itself', () => {
|
|
|
|
const formatter = new TimeFormatter({
|
|
|
|
id: 'year_only',
|
2019-01-25 16:28:26 -05:00
|
|
|
formatFunc: (value: Date) => `${value.getFullYear()}`,
|
2018-11-29 16:01:57 -05:00
|
|
|
});
|
|
|
|
it('returns formatted value', () => {
|
|
|
|
expect(formatter(PREVIEW_TIME)).toEqual('2017');
|
|
|
|
});
|
|
|
|
it('formatter(value) is the same with formatter.format(value)', () => {
|
|
|
|
const value = PREVIEW_TIME;
|
|
|
|
expect(formatter(value)).toEqual(formatter.format(value));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('.format(value)', () => {
|
|
|
|
const formatter = new TimeFormatter({
|
|
|
|
id: 'year_only',
|
|
|
|
formatFunc: value => `${value.getFullYear()}`,
|
|
|
|
});
|
|
|
|
it('handles null', () => {
|
2019-01-25 16:28:26 -05:00
|
|
|
expect(formatter.format(null)).toEqual('null');
|
2018-11-29 16:01:57 -05:00
|
|
|
});
|
|
|
|
it('handles undefined', () => {
|
2019-01-25 16:28:26 -05:00
|
|
|
expect(formatter.format(undefined)).toEqual('undefined');
|
2018-11-29 16:01:57 -05:00
|
|
|
});
|
2020-04-13 17:48:27 -04:00
|
|
|
it('handles number, treating it as a timestamp', () => {
|
|
|
|
expect(formatter.format(PREVIEW_TIME.getTime())).toEqual('2017');
|
|
|
|
});
|
2018-11-29 16:01:57 -05:00
|
|
|
it('otherwise returns formatted value', () => {
|
|
|
|
expect(formatter.format(PREVIEW_TIME)).toEqual('2017');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('.preview(value)', () => {
|
|
|
|
const formatter = new TimeFormatter({
|
|
|
|
id: 'year_only',
|
|
|
|
formatFunc: value => `${value.getFullYear()}`,
|
|
|
|
});
|
|
|
|
it('returns string comparing value before and after formatting', () => {
|
|
|
|
const time = new Date(Date.UTC(2018, 10, 21, 22, 11, 44));
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(formatter.preview(time)).toEqual(
|
|
|
|
'Wed, 21 Nov 2018 22:11:44 GMT => 2018',
|
|
|
|
);
|
2018-11-29 16:01:57 -05:00
|
|
|
});
|
|
|
|
it('uses the default preview value if not specified', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(formatter.preview()).toEqual(
|
|
|
|
'Tue, 14 Feb 2017 11:22:33 GMT => 2017',
|
|
|
|
);
|
2018-11-29 16:01:57 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|