-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlux.test.ts
261 lines (226 loc) · 9.25 KB
/
lux.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import type { LuxOptions } from '../src/types'
import { DateTime } from 'luxon'
import { describe, expect, it } from 'vitest'
import { createApp } from 'vue'
import plugin from '../src/index'
function initApp(options: LuxOptions = {}) {
const app = createApp({})
app.use(plugin, options)
return app
}
describe('plugin', () => {
it('should install the plugin', () => {
const app = initApp()
expect(app.config.globalProperties.$lux).toBeDefined()
expect(app.config.globalProperties.$lp).toBeDefined()
expect(app.config.globalProperties.$lf).toBeDefined()
})
})
describe('datetimes', () => {
it('format', () => {
const app = initApp({
output: {
zone: 'utc',
},
})
const $lux = app.config.globalProperties.$lux
const date = '2024-07-12T12:23:49.000Z'
const outputs = [
{ format: undefined, expected: '7/12/2024, 12:23 PM' },
{ format: 'full', expected: 'July 12, 2024 at 12:23 PM UTC' },
{ format: 'fulls', expected: 'July 12, 2024 at 12:23:49 PM UTC' },
{ format: 'huge', expected: 'Friday, July 12, 2024 at 12:23 PM UTC' },
{ format: 'huges', expected: 'Friday, July 12, 2024 at 12:23:49 PM UTC' },
{ format: 'med', expected: 'Jul 12, 2024, 12:23 PM' },
{ format: 'meds', expected: 'Jul 12, 2024, 12:23:49 PM' },
{ format: 'short', expected: '7/12/2024, 12:23 PM' },
{ format: 'shorts', expected: '7/12/2024, 12:23:49 PM' },
{ format: 'date_full', expected: 'July 12, 2024' },
{ format: 'date_huge', expected: 'Friday, July 12, 2024' },
{ format: 'date_med', expected: 'Jul 12, 2024' },
{ format: 'date_medd', expected: 'Fri, Jul 12, 2024' },
{ format: 'date_short', expected: '7/12/2024' },
{ format: 'time24', expected: '12:23' },
{ format: 'time24longoffset', expected: '12:23:49 UTC' },
{ format: 'time24s', expected: '12:23:49' },
{ format: 'time', expected: '12:23 PM' },
{ format: 'times', expected: '12:23:49 PM' },
{ format: 'iso', expected: '2024-07-12T12:23:49.000Z' },
{ format: 'sql', expected: '2024-07-12 12:23:49.000 Z' },
{ format: 'http', expected: 'Fri, 12 Jul 2024 12:23:49 GMT' },
{ format: 'rfc', expected: 'Fri, 12 Jul 2024 12:23:49 +0000' },
{ format: 'rfc2822', expected: 'Fri, 12 Jul 2024 12:23:49 +0000' },
{ format: 'millis', expected: 1720787029000 },
{ format: 'unix', expected: 1720787029 },
{ format: 'seconds', expected: 1720787029 },
]
for (const { format, expected } of outputs) {
expect($lux(date, format), format).toBe(expected)
}
expect($lux(date, 'jsdate').getTime()).toBe(new Date(1720787029 * 1000).getTime())
expect($lux(date, 'relative')).toMatch('ago')
expect(() => $lux(date, { format: 1 })).toThrowError(TypeError)
})
it('format with locale', () => {
const app = initApp({
output: {
zone: 'utc',
locale: 'it',
},
})
const $lux = app.config.globalProperties.$lux
const date = '2024-07-12T12:23:49.000Z'
const outputs = [
{ format: 'full', expected: '12 luglio 2024 alle ore 12:23 UTC' },
{ format: 'fulls', expected: '12 luglio 2024 alle ore 12:23:49 UTC' },
{ format: 'huge', expected: 'venerdì 12 luglio 2024 alle ore 12:23 UTC' },
{
format: 'huges',
expected: 'venerdì 12 luglio 2024 alle ore 12:23:49 UTC',
},
{ format: 'med', expected: '12 lug 2024, 12:23' },
{ format: 'meds', expected: '12 lug 2024, 12:23:49' },
{ format: 'short', expected: '12/07/2024, 12:23' },
{ format: 'shorts', expected: '12/07/2024, 12:23:49' },
{ format: 'date_full', expected: '12 luglio 2024' },
{ format: 'date_huge', expected: 'venerdì 12 luglio 2024' },
{ format: 'date_med', expected: '12 lug 2024' },
{ format: 'date_medd', expected: 'ven 12 lug 2024' },
{ format: 'date_short', expected: '12/07/2024' },
{ format: 'time24', expected: '12:23' },
{ format: 'time24longoffset', expected: '12:23:49 UTC' },
{ format: 'time24s', expected: '12:23:49' },
{ format: 'time', expected: '12:23' },
{ format: 'times', expected: '12:23:49' },
]
for (const { format, expected } of outputs) {
expect($lux(date, format), format).toBe(expected)
}
})
it('format with timezones', () => {
const app = initApp({
output: {
zone: 'America/New_York',
},
})
const $lux = app.config.globalProperties.$lux
const date = '2024-07-12T12:23:49.000Z'
const outputs = [
{ format: undefined, expected: '7/12/2024, 8:23 AM' },
{ format: 'full', expected: 'July 12, 2024 at 8:23 AM EDT' },
{ format: 'fulls', expected: 'July 12, 2024 at 8:23:49 AM EDT' },
{ format: 'huge', expected: 'Friday, July 12, 2024 at 8:23 AM Eastern Daylight Time' },
{ format: 'huges', expected: 'Friday, July 12, 2024 at 8:23:49 AM Eastern Daylight Time' },
{ format: 'med', expected: 'Jul 12, 2024, 8:23 AM' },
{ format: 'meds', expected: 'Jul 12, 2024, 8:23:49 AM' },
{ format: 'short', expected: '7/12/2024, 8:23 AM' },
{ format: 'shorts', expected: '7/12/2024, 8:23:49 AM' },
{ format: 'time24', expected: '08:23' },
{ format: 'time24longoffset', expected: '08:23:49 Eastern Daylight Time' },
{ format: 'time24s', expected: '08:23:49' },
{ format: 'time', expected: '8:23 AM' },
{ format: 'times', expected: '8:23:49 AM' },
]
for (const { format, expected } of outputs) {
expect($lux(date, format), format).toMatch(expected)
}
const dateIn = '2024-07-12T12:23:49'
const zoneIn = 'Asia/Tokyo'
const zoneOut = 'Europe/Rome'
expect($lux(dateIn, { format: 'huges', zone: zoneOut }, { zone: zoneIn })).toMatch('Friday, July 12, 2024 at 5:23:49 AM Central European Summer Time')
})
it('format with templates', () => {
const app = initApp({
templates: {
date: { format: 'dd/MM/yyyy' },
},
})
const $lux = app.config.globalProperties.$lux
const date = '2024-07-12T12:23:49.000Z'
const outputs = [{ format: 'date', expected: '12/07/2024' }]
for (const { format, expected } of outputs) {
expect($lux(date, format)).toBe(expected)
}
})
})
describe('parse', () => {
it('inputs', () => {
const app = initApp({
templates: {
my_template: { format: 'dd-MM--yyyy' },
},
})
const $lp = app.config.globalProperties.$lp
const date = '2024-07-12'
expect($lp('12/07/2024', 'dd/MM/yyyy').toISODate()).toBe(date)
expect($lp('12/07/2024', { format: 'dd/MM/yyyy' }).toISODate()).toBe(date)
expect($lp('12-07--2024', { format: 'my_template' }).toISODate()).toBe(date)
expect($lp(1720742400 * 1000).toISODate()).toBe(date)
expect($lp(DateTime.fromObject({ year: 2024, month: 7, day: 12 }, { zone: 'utc' })).toISODate()).toBe(date)
const jsDate = new Date()
jsDate.setUTCFullYear(2024, 6, 12)
jsDate.setUTCHours(0, 0, 0, 0)
expect($lp(jsDate).toISODate()).toBe(date)
const formats = [
{ format: 'iso', value: '2024-07-12' },
{ format: 'sql', value: '2024-07-12' },
{ format: 'http', value: 'Fri, 12 Jul 2024 12:23:49 GMT' },
{ format: 'rfc2822', value: 'Fri, 12 Jul 2024 12:23:49 +0000' },
{ format: 'millis', value: 1720787029000 },
{ format: 'unix', value: 1720787029 },
{ format: 'seconds', value: 1720787029 },
{ format: 'luxon', value: DateTime.fromObject({ year: 2024, month: 7, day: 12 }, { zone: 'utc' }) },
]
expect(() => $lp('2024-07-12', { format: 'jsdate' })).toThrowError(TypeError)
expect(() => $lp(null, { format: 'jsdate' })).toThrowError(TypeError)
expect(() => $lp('2024-07-12', { format: 'millis' })).toThrowError(TypeError)
expect(() => $lp(null, { format: 'millis' })).toThrowError(TypeError)
expect(() => $lp('2024-07-12', { format: 'seconds' })).toThrowError(TypeError)
expect(() => $lp(null, { format: 'seconds' })).toThrowError(TypeError)
expect(() => $lp('2024-07-12', { format: 0 })).toThrowError(TypeError)
expect(() => $lp(0, { format: 'short' })).toThrowError(TypeError)
expect(() => $lp(null, { format: 'short' })).toThrowError(TypeError)
for (const { format, value } of formats) {
expect($lp(value, { format }).toISODate(), format).toBe(date)
}
})
})
describe('dates', () => {
it('simple dates utc', () => {
const app = initApp({
output: {
zone: 'utc',
locale: 'it',
},
})
const $lux = app.config.globalProperties.$lux
const date = '2024-07-12'
const outputs = [
{ format: 'date_short', expected: '12/07/2024' },
{ format: 'short', expected: '12/07/2024, 00:00' },
]
for (const { format, expected } of outputs) {
expect($lux(date, format)).toBe(expected)
}
})
})
describe('times', () => {
it('simple utc', () => {
const app = initApp({
output: {
zone: 'utc',
locale: 'it',
},
})
const $lux = app.config.globalProperties.$lux
const date = '16:20:00'
const today = $lux(new Date(), 'date_short')
const outputs = [
{ format: 'time', expected: '16:20' },
{ format: 'short', expected: `${today}, 16:20` },
]
for (const { format, expected } of outputs) {
expect($lux(date, format)).toBe(expected)
}
})
})