-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProfile.test.tsx
37 lines (32 loc) · 946 Bytes
/
Profile.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter, Routes, Route } from 'react-router-dom';
import Profile from '../Profile';
describe('[Testing with router] Profile', () => {
beforeEach(() => {
global.fetch = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
});
it('fetches and displays profile data based on URL params', async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: async () => ({
id: 1,
name: 'John Doe',
username: 'john.doe',
email: 'john.doe@example.com',
}),
});
render(
<MemoryRouter initialEntries={['/profile/1']}>
<Routes>
<Route path="/profile/:userId" element={<Profile />} />
</Routes>
</MemoryRouter>
);
await waitFor(() => {
expect(screen.getByText('Username: john.doe')).toBeInTheDocument();
});
});
});