|
| 1 | +import type { CalendarEventApi } from '@repo/types'; |
| 2 | +import { utcStringToLocalDateTime } from '@repo/utils'; |
| 3 | +import { Temporal } from 'temporal-polyfill'; |
| 4 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 5 | + |
| 6 | +import { CalendarEvent } from './calendar-event'; |
| 7 | + |
| 8 | +vi.mock('@repo/utils', () => ({ |
| 9 | + utcStringToLocalDateTime: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const mockUtcStringToLocalDateTime = vi.mocked(utcStringToLocalDateTime); |
| 13 | + |
| 14 | +describe('CalendarEvent', () => { |
| 15 | + const DEFAULT_EVENT_ID = 'event-123'; |
| 16 | + const DEFAULT_CALENDAR_ID = 'calendar-456'; |
| 17 | + const DEFAULT_START_DATE = '2025-01-15T10:30:00Z'; |
| 18 | + const DEFAULT_END_DATE = '2025-01-15T12:00:00Z'; |
| 19 | + const DEFAULT_TITLE = 'Test Event'; |
| 20 | + const DEFAULT_DESCRIPTION = 'Test event description'; |
| 21 | + |
| 22 | + const createMockCalendarEventApi = (overrides: Partial<CalendarEventApi> = {}): CalendarEventApi => ({ |
| 23 | + id: DEFAULT_EVENT_ID, |
| 24 | + title: DEFAULT_TITLE, |
| 25 | + description: DEFAULT_DESCRIPTION, |
| 26 | + start_date: DEFAULT_START_DATE, |
| 27 | + end_date: DEFAULT_END_DATE, |
| 28 | + all_day: false, |
| 29 | + calendar_id: DEFAULT_CALENDAR_ID, |
| 30 | + ...overrides, |
| 31 | + }); |
| 32 | + |
| 33 | + const mockStartDateTime = new Temporal.PlainDateTime(2025, 1, 15, 10, 30, 0); |
| 34 | + const mockEndDateTime = new Temporal.PlainDateTime(2025, 1, 15, 12, 0, 0); |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks(); |
| 38 | + mockUtcStringToLocalDateTime.mockImplementation((utcString: string) => { |
| 39 | + if (utcString === DEFAULT_START_DATE) { |
| 40 | + return mockStartDateTime; |
| 41 | + } |
| 42 | + if (utcString === DEFAULT_END_DATE) { |
| 43 | + return mockEndDateTime; |
| 44 | + } |
| 45 | + |
| 46 | + return new Temporal.PlainDateTime(2025, 1, 1, 0, 0, 0); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + vi.restoreAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('constructor', () => { |
| 55 | + it.each([ |
| 56 | + { |
| 57 | + name: 'default event', |
| 58 | + api: createMockCalendarEventApi(), |
| 59 | + expected: { |
| 60 | + id: DEFAULT_EVENT_ID, |
| 61 | + title: DEFAULT_TITLE, |
| 62 | + description: DEFAULT_DESCRIPTION, |
| 63 | + start: mockStartDateTime, |
| 64 | + end: mockEndDateTime, |
| 65 | + allDay: false, |
| 66 | + calendarId: DEFAULT_CALENDAR_ID, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'all-day event', |
| 71 | + api: createMockCalendarEventApi({ |
| 72 | + all_day: true, |
| 73 | + title: 'All Day Event', |
| 74 | + description: 'All day event description', |
| 75 | + }), |
| 76 | + expected: { |
| 77 | + id: DEFAULT_EVENT_ID, |
| 78 | + title: 'All Day Event', |
| 79 | + description: 'All day event description', |
| 80 | + start: mockStartDateTime, |
| 81 | + end: mockEndDateTime, |
| 82 | + allDay: true, |
| 83 | + calendarId: DEFAULT_CALENDAR_ID, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'event with empty strings', |
| 88 | + api: createMockCalendarEventApi({ |
| 89 | + id: '', |
| 90 | + title: '', |
| 91 | + description: '', |
| 92 | + calendar_id: '', |
| 93 | + }), |
| 94 | + expected: { |
| 95 | + id: '', |
| 96 | + title: '', |
| 97 | + description: '', |
| 98 | + start: mockStartDateTime, |
| 99 | + end: mockEndDateTime, |
| 100 | + allDay: false, |
| 101 | + calendarId: '', |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: 'custom event with different dates', |
| 106 | + api: createMockCalendarEventApi({ |
| 107 | + id: 'custom-event-789', |
| 108 | + title: 'Custom Event', |
| 109 | + description: 'Custom event description', |
| 110 | + start_date: '2024-12-25T09:00:00Z', |
| 111 | + end_date: '2024-12-25T17:00:00Z', |
| 112 | + calendar_id: 'custom-calendar-999', |
| 113 | + }), |
| 114 | + expected: { |
| 115 | + id: 'custom-event-789', |
| 116 | + title: 'Custom Event', |
| 117 | + description: 'Custom event description', |
| 118 | + start: new Temporal.PlainDateTime(2025, 1, 1, 0, 0, 0), |
| 119 | + end: new Temporal.PlainDateTime(2025, 1, 1, 0, 0, 0), |
| 120 | + allDay: false, |
| 121 | + calendarId: 'custom-calendar-999', |
| 122 | + }, |
| 123 | + }, |
| 124 | + ])('should create CalendarEvent instance correctly: $name', ({ api, expected }) => { |
| 125 | + const calendarEvent = new CalendarEvent(api); |
| 126 | + |
| 127 | + expect(calendarEvent.id).toBe(expected.id); |
| 128 | + expect(calendarEvent.title).toBe(expected.title); |
| 129 | + expect(calendarEvent.description).toBe(expected.description); |
| 130 | + expect(calendarEvent.start).toStrictEqual(expected.start); |
| 131 | + expect(calendarEvent.end).toStrictEqual(expected.end); |
| 132 | + expect(calendarEvent.allDay).toBe(expected.allDay); |
| 133 | + expect(calendarEvent.calendarId).toBe(expected.calendarId); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should call utcStringToLocalDateTime with correct parameters', () => { |
| 137 | + const eventApi = createMockCalendarEventApi(); |
| 138 | + |
| 139 | + new CalendarEvent(eventApi); |
| 140 | + |
| 141 | + expect(mockUtcStringToLocalDateTime).toHaveBeenCalledTimes(2); |
| 142 | + expect(mockUtcStringToLocalDateTime).toHaveBeenCalledWith(DEFAULT_START_DATE); |
| 143 | + expect(mockUtcStringToLocalDateTime).toHaveBeenCalledWith(DEFAULT_END_DATE); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should handle different date formats correctly', () => { |
| 147 | + const customStartDate = '2024-06-15T14:30:00Z'; |
| 148 | + const customEndDate = '2024-06-15T16:45:00Z'; |
| 149 | + |
| 150 | + const customStartDateTime = new Temporal.PlainDateTime(2024, 6, 15, 14, 30, 0); |
| 151 | + const customEndDateTime = new Temporal.PlainDateTime(2024, 6, 15, 16, 45, 0); |
| 152 | + |
| 153 | + mockUtcStringToLocalDateTime.mockImplementation((utcString: string) => { |
| 154 | + if (utcString === customStartDate) { |
| 155 | + return customStartDateTime; |
| 156 | + } |
| 157 | + if (utcString === customEndDate) { |
| 158 | + return customEndDateTime; |
| 159 | + } |
| 160 | + |
| 161 | + return new Temporal.PlainDateTime(2025, 1, 1, 0, 0, 0); |
| 162 | + }); |
| 163 | + |
| 164 | + const eventApi = createMockCalendarEventApi({ |
| 165 | + start_date: customStartDate, |
| 166 | + end_date: customEndDate, |
| 167 | + }); |
| 168 | + |
| 169 | + const calendarEvent = new CalendarEvent(eventApi); |
| 170 | + |
| 171 | + expect(calendarEvent.start).toStrictEqual(customStartDateTime); |
| 172 | + expect(calendarEvent.end).toStrictEqual(customEndDateTime); |
| 173 | + expect(mockUtcStringToLocalDateTime).toHaveBeenCalledWith(customStartDate); |
| 174 | + expect(mockUtcStringToLocalDateTime).toHaveBeenCalledWith(customEndDate); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments