OrgAuditLogs.utils.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import dayjs from 'dayjs'
  2. import { afterEach, describe, expect, test, vi } from 'vitest'
  3. import { formatSelectedDateRange } from '@/components/interfaces/Organization/AuditLogs/AuditLogs.utils'
  4. // Pin "now" to a fixed point so date comparisons are deterministic
  5. const NOW = dayjs('2024-06-15T14:30:00')
  6. afterEach(() => {
  7. vi.useRealTimers()
  8. })
  9. function fakeNow() {
  10. vi.setSystemTime(NOW.toDate())
  11. }
  12. describe('formatSelectedDateRange', () => {
  13. test('two different dates: preserves current time on both ends', () => {
  14. fakeNow()
  15. const result = formatSelectedDateRange({
  16. from: '2024-06-10',
  17. to: '2024-06-14',
  18. })
  19. const from = dayjs(result.from)
  20. const to = dayjs(result.to)
  21. expect(from.date()).toBe(10)
  22. expect(to.date()).toBe(14)
  23. // Both should carry current H:M:S
  24. expect(from.hour()).toBe(NOW.hour())
  25. expect(to.hour()).toBe(NOW.hour())
  26. })
  27. test('single date matching today: from is set to 00:00:00', () => {
  28. fakeNow()
  29. const today = NOW.format('YYYY-MM-DD')
  30. const result = formatSelectedDateRange({ from: today, to: today })
  31. const from = dayjs(result.from)
  32. expect(from.hour()).toBe(0)
  33. expect(from.minute()).toBe(0)
  34. expect(from.second()).toBe(0)
  35. })
  36. test('single date matching today: to keeps current time', () => {
  37. fakeNow()
  38. const today = NOW.format('YYYY-MM-DD')
  39. const result = formatSelectedDateRange({ from: today, to: today })
  40. const to = dayjs(result.to)
  41. expect(to.hour()).toBe(NOW.hour())
  42. expect(to.minute()).toBe(NOW.minute())
  43. })
  44. test('single date in the past: to is set to 23:59:59', () => {
  45. fakeNow()
  46. const result = formatSelectedDateRange({
  47. from: '2024-06-01',
  48. to: '2024-06-01',
  49. })
  50. const to = dayjs(result.to)
  51. expect(to.hour()).toBe(23)
  52. expect(to.minute()).toBe(59)
  53. expect(to.second()).toBe(59)
  54. })
  55. test('single date in the past: from keeps current time', () => {
  56. fakeNow()
  57. const result = formatSelectedDateRange({
  58. from: '2024-06-01',
  59. to: '2024-06-01',
  60. })
  61. const from = dayjs(result.from)
  62. expect(from.hour()).toBe(NOW.hour())
  63. })
  64. test('output is in UTC ISO format', () => {
  65. fakeNow()
  66. const result = formatSelectedDateRange({
  67. from: '2024-06-10',
  68. to: '2024-06-14',
  69. })
  70. expect(result.from).toMatch(/Z$/)
  71. expect(result.to).toMatch(/Z$/)
  72. })
  73. })