Reports.utils.test.ts45 lines · main
1import dayjs from 'dayjs'
2import utc from 'dayjs/plugin/utc'
3import { describe, expect, it } from 'vitest'
4
5import { formatTimestamp } from './Reports.utils'
6
7dayjs.extend(utc)
8
9describe('formatTimestamp', () => {
10 it('formats milliseconds timestamp correctly', () => {
11 const timestamp = 1640995200000 // 2022-01-01 00:00:00 UTC in milliseconds
12 const result = formatTimestamp(timestamp, { returnUtc: true })
13 expect(result).toBe('Jan 1, 12:00am')
14 })
15
16 it('formats microseconds timestamp correctly', () => {
17 const timestamp = 1640995200000000 // 2022-01-01 00:00:00 UTC in microseconds
18 const result = formatTimestamp(timestamp, { returnUtc: true })
19 expect(result).toBe('Jan 1, 12:00am')
20 })
21
22 it('formats seconds timestamp correctly', () => {
23 const timestamp = 1640995200 // 2022-01-01 00:00:00 UTC in seconds
24 const result = formatTimestamp(timestamp, { returnUtc: true })
25 expect(result).toBe('Jan 1, 12:00am')
26 })
27
28 it('handles string timestamp input', () => {
29 const timestamp = '1640995200000'
30 const result = formatTimestamp(timestamp, { returnUtc: true })
31 expect(result).toBe('Jan 1, 12:00am')
32 })
33
34 it('handles invalid string timestamp', () => {
35 const timestamp = 'invalid-timestamp'
36 const result = formatTimestamp(timestamp, { returnUtc: true })
37 expect(result).toBe('Invalid Date')
38 })
39
40 it('handles zero timestamp', () => {
41 const timestamp = 0
42 const result = formatTimestamp(timestamp, { returnUtc: true })
43 expect(result).toBe('Jan 1, 12:00am')
44 })
45})