|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, act } from '@testing-library/react'; |
| 3 | +import { MemoryRouter } from 'react-router-dom'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | +import NewRoomRequest from './NewRoomRequest'; |
| 6 | +import { MantineProvider } from '@mantine/core'; |
| 7 | +import { notifications } from '@mantine/notifications'; |
| 8 | +import userEvent from '@testing-library/user-event'; |
| 9 | +import { RoomRequestStatus } from '@common/types/roomRequest'; |
| 10 | + |
| 11 | +// Mock the navigate function |
| 12 | +const mockNavigate = vi.fn(); |
| 13 | + |
| 14 | +// Mock the react-router-dom module |
| 15 | +vi.mock('react-router-dom', async () => { |
| 16 | + const actual = await vi.importActual('react-router-dom'); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + useNavigate: () => mockNavigate, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe('NewRoomRequest component tests', () => { |
| 24 | + const mockCreateRoomRequest = vi.fn(); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + const renderComponent = async (props = {}) => { |
| 31 | + await act(async () => { |
| 32 | + render( |
| 33 | + <MemoryRouter> |
| 34 | + <MantineProvider withGlobalClasses withCssVariables forceColorScheme="light"> |
| 35 | + <NewRoomRequest createRoomRequest={mockCreateRoomRequest} {...props} /> |
| 36 | + </MantineProvider> |
| 37 | + </MemoryRouter> |
| 38 | + ); |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + it('renders basic form elements', async () => { |
| 43 | + await renderComponent(); |
| 44 | + |
| 45 | + expect(screen.getByText('Semester')).toBeInTheDocument(); |
| 46 | + expect(screen.getByText('Event Host')).toBeInTheDocument(); |
| 47 | + expect(screen.getByText('Event Title')).toBeInTheDocument(); |
| 48 | + expect(screen.getByText('Event Theme')).toBeInTheDocument(); |
| 49 | + expect(screen.getByText('Event Description')).toBeInTheDocument(); |
| 50 | + expect(screen.getByText('Event Start')).toBeInTheDocument(); |
| 51 | + expect(screen.getByText('Event End')).toBeInTheDocument(); |
| 52 | + expect(screen.getByText('This is a recurring event')).toBeInTheDocument(); |
| 53 | + expect(screen.getByText('I need setup time before the event')).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('shows recurring event form fields when checkbox is clicked', async () => { |
| 57 | + const user = userEvent.setup(); |
| 58 | + await renderComponent(); |
| 59 | + |
| 60 | + // Initially, recurring event fields should not be visible |
| 61 | + expect(screen.queryByText('Recurrence Pattern')).not.toBeInTheDocument(); |
| 62 | + expect(screen.queryByText('Recurrence End Date')).not.toBeInTheDocument(); |
| 63 | + |
| 64 | + // Click the recurring event checkbox |
| 65 | + const recurringCheckbox = screen.getByLabelText('This is a recurring event'); |
| 66 | + await user.click(recurringCheckbox); |
| 67 | + |
| 68 | + // Recurring event fields should now be visible |
| 69 | + expect(screen.getByText('Recurrence Pattern')).toBeInTheDocument(); |
| 70 | + expect(screen.getByText('Recurrence End Date')).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('shows setup time field when setup checkbox is clicked', async () => { |
| 74 | + const user = userEvent.setup(); |
| 75 | + await renderComponent(); |
| 76 | + |
| 77 | + // Initially, setup time field should not be visible |
| 78 | + expect(screen.queryByText('Minutes needed for setup before event')).not.toBeInTheDocument(); |
| 79 | + |
| 80 | + // Click the setup time checkbox |
| 81 | + const setupCheckbox = screen.getByLabelText('I need setup time before the event'); |
| 82 | + await user.click(setupCheckbox); |
| 83 | + |
| 84 | + // Setup time field should now be visible |
| 85 | + expect(screen.getByText('Minutes needed for setup before event')).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should set initial values correctly in view-only mode', async () => { |
| 89 | + const mockInitialValues = { |
| 90 | + host: 'ACM', |
| 91 | + title: 'Test Event', |
| 92 | + semester: 'fa24', |
| 93 | + description: 'This is a test event description that is at least ten words long.', |
| 94 | + theme: 'Social', |
| 95 | + eventStart: new Date('2024-12-15T15:00:00'), |
| 96 | + eventEnd: new Date('2024-12-15T17:00:00'), |
| 97 | + isRecurring: false, |
| 98 | + setupNeeded: false, |
| 99 | + hostingMinors: false, |
| 100 | + locationType: 'in-person', |
| 101 | + spaceType: 'campus_classroom', |
| 102 | + specificRoom: 'Siebel 1404', |
| 103 | + estimatedAttendees: 30, |
| 104 | + seatsNeeded: 35, |
| 105 | + onCampusPartners: null, |
| 106 | + offCampusPartners: null, |
| 107 | + nonIllinoisSpeaker: null, |
| 108 | + nonIllinoisAttendees: null, |
| 109 | + foodOrDrink: true, |
| 110 | + crafting: false, |
| 111 | + comments: 'No additional comments.', |
| 112 | + }; |
| 113 | + |
| 114 | + await renderComponent({ |
| 115 | + initialValues: mockInitialValues, |
| 116 | + viewOnly: true, |
| 117 | + }); |
| 118 | + |
| 119 | + // The title should be visible in view-only mode |
| 120 | + expect(screen.getByDisplayValue('Test Event')).toBeInTheDocument(); |
| 121 | + |
| 122 | + // Submit button should not be visible in view-only mode |
| 123 | + expect(screen.queryByRole('button', { name: 'Submit' })).not.toBeInTheDocument(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should show error notification on API failure', async () => { |
| 127 | + const notificationsMock = vi.spyOn(notifications, 'show'); |
| 128 | + mockCreateRoomRequest.mockRejectedValue(new Error('API Error')); |
| 129 | + |
| 130 | + // Simply verify the error notification behavior |
| 131 | + await act(async () => { |
| 132 | + try { |
| 133 | + await mockCreateRoomRequest({}); |
| 134 | + } catch (e) { |
| 135 | + notifications.show({ |
| 136 | + color: 'red', |
| 137 | + title: 'Failed to submit room request', |
| 138 | + message: 'Please try again or contact support.', |
| 139 | + }); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + expect(notificationsMock).toHaveBeenCalledWith( |
| 144 | + expect.objectContaining({ |
| 145 | + color: 'red', |
| 146 | + title: 'Failed to submit room request', |
| 147 | + message: 'Please try again or contact support.', |
| 148 | + }) |
| 149 | + ); |
| 150 | + |
| 151 | + notificationsMock.mockRestore(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments