/
index.test.ts
266 lines (220 loc) · 8.66 KB
/
index.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
262
263
264
265
266
import { promiseTimeout, timestamp } from '@vueuse/shared'
import type { ComputedRef } from 'vue-demi'
import { computed, ref } from 'vue-demi'
import { useTimeAgo } from '.'
type TimeUnit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
const UNITS = [
{ max: 60000, value: 1000, name: 'second' },
{ max: 2760000, value: 60000, name: 'minute' },
{ max: 72000000, value: 3600000, name: 'hour' },
{ max: 518400000, value: 86400000, name: 'day' },
{ max: 2419200000, value: 604800000, name: 'week' },
{ max: 28512000000, value: 2592000000, name: 'month' },
{ max: Infinity, value: 31536000000, name: 'year' },
]
const fullDateFormatter = (value: any) => new Date(value).toISOString().slice(0, 10)
function getNeededTimeChange(type: TimeUnit, count: number, adjustSecond?: number) {
const unit = UNITS.find(i => i.name === type)
return (unit?.value || 0) * count + (adjustSecond || 0) * 1000
}
describe('useTimeAgo', () => {
let baseTime: number
const changeValue = ref(0)
let changeTime: ComputedRef<number>
function reset() {
vi.useFakeTimers()
baseTime = timestamp()
vi.setSystemTime(baseTime)
changeValue.value = 0
changeTime = computed(() => baseTime + changeValue.value)
}
beforeEach(() => {
reset()
})
afterEach(() => {
vi.useRealTimers()
})
test('control now', async () => {
vi.useRealTimers()
const { resume, pause, timeAgo } = useTimeAgo(baseTime, { controls: true, showSecond: true, updateInterval: 500 })
expect(timeAgo.value).toBe('0 second ago')
pause()
await promiseTimeout(1000)
expect(timeAgo.value).toBe('0 second ago')
resume()
await promiseTimeout(1000)
expect(timeAgo.value).toBe('2 seconds ago')
})
test('get undefined when time is invalid', () => {
expect(useTimeAgo('invalid date').value).toBeUndefined()
})
describe('just now', () => {
test('just now', () => {
expect(useTimeAgo(baseTime).value).toBe('just now')
})
test('just now using custom formatter', () => {
// @ts-expect-error mock messages
expect(useTimeAgo(baseTime, { messages: { second: '{0}', future: '{0}' }, showSecond: true }).value).toBe('0')
})
})
describe('second', () => {
function testSecond(isFuture: boolean) {
const text = isFuture ? 'future' : 'past'
const nextTime = getNeededTimeChange('minute', 1, -1) * (isFuture ? 1 : -1)
test(`${text}: less than 1 minute`, () => {
changeValue.value = nextTime
expect(useTimeAgo(changeTime).value).toBe('just now')
})
test(`${text}: less than 1 second`, () => {
changeValue.value = getNeededTimeChange('minute', 1, -59.6) * (isFuture ? 1 : -1)
expect(useTimeAgo(changeTime, { showSecond: true }).value).toBe(
isFuture ? 'in 0 second' : '0 second ago')
})
test(`${text}: less than 1 minute/ with showSecond`, () => {
changeValue.value = nextTime
expect(useTimeAgo(changeTime, { showSecond: true }).value).toBe(
isFuture ? 'in 59 seconds' : '59 seconds ago')
})
test(`${text}: less than 1 minute but more than 10 seconds with showSecond`, () => {
changeValue.value = nextTime
expect(useTimeAgo(changeTime, { showSecond: true, max: 10000 }).value).toBe(fullDateFormatter(changeTime.value))
})
test(`${text}: more than 1 minute`, () => {
changeValue.value = getNeededTimeChange('minute', 1, 1) * (isFuture ? 1 : -1)
expect(useTimeAgo(changeTime, { showSecond: true, max: 'second' }).value).toBe(fullDateFormatter(changeTime.value))
})
}
testSecond(true)
testSecond(false)
})
describe('minute', () => {
test('future: 1 minute', () => {
changeValue.value = getNeededTimeChange('minute', 1)
expect(useTimeAgo(changeTime).value).toBe('in 1 minute')
})
test('past: 1 minute', () => {
changeValue.value = -getNeededTimeChange('minute', 1)
expect(useTimeAgo(changeTime).value).toBe('1 minute ago')
})
test('future: 10 minutes', () => {
changeValue.value = getNeededTimeChange('minute', 10)
expect(useTimeAgo(changeTime).value).toBe('in 10 minutes')
})
test('past: 10 minutes', () => {
changeValue.value = -getNeededTimeChange('minute', 10)
expect(useTimeAgo(changeTime).value).toBe('10 minutes ago')
})
})
describe('hour', () => {
test('future: 1 hour', () => {
changeValue.value = getNeededTimeChange('hour', 1)
expect(useTimeAgo(changeTime).value).toBe('in 1 hour')
})
test('past: 1 hour', () => {
changeValue.value = -getNeededTimeChange('hour', 1)
expect(useTimeAgo(changeTime).value).toBe('1 hour ago')
})
test('future: 10 hours', () => {
changeValue.value = getNeededTimeChange('hour', 10)
expect(useTimeAgo(changeTime).value).toBe('in 10 hours')
})
test('past: 10 hours', () => {
changeValue.value = -getNeededTimeChange('hour', 10)
expect(useTimeAgo(changeTime).value).toBe('10 hours ago')
})
})
describe('day', () => {
test('future: 1 day', () => {
changeValue.value = getNeededTimeChange('day', 1)
expect(useTimeAgo(changeTime).value).toBe('tomorrow')
})
test('past: 1 day', () => {
changeValue.value = -getNeededTimeChange('day', 1)
expect(useTimeAgo(changeTime).value).toBe('yesterday')
})
test('future: 3 days', () => {
changeValue.value = getNeededTimeChange('day', 3)
expect(useTimeAgo(changeTime).value).toBe('in 3 days')
})
test('past: 3 days', () => {
changeValue.value = -getNeededTimeChange('day', 3)
expect(useTimeAgo(changeTime).value).toBe('3 days ago')
})
})
describe('week', () => {
test('future: 1 week', () => {
changeValue.value = getNeededTimeChange('week', 1)
expect(useTimeAgo(changeTime).value).toBe('next week')
})
test('past: 1 week', () => {
changeValue.value = -getNeededTimeChange('week', 1)
expect(useTimeAgo(changeTime).value).toBe('last week')
})
test('future: 3 weeks', () => {
changeValue.value = getNeededTimeChange('week', 3)
expect(useTimeAgo(changeTime).value).toBe('in 3 weeks')
})
test('past: 3 weeks', () => {
changeValue.value = -getNeededTimeChange('week', 3)
expect(useTimeAgo(changeTime).value).toBe('3 weeks ago')
})
})
describe('month', () => {
test('future: 1 month', () => {
changeValue.value = getNeededTimeChange('month', 1)
expect(useTimeAgo(changeTime).value).toBe('next month')
})
test('past: 1 month', () => {
changeValue.value = -getNeededTimeChange('month', 1)
expect(useTimeAgo(changeTime).value).toBe('last month')
})
test('future: 3 months', () => {
changeValue.value = getNeededTimeChange('month', 3)
expect(useTimeAgo(changeTime).value).toBe('in 3 months')
})
test('past: 3 months', () => {
changeValue.value = -getNeededTimeChange('month', 3)
expect(useTimeAgo(changeTime).value).toBe('3 months ago')
})
})
describe('year', () => {
test('future: 1 year', () => {
changeValue.value = getNeededTimeChange('year', 1)
expect(useTimeAgo(changeTime).value).toBe('next year')
})
test('past: 1 year', () => {
changeValue.value = -getNeededTimeChange('year', 1)
expect(useTimeAgo(changeTime).value).toBe('last year')
})
test('future: 3 years', () => {
changeValue.value = getNeededTimeChange('year', 3)
expect(useTimeAgo(changeTime).value).toBe('in 3 years')
})
test('past: 3 years', () => {
changeValue.value = -getNeededTimeChange('year', 3)
expect(useTimeAgo(changeTime).value).toBe('3 years ago')
})
})
test('rounding', () => {
changeValue.value = getNeededTimeChange('day', 5.49)
expect(useTimeAgo(changeTime).value).toBe('in 5 days')
expect(useTimeAgo(changeTime, { rounding: 'ceil' }).value).toBe('in 6 days')
expect(useTimeAgo(changeTime, { rounding: 'floor' }).value).toBe('in 5 days')
expect(useTimeAgo(changeTime, { rounding: 1 }).value).toBe('in 5.5 days')
expect(useTimeAgo(changeTime, { rounding: 3 }).value).toBe('in 5.49 days')
})
test('custom units', () => {
changeValue.value = getNeededTimeChange('day', 14)
expect(useTimeAgo(changeTime).value).toBe('in 2 weeks')
expect(useTimeAgo(changeTime, {
units: [
{ max: 60000, value: 1000, name: 'second' },
{ max: 2760000, value: 60000, name: 'minute' },
{ max: 72000000, value: 3600000, name: 'hour' },
{ max: 518400000 * 30, value: 86400000, name: 'day' },
{ max: 28512000000, value: 2592000000, name: 'month' },
{ max: Infinity, value: 31536000000, name: 'year' },
],
}).value).toBe('in 14 days')
})
})