Skip to content

Commit 1dfb7f5

Browse files
committed
adding basic tests for calendar body and utils
1 parent fc6ed79 commit 1dfb7f5

File tree

3 files changed

+221
-15
lines changed

3 files changed

+221
-15
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import CalendarBody from '../CalendarBody';
4+
import { GenericEvent, WeekObject, CalendarBodyProps } from '../types';
5+
import '@testing-library/jest-dom';
6+
// import userEvent from '@testing-library/user-event';
7+
import { describe, it, expect, vi, beforeAll } from 'vitest';
8+
import userEvent from '@testing-library/user-event';
9+
10+
11+
// Setup for the tests
12+
beforeAll(() => {
13+
// Mock matchMedia
14+
window.matchMedia = vi.fn().mockImplementation(query => ({
15+
matches: false,
16+
media: query,
17+
onchange: null,
18+
addListener: vi.fn(),
19+
removeListener: vi.fn(),
20+
addEventListener: vi.fn(),
21+
removeEventListener: vi.fn(),
22+
dispatchEvent: vi.fn(),
23+
}));
24+
25+
// Optional: Mock scrollIntoView if your tests trigger this function
26+
Element.prototype.scrollIntoView = vi.fn();
27+
});
28+
29+
30+
31+
describe('Calendar Component', () => {
32+
const mockWeekDates = {
33+
startDate: new Date('2023-01-01T00:00:00Z'), // This is a Sunday
34+
endDate: new Date('2023-01-07T23:59:59Z'),
35+
};
36+
37+
const mockEvents: GenericEvent[] = [
38+
{
39+
eventId: '1',
40+
startTime: new Date('2023-01-02T10:00:00Z'),
41+
endTime: new Date('2023-01-02T11:00:00Z'),
42+
title: 'Test Event 1'
43+
},
44+
{
45+
eventId: '2',
46+
startTime: new Date('2023-01-03T14:00:00Z'),
47+
endTime: new Date('2023-01-03T15:00:00Z'),
48+
title: 'Test Event 2'
49+
},
50+
];
51+
52+
const mockGetDayEvents: WeekObject<GenericEvent> = {
53+
sunday: [],
54+
monday: [mockEvents[0]],
55+
tuesday: [mockEvents[1]],
56+
wednesday: [],
57+
thursday: [],
58+
friday: [],
59+
saturday: [],
60+
};
61+
62+
const defaultProps: CalendarBodyProps<GenericEvent> = {
63+
weekDatesRange: mockWeekDates,
64+
getDayEvents: mockGetDayEvents,
65+
onEventClick: vi.fn(),
66+
weekends: false,
67+
};
68+
69+
it('renders without errors', () => {
70+
const { getByRole } = render(<CalendarBody {...defaultProps} />);
71+
expect(getByRole('table')).toBeInTheDocument();
72+
});
73+
74+
it('renders correct date labels', () => {
75+
const { getByText } = render(<CalendarBody {...defaultProps} weekends={true} />);
76+
const expectedLabels = ['Hours', 'Mon 02', 'Tue 03', 'Wed 04', 'Thu 05', 'Fri 06', 'Sat 07', 'Sun 08'];
77+
expectedLabels.forEach(label => {
78+
expect(getByText(label)).toBeInTheDocument();
79+
});
80+
});
81+
// currenly this is test is not catching the error
82+
it('renders events when provided', () => {
83+
const { getByText } = render(<CalendarBody {...defaultProps} />);
84+
expect(getByText('Test Event 1')).toBeInTheDocument();
85+
expect(getByText('Test Event 2')).toBeInTheDocument();
86+
});
87+
88+
it('renders events when provided', () => {
89+
const { queryByText } = render(<CalendarBody {...defaultProps} />);
90+
91+
// Check that the events are rendered
92+
expect(queryByText('Test Event 1')).toBeInTheDocument();
93+
expect(queryByText('Test Event 2')).toBeInTheDocument();
94+
95+
// Negative test: Ensure the event elements are not absent
96+
expect(queryByText('Nonexistent Event')).not.toBeInTheDocument();
97+
});
98+
99+
it('calls onEventClick when an event is clicked', async () => {
100+
const user = userEvent.setup();
101+
const onEventClick = vi.fn(); // Use `vi.fn()` instead of `jest.fn()`
102+
103+
const { getByText } = render(<CalendarBody {...defaultProps} onEventClick={onEventClick} />);
104+
105+
const event = getByText('Test Event 1');
106+
await user.click(event);
107+
108+
expect(onEventClick).toHaveBeenCalledWith(mockEvents[0]);
109+
});
110+
111+
});

src/components/__tests__/index.test.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { GenericEvent } from '../types';
2+
import { daysToWeekObject } from '../utils';
3+
import { describe, it, expect } from 'vitest';
4+
5+
// import { isSameDay, isSameWeek, eachDayOfInterval, getDay } from 'date-fns';
6+
7+
describe('daysToWeekObject', () => {
8+
const startWeek = new Date('2024-08-19');
9+
10+
it('should correctly place single-day events into the correct days', () => {
11+
const events: GenericEvent[] = [
12+
{
13+
eventId: '1',
14+
startTime: new Date('2024-08-21T09:00:00'),
15+
endTime: new Date('2024-08-21T10:00:00'),
16+
title: 'Event 1'
17+
},
18+
{
19+
eventId: '2',
20+
startTime: new Date('2024-08-22T11:00:00'),
21+
endTime: new Date('2024-08-22T12:00:00'),
22+
title: 'Event 2'
23+
},
24+
{
25+
eventId: '3',
26+
startTime: new Date('2024-08-23T13:00:00'),
27+
endTime: new Date('2024-08-23T14:00:00'),
28+
title: 'Event 3'
29+
},
30+
];
31+
const result = daysToWeekObject(events, startWeek);
32+
expect(result.wednesday.length).toBe(1);
33+
expect(result.thursday.length).toBe(1);
34+
expect(result.friday.length).toBe(1);
35+
expect(result.wednesday[0].startTime).toEqual(events[0].startTime);
36+
expect(result.thursday[0].startTime).toEqual(events[1].startTime);
37+
expect(result.friday[0].startTime).toEqual(events[2].startTime);
38+
});
39+
40+
it('should correctly split a multi-day event and place it into the correct days', () => {
41+
const events: GenericEvent[] = [
42+
{
43+
eventId: '4',
44+
startTime: new Date('2024-08-21T09:00:00'),
45+
endTime: new Date('2024-08-23T10:00:00'),
46+
title: 'Event 4'
47+
},
48+
{
49+
eventId: '5',
50+
startTime: new Date('2024-08-22T14:00:00'),
51+
endTime: new Date('2024-08-24T15:00:00'),
52+
title: 'Event 5'
53+
},
54+
];
55+
const result = daysToWeekObject(events, startWeek);
56+
expect(result.wednesday.length).toBe(1);
57+
expect(result.thursday.length).toBe(2);
58+
expect(result.friday.length).toBe(2);
59+
expect(result.saturday.length).toBe(1);
60+
});
61+
62+
it('should handle multiple events on the same day correctly', () => {
63+
const events: GenericEvent[] = [
64+
{
65+
eventId: '6',
66+
startTime: new Date('2024-08-22T09:00:00'),
67+
endTime: new Date('2024-08-22T10:00:00'),
68+
title: 'Event 6'
69+
},
70+
{
71+
eventId: '7',
72+
startTime: new Date('2024-08-22T11:00:00'),
73+
endTime: new Date('2024-08-22T12:00:00'),
74+
title: 'Event 7'
75+
},
76+
{
77+
eventId: '8',
78+
startTime: new Date('2024-08-22T13:00:00'),
79+
endTime: new Date('2024-08-22T14:00:00'),
80+
title: 'Event 8'
81+
},
82+
];
83+
const result = daysToWeekObject(events, startWeek);
84+
expect(result.thursday.length).toBe(3);
85+
expect(result.thursday[0].startTime).toEqual(events[0].startTime);
86+
expect(result.thursday[1].startTime).toEqual(events[1].startTime);
87+
expect(result.thursday[2].startTime).toEqual(events[2].startTime);
88+
});
89+
90+
//currently this is failing, I need to verify if there is a bug or this test doesn't make sense
91+
it.skip('should not include events that occur outside of the start week', () => {
92+
const events: GenericEvent[] = [
93+
{
94+
eventId: '9',
95+
startTime: new Date('2024-08-26T09:00:00'),
96+
endTime: new Date('2024-08-26T10:00:00'),
97+
title: 'Event 9'
98+
},
99+
{
100+
eventId: '10',
101+
startTime: new Date('2024-08-25T08:00:00'),
102+
endTime: new Date('2024-08-25T09:00:00'),
103+
title: 'Event 10'
104+
},
105+
];
106+
const result = daysToWeekObject(events, startWeek);
107+
expect(result.monday.length).toBe(0);
108+
expect(result.sunday.length).toBe(0);
109+
});
110+
});

0 commit comments

Comments
 (0)