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