|
| 1 | +import React, { act } from 'react'; |
| 2 | +import type { RenderResult } from '@testing-library/react'; |
| 3 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import { MockedProvider } from '@apollo/react-testing'; |
| 5 | +import { I18nextProvider } from 'react-i18next'; |
| 6 | +import i18nForTest from 'utils/i18nForTest'; |
| 7 | +import { MemoryRouter, Route, Routes, useParams } from 'react-router-dom'; |
| 8 | +import { Provider } from 'react-redux'; |
| 9 | +import { store } from 'state/store'; |
| 10 | +import EventManagement from './EventManagement'; |
| 11 | +import userEvent from '@testing-library/user-event'; |
| 12 | +import { StaticMockLink } from 'utils/StaticMockLink'; |
| 13 | +import { MOCKS_WITH_TIME } from 'components/EventManagement/Dashboard/EventDashboard.mocks'; |
| 14 | +import useLocalStorage from 'utils/useLocalstorage'; |
| 15 | +import { vi } from 'vitest'; |
| 16 | +const { setItem } = useLocalStorage(); |
| 17 | + |
| 18 | +const mockWithTime = new StaticMockLink(MOCKS_WITH_TIME, true); |
| 19 | + |
| 20 | +const renderEventManagement = (): RenderResult => { |
| 21 | + return render( |
| 22 | + <MockedProvider |
| 23 | + addTypename={false} |
| 24 | + link={mockWithTime} |
| 25 | + mocks={MOCKS_WITH_TIME} |
| 26 | + > |
| 27 | + <MemoryRouter initialEntries={['/event/orgId/eventId']}> |
| 28 | + <Provider store={store}> |
| 29 | + <I18nextProvider i18n={i18nForTest}> |
| 30 | + <Routes> |
| 31 | + <Route |
| 32 | + path="/event/:orgId/:eventId" |
| 33 | + element={<EventManagement />} |
| 34 | + /> |
| 35 | + <Route |
| 36 | + path="/orglist" |
| 37 | + element={<div data-testid="paramsError">paramsError</div>} |
| 38 | + /> |
| 39 | + <Route |
| 40 | + path="/orgevents/:orgId" |
| 41 | + element={<div data-testid="eventsScreen">eventsScreen</div>} |
| 42 | + /> |
| 43 | + <Route |
| 44 | + path="/user/events/:orgId" |
| 45 | + element={ |
| 46 | + <div data-testid="userEventsScreen">userEventsScreen</div> |
| 47 | + } |
| 48 | + /> |
| 49 | + </Routes> |
| 50 | + </I18nextProvider> |
| 51 | + </Provider> |
| 52 | + </MemoryRouter> |
| 53 | + </MockedProvider>, |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +describe('Event Management', () => { |
| 58 | + beforeAll(() => { |
| 59 | + vi.mock('react-router-dom', async () => { |
| 60 | + const actual = await vi.importActual('react-router-dom'); |
| 61 | + return { |
| 62 | + ...actual, |
| 63 | + useParams: vi.fn(), |
| 64 | + }; |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + vi.clearAllMocks(); |
| 70 | + localStorage.clear(); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('Navigation Tests', () => { |
| 74 | + beforeEach(() => { |
| 75 | + vi.mocked(useParams).mockReturnValue({ |
| 76 | + orgId: 'orgId', |
| 77 | + eventId: 'eventId', |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('Testing back button navigation when userType is SuperAdmin', async () => { |
| 82 | + setItem('SuperAdmin', true); |
| 83 | + renderEventManagement(); |
| 84 | + |
| 85 | + const backButton = screen.getByTestId('backBtn'); |
| 86 | + userEvent.click(backButton); |
| 87 | + await waitFor(() => { |
| 88 | + const eventsScreen = screen.getByTestId('eventsScreen'); |
| 89 | + expect(eventsScreen).toBeInTheDocument(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('Testing back button navigation when userType is USER', async () => { |
| 94 | + setItem('SuperAdmin', false); |
| 95 | + setItem('AdminFor', []); |
| 96 | + |
| 97 | + renderEventManagement(); |
| 98 | + |
| 99 | + const backButton = screen.getByTestId('backBtn'); |
| 100 | + userEvent.click(backButton); |
| 101 | + |
| 102 | + await waitFor(() => { |
| 103 | + const userEventsScreen = screen.getByTestId('userEventsScreen'); |
| 104 | + expect(userEventsScreen).toBeInTheDocument(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Testing back button navigation when userType is ADMIN', async () => { |
| 109 | + setItem('SuperAdmin', false); |
| 110 | + setItem('AdminFor', ['someOrg']); |
| 111 | + |
| 112 | + renderEventManagement(); |
| 113 | + |
| 114 | + const backButton = screen.getByTestId('backBtn'); |
| 115 | + userEvent.click(backButton); |
| 116 | + |
| 117 | + await waitFor(() => { |
| 118 | + const eventsScreen = screen.getByTestId('eventsScreen'); |
| 119 | + expect(eventsScreen).toBeInTheDocument(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + it('redirects to orglist when params are missing', async () => { |
| 123 | + vi.mocked(useParams).mockReturnValue({}); |
| 124 | + |
| 125 | + renderEventManagement(); |
| 126 | + |
| 127 | + await waitFor(() => { |
| 128 | + const paramsError = screen.getByTestId('paramsError'); |
| 129 | + expect(paramsError).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('Tab Management Tests', () => { |
| 135 | + beforeEach(() => { |
| 136 | + vi.mocked(useParams).mockReturnValue({ |
| 137 | + orgId: 'orgId', |
| 138 | + eventId: 'event123', |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('renders dashboard tab by default', async () => { |
| 143 | + renderEventManagement(); |
| 144 | + expect(screen.getByTestId('eventDashboardTab')).toBeInTheDocument(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('switches between all available tabs', async () => { |
| 148 | + renderEventManagement(); |
| 149 | + |
| 150 | + const tabsToTest = [ |
| 151 | + { button: 'registrantsBtn', tab: 'eventRegistrantsTab' }, |
| 152 | + { button: 'attendanceBtn', tab: 'eventAttendanceTab' }, |
| 153 | + { button: 'actionsBtn', tab: 'eventActionsTab' }, |
| 154 | + { button: 'agendasBtn', tab: 'eventAgendasTab' }, |
| 155 | + { button: 'statisticsBtn', tab: 'eventStatsTab' }, |
| 156 | + { button: 'volunteersBtn', tab: 'eventVolunteersTab' }, |
| 157 | + ]; |
| 158 | + |
| 159 | + for (const { button, tab } of tabsToTest) { |
| 160 | + userEvent.click(screen.getByTestId(button)); |
| 161 | + expect(screen.getByTestId(tab)).toBeInTheDocument(); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + it('returns dashboard tab for an invalid tab selection', async () => { |
| 166 | + const setTab = vi.fn(); |
| 167 | + const useStateSpy = vi.spyOn(React, 'useState'); |
| 168 | + useStateSpy.mockReturnValueOnce(['invalid', setTab]); |
| 169 | + await act(async () => { |
| 170 | + renderEventManagement(); |
| 171 | + }); |
| 172 | + |
| 173 | + expect(screen.queryByTestId('eventDashboardTab')).toBeInTheDocument(); |
| 174 | + expect( |
| 175 | + screen.queryByTestId('eventRegistrantsTab'), |
| 176 | + ).not.toBeInTheDocument(); |
| 177 | + expect( |
| 178 | + screen.queryByTestId('eventAttendanceTab'), |
| 179 | + ).not.toBeInTheDocument(); |
| 180 | + expect(screen.queryByTestId('eventActionsTab')).not.toBeInTheDocument(); |
| 181 | + expect( |
| 182 | + screen.queryByTestId('eventVolunteersTab'), |
| 183 | + ).not.toBeInTheDocument(); |
| 184 | + expect(screen.queryByTestId('eventAgendasTab')).not.toBeInTheDocument(); |
| 185 | + expect(screen.queryByTestId('eventStatsTab')).not.toBeInTheDocument(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('Responsive Dropdown Tests', () => { |
| 190 | + beforeEach(() => { |
| 191 | + vi.mocked(useParams).mockReturnValue({ |
| 192 | + orgId: 'orgId', |
| 193 | + eventId: 'event123', |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + it('renders dropdown with all options', async () => { |
| 198 | + await act(async () => { |
| 199 | + renderEventManagement(); |
| 200 | + }); |
| 201 | + |
| 202 | + const dropdownContainer = screen.getByTestId('tabsDropdownContainer'); |
| 203 | + expect(dropdownContainer).toBeInTheDocument(); |
| 204 | + |
| 205 | + await act(async () => { |
| 206 | + userEvent.click(screen.getByTestId('tabsDropdownToggle')); |
| 207 | + }); |
| 208 | + |
| 209 | + const tabOptions = [ |
| 210 | + 'dashboard', |
| 211 | + 'registrants', |
| 212 | + 'attendance', |
| 213 | + 'agendas', |
| 214 | + 'actions', |
| 215 | + 'volunteers', |
| 216 | + 'statistics', |
| 217 | + ]; |
| 218 | + |
| 219 | + tabOptions.forEach((option) => { |
| 220 | + expect(screen.getByTestId(`${option}DropdownItem`)).toBeInTheDocument(); |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + it('switches tabs through dropdown selection', async () => { |
| 225 | + await act(async () => { |
| 226 | + renderEventManagement(); |
| 227 | + }); |
| 228 | + await act(async () => { |
| 229 | + userEvent.click(screen.getByTestId('tabsDropdownToggle')); |
| 230 | + }); |
| 231 | + |
| 232 | + const tabOptions = [ |
| 233 | + 'dashboard', |
| 234 | + 'registrants', |
| 235 | + 'attendance', |
| 236 | + 'agendas', |
| 237 | + 'actions', |
| 238 | + 'volunteers', |
| 239 | + 'statistics', |
| 240 | + ]; |
| 241 | + |
| 242 | + for (const option of tabOptions) { |
| 243 | + act(() => { |
| 244 | + userEvent.click(screen.getByTestId(`${option}DropdownItem`)); |
| 245 | + }); |
| 246 | + |
| 247 | + expect(screen.getByTestId(`${option}DropdownItem`)).toHaveClass( |
| 248 | + 'text-secondary', |
| 249 | + ); |
| 250 | + } |
| 251 | + }); |
| 252 | + }); |
| 253 | +}); |
0 commit comments