-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathformat.test.ts
More file actions
45 lines (34 loc) · 1.31 KB
/
format.test.ts
File metadata and controls
45 lines (34 loc) · 1.31 KB
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
import type { Formatter } from '@nanostores/i18n'
import { equal, match } from 'node:assert/strict'
import { describe, test } from 'node:test'
import { formatPublishedAt } from '../format.ts'
describe('format', () => {
function createMockFormatter(): Formatter {
return {
number: () => '',
relativeTime: (num, unit) => `${num} ${unit}`,
time: date => {
let d = date instanceof Date ? date : new Date(date ?? 0)
return d.toISOString()
}
}
}
test('formats time as minutes ago when less than hour', () => {
let format = createMockFormatter()
let thirtyMinutesAgo = Math.floor((Date.now() - 30 * 60 * 1000) / 1000)
let result = formatPublishedAt(format, thirtyMinutesAgo)
equal(result, '-30 minute')
})
test('formats time as hours ago when less than day', () => {
let format = createMockFormatter()
let threeHoursAgo = Math.floor((Date.now() - 3 * 60 * 60 * 1000) / 1000)
let result = formatPublishedAt(format, threeHoursAgo)
equal(result, '-3 hour')
})
test('formats time as full date when more than day', () => {
let format = createMockFormatter()
let twoDaysAgo = Math.floor((Date.now() - 2 * 24 * 60 * 60 * 1000) / 1000)
let result = formatPublishedAt(format, twoDaysAgo)
match(result, /^\d{4}-\d{2}-\d{2}T/)
})
})