|
| 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 | +}); |
0 commit comments