/
formatTime.test.ts
110 lines (96 loc) · 2.88 KB
/
formatTime.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { toDataFrame } from '../../dataframe/processDataFrame';
import { FieldType } from '../../types/dataFrame';
import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry';
import { applyFormatTime, formatTimeTransformer } from './formatTime';
describe('Format Time Transformer', () => {
beforeAll(() => {
mockTransformationsRegistry([formatTimeTransformer]);
});
it('will convert time to formatted string', () => {
const options = {
timeField: 'time',
outputFormat: 'YYYY-MM',
timezone: 'utc',
};
const frame = toDataFrame({
fields: [
{
name: 'time',
type: FieldType.time,
values: [1612939600000, 1689192000000, 1682025600000, 1690328089000, 1691011200000],
},
],
});
const newFrames = applyFormatTime(options, [frame]);
expect(newFrames[0].fields[0].values).toEqual(['2021-02', '2023-07', '2023-04', '2023-07', '2023-08']);
});
it('will match on getFieldDisplayName', () => {
const options = {
timeField: 'Created',
outputFormat: 'YYYY-MM',
timezone: 'utc',
};
const frame = toDataFrame({
fields: [
{
name: 'created',
type: FieldType.time,
values: [1612939600000, 1689192000000, 1682025600000, 1690328089000, 1691011200000],
config: {
displayName: 'Created',
},
},
],
});
const newFrames = applyFormatTime(options, [frame]);
expect(newFrames[0].fields[0].values).toEqual(['2021-02', '2023-07', '2023-04', '2023-07', '2023-08']);
});
it('will handle formats with times', () => {
const options = {
timeField: 'time',
outputFormat: 'YYYY-MM h:mm:ss a',
timezone: 'utc',
};
const frame = toDataFrame({
fields: [
{
name: 'time',
type: FieldType.time,
values: [1612939600000, 1689192000000, 1682025600000, 1690328089000, 1691011200000],
},
],
});
const newFrames = applyFormatTime(options, [frame]);
expect(newFrames[0].fields[0].values).toEqual([
'2021-02 6:46:40 am',
'2023-07 8:00:00 pm',
'2023-04 9:20:00 pm',
'2023-07 11:34:49 pm',
'2023-08 9:20:00 pm',
]);
});
it('will handle null times', () => {
const options = {
timeField: 'time',
outputFormat: 'YYYY-MM h:mm:ss a',
timezone: 'utc',
};
const frame = toDataFrame({
fields: [
{
name: 'time',
type: FieldType.time,
values: [1612939600000, 1689192000000, 1682025600000, 1690328089000, null],
},
],
});
const newFrames = applyFormatTime(options, [frame]);
expect(newFrames[0].fields[0].values).toEqual([
'2021-02 6:46:40 am',
'2023-07 8:00:00 pm',
'2023-04 9:20:00 pm',
'2023-07 11:34:49 pm',
'Invalid date',
]);
});
});