Skip to content

Commit 1490b0b

Browse files
SergeySergey
authored andcommitted
add tests for models package
1 parent fc48092 commit 1490b0b

13 files changed

Lines changed: 1822 additions & 24 deletions

File tree

packages/models/src/api/Api.test.ts

Lines changed: 513 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import type { CalendarApi } from '@repo/types';
2+
import { describe, it, expect } from 'vitest';
3+
4+
import { Calendar } from './calendar-entity';
5+
6+
describe('Calendar', () => {
7+
const DEFAULT_CALENDAR_ID = 'calendar-123';
8+
const DEFAULT_SUMMARY = 'Test Calendar';
9+
const DEFAULT_DESCRIPTION = 'Test calendar description';
10+
const DEFAULT_COLOR = '#FF5733';
11+
const DEFAULT_USER_ID = 'user-456';
12+
const DEFAULT_CREATED_AT = '2025-01-01T00:00:00Z';
13+
const DEFAULT_UPDATED_AT = '2025-01-02T00:00:00Z';
14+
15+
const createMockCalendarApi = (overrides: Partial<CalendarApi> = {}): CalendarApi => ({
16+
id: DEFAULT_CALENDAR_ID,
17+
summary: DEFAULT_SUMMARY,
18+
description: DEFAULT_DESCRIPTION,
19+
color: DEFAULT_COLOR,
20+
user_id: DEFAULT_USER_ID,
21+
created_at: DEFAULT_CREATED_AT,
22+
updated_at: DEFAULT_UPDATED_AT,
23+
checked: true,
24+
...overrides,
25+
});
26+
27+
describe('constructor', () => {
28+
it.each([
29+
{
30+
name: 'default calendar',
31+
api: createMockCalendarApi(),
32+
expected: {
33+
id: DEFAULT_CALENDAR_ID,
34+
summary: DEFAULT_SUMMARY,
35+
description: DEFAULT_DESCRIPTION,
36+
color: DEFAULT_COLOR,
37+
userId: DEFAULT_USER_ID,
38+
createdAt: DEFAULT_CREATED_AT,
39+
updatedAt: DEFAULT_UPDATED_AT,
40+
checked: true,
41+
},
42+
},
43+
{
44+
name: 'custom calendar',
45+
api: createMockCalendarApi({
46+
id: 'unique-calendar-789',
47+
summary: 'Work Calendar',
48+
description: 'Calendar for work events',
49+
color: '#3498DB',
50+
user_id: 'user-999',
51+
created_at: '2023-12-01T10:30:00Z',
52+
updated_at: '2023-12-15T14:45:00Z',
53+
checked: false,
54+
}),
55+
expected: {
56+
id: 'unique-calendar-789',
57+
summary: 'Work Calendar',
58+
description: 'Calendar for work events',
59+
color: '#3498DB',
60+
userId: 'user-999',
61+
createdAt: '2023-12-01T10:30:00Z',
62+
updatedAt: '2023-12-15T14:45:00Z',
63+
checked: false,
64+
},
65+
},
66+
{
67+
name: 'empty calendar',
68+
api: createMockCalendarApi({
69+
id: '',
70+
summary: '',
71+
description: '',
72+
color: '',
73+
user_id: '',
74+
created_at: '',
75+
updated_at: '',
76+
checked: false,
77+
}),
78+
expected: {
79+
id: '',
80+
summary: '',
81+
description: '',
82+
color: '',
83+
userId: '',
84+
createdAt: '',
85+
updatedAt: '',
86+
checked: false,
87+
},
88+
},
89+
])('should create Calendar instance and assign properties correctly: $name', ({ api, expected }) => {
90+
const calendar = new Calendar(api);
91+
92+
expect(calendar.id).toBe(expected.id);
93+
expect(calendar.summary).toBe(expected.summary);
94+
expect(calendar.description).toBe(expected.description);
95+
expect(calendar.color).toBe(expected.color);
96+
expect(calendar.userId).toBe(expected.userId);
97+
expect(calendar.createdAt).toBe(expected.createdAt);
98+
expect(calendar.updatedAt).toBe(expected.updatedAt);
99+
expect(calendar.checked).toBe(expected.checked);
100+
});
101+
});
102+
});
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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

Comments
 (0)