Skip to content

Commit f268641

Browse files
committed
add UserProfile tests for rendering without playback history and handling fetch errors
1 parent 40a6582 commit f268641

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

Electron/src/__tests__/pages/UserProfile.test.tsx

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,111 @@ test('UserProfile Artist load Songs and total streams', async () => {
243243
component.queryByText(`${songMockFetch.streams} reproducciones totales`),
244244
).toBeInTheDocument();
245245
});
246+
247+
test('UserProfile renders correctly without playback history or playlists', async () => {
248+
const userMockFetch = {
249+
name: 'name',
250+
photo: 'photo',
251+
register_date: 'date',
252+
password: 'hashpassword',
253+
playback_history: [],
254+
playlists: [],
255+
saved_playlists: [],
256+
};
257+
258+
jest.mock('react-router-dom', () => ({
259+
...jest.requireActual('react-router-dom'),
260+
useParams: () => ({
261+
id: userMockFetch.name,
262+
}),
263+
}));
264+
265+
global.fetch = jest.fn((url: string) => {
266+
if (url === `${Global.backendBaseUrl}/users/${userMockFetch.name}`) {
267+
return Promise.resolve({
268+
json: () => userMockFetch,
269+
status: 200,
270+
ok: true,
271+
headers: getMockHeaders(),
272+
}).catch((error) => {
273+
console.log(error);
274+
});
275+
}
276+
277+
// In case the URL doesn't match, return a rejected promise
278+
return Promise.reject(new Error(`Unhandled URL in fetch mock: ${url}`));
279+
}) as jest.Mock;
280+
281+
const component = await act(() => {
282+
return render(
283+
<MemoryRouter initialEntries={[`/user/${userMockFetch.name}`]}>
284+
<NowPlayingContextProvider>
285+
<Routes>
286+
<Route
287+
path="/user/:id"
288+
element={
289+
<UserProfile
290+
refreshSidebarData={jest.fn()}
291+
userType={UserType.USER}
292+
/>
293+
}
294+
/>
295+
</Routes>
296+
</NowPlayingContextProvider>
297+
</MemoryRouter>,
298+
);
299+
});
300+
301+
const elementsWithUserName = component.getAllByText(userMockFetch.name);
302+
303+
expect(elementsWithUserName.length).toBeGreaterThan(0);
304+
expect(
305+
component.queryByText('Historial de reproducción'),
306+
).not.toBeInTheDocument();
307+
expect(component.queryByText('playlisttest')).not.toBeInTheDocument();
308+
});
309+
310+
test('UserProfile handles fetch errors gracefully', async () => {
311+
const userMockFetch = {
312+
name: 'name',
313+
photo: 'photo',
314+
register_date: 'date',
315+
password: 'hashpassword',
316+
playback_history: [],
317+
playlists: [],
318+
saved_playlists: [],
319+
};
320+
321+
jest.mock('react-router-dom', () => ({
322+
...jest.requireActual('react-router-dom'),
323+
useParams: () => ({
324+
id: userMockFetch.name,
325+
}),
326+
}));
327+
328+
global.fetch = jest.fn((url: string) => {
329+
return Promise.reject(new Error('Fetch error'));
330+
}) as jest.Mock;
331+
332+
const component = await act(() => {
333+
return render(
334+
<MemoryRouter initialEntries={[`/user/${userMockFetch.name}`]}>
335+
<NowPlayingContextProvider>
336+
<Routes>
337+
<Route
338+
path="/user/:id"
339+
element={
340+
<UserProfile
341+
refreshSidebarData={jest.fn()}
342+
userType={UserType.USER}
343+
/>
344+
}
345+
/>
346+
</Routes>
347+
</NowPlayingContextProvider>
348+
</MemoryRouter>,
349+
);
350+
});
351+
352+
expect(component.queryByText('Fetch error')).toBeInTheDocument();
353+
});

0 commit comments

Comments
 (0)