|
1 | | -import { render, screen, waitFor } from '@testing-library/react'; |
| 1 | +import { |
| 2 | + render, |
| 3 | + screen, |
| 4 | + waitFor, |
| 5 | + waitForElementToBeRemoved, |
| 6 | + fireEvent, |
| 7 | +} from '@testing-library/react'; |
2 | 8 | import Leagues from './page'; |
3 | 9 | import { useDataStore } from '@/store/dataStore'; |
4 | | -import { getUserLeagues, cn } from '@/utils/utils'; |
5 | | -import { getGameWeek, getCurrentUserEntries } from '@/api/apiFunctions'; |
| 10 | +import { getUserLeagues } from '@/utils/utils'; |
| 11 | +import { getAllLeagues, addUserToLeague } from '@/api/apiFunctions'; |
| 12 | +import { toast } from 'react-hot-toast'; |
| 13 | +import Alert from '@/components/AlertNotification/AlertNotification'; |
| 14 | +import { AlertVariants } from '@/components/AlertNotification/Alerts.enum'; |
| 15 | + |
| 16 | +const mockUseAuthContext = { |
| 17 | + isSignedIn: false, |
| 18 | +}; |
| 19 | + |
| 20 | +jest.mock('@/context/AuthContextProvider', () => ({ |
| 21 | + useAuthContext() { |
| 22 | + return { |
| 23 | + ...mockUseAuthContext, |
| 24 | + }; |
| 25 | + }, |
| 26 | +})); |
6 | 27 |
|
7 | 28 | jest.mock('@/store/dataStore', () => ({ |
8 | | - useDataStore: jest.fn(), |
| 29 | + useDataStore: jest.fn(() => ({ |
| 30 | + user: { |
| 31 | + documentId: '123', |
| 32 | + id: '1234', |
| 33 | + |
| 34 | + leagues: ['league1'], |
| 35 | + }, |
| 36 | + allLeagues: [ |
| 37 | + { |
| 38 | + leagueId: '123', |
| 39 | + leagueName: 'Test League', |
| 40 | + logo: 'logo.png', |
| 41 | + participants: ['123456', '78'], |
| 42 | + survivors: ['123456', '78'], |
| 43 | + }, |
| 44 | + ], |
| 45 | + updateUser: jest.fn(), |
| 46 | + })), |
9 | 47 | })); |
10 | 48 |
|
11 | 49 | jest.mock('@/utils/utils', () => ({ |
12 | | - getUserLeagues: jest.fn(), |
| 50 | + getUserLeagues: jest.fn(() => Promise.resolve([])), |
13 | 51 | cn: jest.fn(), |
14 | 52 | })); |
15 | 53 |
|
16 | 54 | jest.mock('@/api/apiFunctions', () => ({ |
17 | | - getGameWeek: jest.fn(), |
18 | | - getCurrentUserEntries: jest.fn(), |
| 55 | + getAllLeagues: jest.fn(), |
| 56 | + addUserToLeague: jest.fn(), |
19 | 57 | })); |
20 | 58 |
|
21 | | -const mockLeagues = [ |
22 | | - { |
23 | | - leagueId: '123', |
24 | | - leagueName: 'Test League', |
25 | | - logo: 'https://findmylogo.com/logo.png', |
26 | | - participants: ['123456', '78'], |
27 | | - survivors: ['123456', '78', '9'], |
28 | | - }, |
29 | | -]; |
30 | | - |
31 | | -const mockEntries = [ |
32 | | - { |
33 | | - $id: '123', |
34 | | - name: 'Test Entry', |
35 | | - user: '123', |
36 | | - league: '123', |
37 | | - selectedTeams: [], |
38 | | - eliminated: false, |
| 59 | +jest.mock('react-hot-toast', () => ({ |
| 60 | + toast: { |
| 61 | + custom: jest.fn(), |
39 | 62 | }, |
40 | | -]; |
41 | | - |
42 | | -const mockGameWeek = { |
43 | | - id: '123', |
44 | | - week: 1, |
45 | | -}; |
46 | | - |
47 | | -const mockUseDataStore = useDataStore as jest.MockedFunction< |
48 | | - typeof useDataStore |
49 | | ->; |
50 | | -const mockGetUserLeagues = getUserLeagues as jest.MockedFunction< |
51 | | - typeof getUserLeagues |
52 | | ->; |
53 | | -const mockGetGameWeek = getGameWeek as jest.MockedFunction<typeof getGameWeek>; |
54 | | -const mockGetCurrentUserEntries = getCurrentUserEntries as jest.MockedFunction< |
55 | | - typeof getCurrentUserEntries |
56 | | ->; |
| 63 | +})); |
57 | 64 |
|
58 | 65 | describe('Leagues Component', () => { |
59 | | - const mockUpdateLeagues = jest.fn(); |
60 | | - const mockUpdateGameWeek = jest.fn(); |
61 | | - const mockUpdateEntries = jest.fn(); |
62 | | - const mockCn = cn as jest.MockedFunction<typeof cn>; |
| 66 | + const mockUseDataStore = useDataStore as unknown as jest.Mock; |
| 67 | + const mockGetUserLeagues = getUserLeagues as jest.Mock; |
| 68 | + const mockGetAllLeagues = getAllLeagues as jest.Mock; |
| 69 | + const mockAddUserToLeague = addUserToLeague as jest.Mock; |
63 | 70 |
|
64 | 71 | beforeEach(() => { |
65 | 72 | jest.clearAllMocks(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should render "You are not enrolled in any leagues" message when no leagues are found', async () => { |
| 76 | + mockUseAuthContext.isSignedIn = true; |
66 | 77 | mockUseDataStore.mockReturnValue({ |
67 | | - user: { id: '123', leagues: [] }, |
68 | | - leagues: [], |
69 | | - updateLeagues: mockUpdateLeagues, |
70 | | - updateGameWeek: mockUpdateGameWeek, |
71 | | - updateEntries: mockUpdateEntries, |
| 78 | + user: { |
| 79 | + documentId: '123', |
| 80 | + |
| 81 | + id: '123', |
| 82 | + leagues: [], |
| 83 | + }, |
| 84 | + allLeagues: [], |
72 | 85 | }); |
73 | | - }); |
74 | 86 |
|
75 | | - test('should display GlobalSpinner while loading data', async () => { |
76 | 87 | render(<Leagues />); |
77 | 88 |
|
78 | | - expect(screen.getByTestId('global-spinner')).toBeInTheDocument(); |
| 89 | + await waitForElementToBeRemoved(() => screen.getByTestId('global-spinner')); |
79 | 90 |
|
80 | 91 | await waitFor(() => { |
81 | | - expect(mockGetUserLeagues).toHaveBeenCalled(); |
| 92 | + const messageElement = screen.getByTestId('no-leagues-message'); |
| 93 | + expect(messageElement).toBeInTheDocument(); |
82 | 94 | }); |
83 | 95 | }); |
84 | 96 |
|
85 | | - test('should render "You are not enrolled in any leagues" message when no leagues are found', async () => { |
86 | | - mockGetUserLeagues.mockResolvedValue([]); |
87 | | - |
88 | | - render(<Leagues />); |
| 97 | + it('should display GlobalSpinner while loading data', async () => { |
| 98 | + mockUseAuthContext.isSignedIn = true; |
89 | 99 |
|
90 | | - await waitFor(() => { |
91 | | - expect(mockGetUserLeagues).toHaveBeenCalled(); |
| 100 | + mockUseDataStore.mockReturnValueOnce({ |
| 101 | + user: { |
| 102 | + documentId: '123', |
| 103 | + |
| 104 | + id: '123', |
| 105 | + leagues: [], |
| 106 | + }, |
| 107 | + allLeagues: [], |
92 | 108 | }); |
93 | 109 |
|
94 | | - expect( |
95 | | - screen.getByText('You are not enrolled in any leagues'), |
96 | | - ).toBeInTheDocument(); |
97 | | - }); |
98 | | - |
99 | | - test('should not call getGameWeek or getCurrentUserEntries if no leagues are found', async () => { |
100 | | - mockGetUserLeagues.mockResolvedValue([]); |
101 | | - |
102 | 110 | render(<Leagues />); |
103 | 111 |
|
104 | | - await waitFor(() => { |
105 | | - expect(mockGetUserLeagues).toHaveBeenCalled(); |
106 | | - expect(mockGetGameWeek).not.toHaveBeenCalled(); |
107 | | - expect(mockGetCurrentUserEntries).not.toHaveBeenCalled(); |
108 | | - }); |
| 112 | + expect(screen.getByTestId('global-spinner')).toBeInTheDocument(); |
109 | 113 | }); |
110 | 114 |
|
111 | | - test('should not display GlobalSpinner after loading data', async () => { |
| 115 | + it('should not display GlobalSpinner after loading data', async () => { |
| 116 | + mockUseAuthContext.isSignedIn = true; |
| 117 | + |
112 | 118 | mockUseDataStore.mockReturnValue({ |
113 | | - user: { id: '123', leagues: ['123'] }, |
114 | | - leagues: mockLeagues, |
115 | | - updateLeagues: mockUpdateLeagues, |
116 | | - updateGameWeek: mockUpdateGameWeek, |
117 | | - updateEntries: mockUpdateEntries, |
| 119 | + user: { |
| 120 | + documentId: '123', |
| 121 | + |
| 122 | + id: '123', |
| 123 | + leagues: [], |
| 124 | + }, |
| 125 | + allLeagues: [ |
| 126 | + { |
| 127 | + leagueId: '123', |
| 128 | + leagueName: 'Test League', |
| 129 | + logo: 'logo.png', |
| 130 | + participants: ['123456', '78'], |
| 131 | + survivors: ['123456', '78'], |
| 132 | + }, |
| 133 | + ], |
118 | 134 | }); |
119 | | - mockGetUserLeagues.mockResolvedValue(mockLeagues); |
120 | | - mockGetGameWeek.mockResolvedValue(mockGameWeek); |
121 | | - mockGetCurrentUserEntries.mockResolvedValue(mockEntries); |
122 | 135 |
|
123 | | - render(<Leagues />); |
| 136 | + mockGetUserLeagues.mockResolvedValueOnce([]); |
124 | 137 |
|
125 | | - expect(screen.getByTestId('global-spinner')).toBeInTheDocument(); |
| 138 | + render(<Leagues />); |
126 | 139 |
|
127 | | - await waitFor(() => { |
128 | | - expect(mockGetUserLeagues).toHaveBeenCalled(); |
129 | | - expect(mockGetGameWeek).toHaveBeenCalled(); |
130 | | - expect(mockGetCurrentUserEntries).toHaveBeenCalled(); |
131 | | - }); |
| 140 | + await waitForElementToBeRemoved(() => screen.getByTestId('global-spinner')); |
132 | 141 |
|
133 | 142 | expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument(); |
134 | 143 | }); |
135 | | - test('should display "Test League" when leagues are found', async () => { |
| 144 | + |
| 145 | + it('should handle form submission to join a league', async () => { |
| 146 | + mockUseAuthContext.isSignedIn = true; |
| 147 | + |
| 148 | + const user = { |
| 149 | + documentId: '123', |
| 150 | + |
| 151 | + id: '123', |
| 152 | + leagues: [], |
| 153 | + }; |
| 154 | + |
| 155 | + const league = { |
| 156 | + leagueId: '123', |
| 157 | + leagueName: 'Test League', |
| 158 | + logo: 'logo.png', |
| 159 | + participants: [], |
| 160 | + survivors: [], |
| 161 | + }; |
| 162 | + |
| 163 | + const updateUser = jest.fn(); |
| 164 | + |
136 | 165 | mockUseDataStore.mockReturnValue({ |
137 | | - user: { id: '123', leagues: ['123'] }, |
138 | | - leagues: mockLeagues, |
139 | | - updateLeagues: mockUpdateLeagues, |
140 | | - updateGameWeek: mockUpdateGameWeek, |
141 | | - updateEntries: mockUpdateEntries, |
| 166 | + user, |
| 167 | + allLeagues: [league], |
| 168 | + updateUser, |
142 | 169 | }); |
143 | | - mockGetUserLeagues.mockResolvedValue(mockLeagues); |
144 | | - mockGetGameWeek.mockResolvedValue(mockGameWeek); |
145 | | - mockGetCurrentUserEntries.mockResolvedValue(mockEntries); |
| 170 | + |
| 171 | + mockGetAllLeagues.mockResolvedValueOnce([league]); |
| 172 | + mockAddUserToLeague.mockResolvedValue( |
| 173 | + Promise.resolve({ |
| 174 | + userDocumentId: user.documentId, |
| 175 | + selectedLeague: league.leagueId, |
| 176 | + selectedLeagues: [league.leagueId], |
| 177 | + participants: [user.id], |
| 178 | + survivors: [user.id], |
| 179 | + }), |
| 180 | + ); |
146 | 181 |
|
147 | 182 | render(<Leagues />); |
148 | 183 |
|
149 | 184 | await waitFor(() => { |
150 | | - expect(mockGetUserLeagues).toHaveBeenCalled(); |
151 | | - expect(mockGetGameWeek).toHaveBeenCalled(); |
152 | | - expect(mockGetCurrentUserEntries).toHaveBeenCalled(); |
| 185 | + expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument(); |
153 | 186 | }); |
154 | 187 |
|
155 | | - expect(mockUpdateGameWeek).toHaveBeenCalledWith(mockGameWeek); |
156 | | - expect(mockUpdateEntries).toHaveBeenCalledWith(mockEntries); |
| 188 | + const selectElement = screen.getByTestId('select-available-leagues'); |
| 189 | + fireEvent.change(selectElement, { target: { value: '123' } }); |
| 190 | + fireEvent.click(screen.getByTestId('join-league-button')); |
157 | 191 |
|
158 | | - expect(screen.getByText('Test League')).toBeInTheDocument(); |
| 192 | + await waitFor(() => { |
| 193 | + expect(mockAddUserToLeague).toHaveBeenCalledWith({ |
| 194 | + userDocumentId: user.documentId, |
| 195 | + selectedLeague: league.leagueId, |
| 196 | + selectedLeagues: [league.leagueId], |
| 197 | + participants: [user.id], |
| 198 | + survivors: [user.id], |
| 199 | + }); |
| 200 | + expect(updateUser).toHaveBeenCalledWith( |
| 201 | + user.documentId, |
| 202 | + user.id, |
| 203 | + user.email, |
| 204 | + [...user.leagues, league.leagueId], |
| 205 | + ); |
| 206 | + expect(toast.custom).toHaveBeenCalledWith( |
| 207 | + <Alert |
| 208 | + variant={AlertVariants.Success} |
| 209 | + message={`Added ${league.leagueName} to your leagues!`} |
| 210 | + />, |
| 211 | + ); |
| 212 | + }); |
159 | 213 | }); |
160 | | - test('should call getGameWeek and getCurrentUserEntries if leagues are found', async () => { |
| 214 | + |
| 215 | + it('should show error if adding to league fails', async () => { |
| 216 | + mockUseAuthContext.isSignedIn = true; |
| 217 | + |
| 218 | + const user = { |
| 219 | + documentId: '123', |
| 220 | + |
| 221 | + id: '123', |
| 222 | + leagues: [], |
| 223 | + }; |
| 224 | + |
| 225 | + const league = { |
| 226 | + leagueId: '123', |
| 227 | + leagueName: 'Test League', |
| 228 | + logo: 'logo.png', |
| 229 | + participants: [], |
| 230 | + survivors: [], |
| 231 | + }; |
| 232 | + |
161 | 233 | mockUseDataStore.mockReturnValue({ |
162 | | - user: { id: '123', leagues: ['123'] }, |
163 | | - leagues: mockLeagues, |
164 | | - updateLeagues: mockUpdateLeagues, |
165 | | - updateGameWeek: mockUpdateGameWeek, |
166 | | - updateEntries: mockUpdateEntries, |
| 234 | + user, |
| 235 | + allLeagues: [league], |
167 | 236 | }); |
168 | | - mockGetUserLeagues.mockResolvedValue(mockLeagues); |
169 | | - mockGetGameWeek.mockResolvedValue(mockGameWeek); |
170 | | - mockGetCurrentUserEntries.mockResolvedValue(mockEntries); |
| 237 | + |
| 238 | + mockGetUserLeagues.mockResolvedValueOnce([]); |
| 239 | + mockGetAllLeagues.mockResolvedValueOnce([league]); |
| 240 | + mockAddUserToLeague.mockResolvedValue( |
| 241 | + Promise.resolve({ |
| 242 | + userDocumentId: user.documentId, |
| 243 | + selectedLeague: league.leagueId, |
| 244 | + selectedLeagues: [league.leagueId], |
| 245 | + participants: [user.id], |
| 246 | + survivors: [user.id], |
| 247 | + }), |
| 248 | + ); |
171 | 249 |
|
172 | 250 | render(<Leagues />); |
173 | 251 |
|
174 | 252 | await waitFor(() => { |
175 | | - expect(mockGetUserLeagues).toHaveBeenCalled(); |
176 | | - expect(mockGetGameWeek).toHaveBeenCalled(); |
177 | | - expect(mockGetCurrentUserEntries).toHaveBeenCalled(); |
| 253 | + expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument(); |
178 | 254 | }); |
179 | 255 |
|
180 | | - expect(mockUpdateGameWeek).toHaveBeenCalledWith(mockGameWeek); |
181 | | - expect(mockUpdateEntries).toHaveBeenCalledWith(mockEntries); |
| 256 | + const selectElement = screen.getByTestId('select-available-leagues'); |
| 257 | + fireEvent.change(selectElement, { target: { value: '123' } }); |
| 258 | + fireEvent.click(screen.getByTestId('join-league-button')); |
| 259 | + |
| 260 | + await waitFor(() => { |
| 261 | + expect(mockAddUserToLeague).toHaveBeenCalledWith({ |
| 262 | + userDocumentId: user.documentId, |
| 263 | + selectedLeague: league.leagueId, |
| 264 | + selectedLeagues: [league.leagueId], |
| 265 | + participants: [user.id], |
| 266 | + survivors: [user.id], |
| 267 | + }); |
| 268 | + |
| 269 | + expect(toast.custom).toHaveBeenCalledWith( |
| 270 | + <Alert |
| 271 | + variant={AlertVariants.Error} |
| 272 | + message="Failed to add the league. Please try again." |
| 273 | + />, |
| 274 | + ); |
| 275 | + }); |
182 | 276 | }); |
183 | 277 | }); |
0 commit comments