|
1 | 1 | import {describe, expect, it} from 'vitest'; |
2 | 2 | import {formatRelativeDate} from './format-relative-date-fn.svelte'; |
3 | 3 |
|
4 | | -const cfg = (now: Date, smallestUnit: 'milliseconds' | 'seconds' | 'minutes' | 'hours' = 'seconds') => ({ |
5 | | - defaultValue: 'NEVER', |
6 | | - now, |
7 | | - smallestUnit, |
8 | | -}); |
| 4 | +function config(now: Date, smallestUnit: 'milliseconds' | 'seconds' | 'minutes' | 'hours' = 'seconds') { |
| 5 | + return {defaultValue: 'NEVER', now, smallestUnit}; |
| 6 | +} |
9 | 7 |
|
10 | 8 | describe('formatRelativeDate', () => { |
11 | 9 | it('returns defaultValue for nullish input', () => { |
12 | 10 | const now = new Date(); |
13 | | - expect(formatRelativeDate(null, undefined, cfg(now))).toBe('NEVER'); |
14 | | - expect(formatRelativeDate(undefined, undefined, cfg(now))).toBe('NEVER'); |
| 11 | + expect(formatRelativeDate(null, undefined, config(now))).toBe('NEVER'); |
| 12 | + expect(formatRelativeDate(undefined, undefined, config(now))).toBe('NEVER'); |
15 | 13 | }); |
16 | 14 |
|
17 | 15 | it('formats a past duration', () => { |
18 | 16 | const now = new Date(); |
19 | | - const result = formatRelativeDate(new Date(now.getTime() - 3000), undefined, cfg(now)); |
| 17 | + const result = formatRelativeDate(new Date(now.getTime() - 3000), undefined, config(now)); |
20 | 18 | expect(result.startsWith('3 ')).toBe(true); |
21 | 19 | expect(result.endsWith(' ago')).toBe(true); |
22 | 20 | }); |
23 | 21 |
|
24 | 22 | it('formats a future duration', () => { |
25 | 23 | const now = new Date(); |
26 | | - const result = formatRelativeDate(new Date(now.getTime() + 3000), undefined, cfg(now)); |
| 24 | + const result = formatRelativeDate(new Date(now.getTime() + 3000), undefined, config(now)); |
27 | 25 | expect(result.startsWith('in 3 ')).toBe(true); |
28 | 26 | }); |
29 | 27 |
|
30 | 28 | it('falls back to a zero-duration string when diff is below smallestUnit', () => { |
31 | 29 | const now = new Date(); |
32 | | - expect(formatRelativeDate(new Date(now.getTime() - 500), undefined, cfg(now))).toMatch(/^0 .+ ago$/); |
33 | | - expect(formatRelativeDate(new Date(now.getTime() + 500), undefined, cfg(now))).toMatch(/^in 0 /); |
| 30 | + expect(formatRelativeDate(new Date(now.getTime() - 500), undefined, config(now))).toMatch(/^0 .+ ago$/); |
| 31 | + expect(formatRelativeDate(new Date(now.getTime() + 500), undefined, config(now))).toMatch(/^in 0 /); |
34 | 32 | }); |
35 | 33 |
|
36 | 34 | it('produces the correct plural for zero (style=long)', () => { |
37 | 35 | const now = new Date(); |
38 | | - expect(formatRelativeDate(now, {style: 'long'}, cfg(now))).toContain('0 seconds'); |
| 36 | + expect(formatRelativeDate(now, {style: 'long'}, config(now))).toContain('0 seconds'); |
39 | 37 | }); |
40 | 38 |
|
41 | 39 | it('accepts ISO-string dates', () => { |
42 | 40 | const now = new Date(); |
43 | 41 | const earlier = new Date(now.getTime() - 3000).toISOString(); |
44 | | - expect(formatRelativeDate(earlier, undefined, cfg(now)).startsWith('3 ')).toBe(true); |
| 42 | + expect(formatRelativeDate(earlier, undefined, config(now)).startsWith('3 ')).toBe(true); |
45 | 43 | }); |
46 | 44 | }); |
0 commit comments