Reports.utils.test.ts 1.5 KB

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