|
| 1 | +import { act, render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { Link, MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 4 | + |
| 5 | +import UserProfile from './index'; |
| 6 | +import { User } from '../../shared/models'; |
| 7 | +import { hackerNewsApi } from '../../shared/services/hackernews-api'; |
| 8 | + |
| 9 | +const mockNavigate = vi.hoisted(() => vi.fn()); |
| 10 | + |
| 11 | +vi.mock('react-router-dom', async () => ({ |
| 12 | + ...(await vi.importActual<typeof import('react-router-dom')>('react-router-dom')), |
| 13 | + useNavigate: () => mockNavigate, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../../shared/services/hackernews-api', () => ({ |
| 17 | + hackerNewsApi: { |
| 18 | + fetchUser: vi.fn(), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +function makeUser(id = 'a', about = ''): User { |
| 23 | + return { |
| 24 | + id, |
| 25 | + crated_time: 1672531200, |
| 26 | + created: '2 years ago', |
| 27 | + karma: 123, |
| 28 | + avg: 4.5, |
| 29 | + about, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function deferred<T>() { |
| 34 | + let resolve!: (value: T | PromiseLike<T>) => void; |
| 35 | + let reject!: (reason?: unknown) => void; |
| 36 | + const promise = new Promise<T>((promiseResolve, promiseReject) => { |
| 37 | + resolve = promiseResolve; |
| 38 | + reject = promiseReject; |
| 39 | + }); |
| 40 | + |
| 41 | + return { promise, resolve, reject }; |
| 42 | +} |
| 43 | + |
| 44 | +function renderUser(initialEntries = ['/user/a'], initialIndex = 0, withNavigationLink = false) { |
| 45 | + return render( |
| 46 | + <MemoryRouter initialEntries={initialEntries} initialIndex={initialIndex}> |
| 47 | + <Routes> |
| 48 | + <Route |
| 49 | + path="/user/:id" |
| 50 | + element={ |
| 51 | + <> |
| 52 | + <UserProfile /> |
| 53 | + {withNavigationLink && <Link to="/user/b">go</Link>} |
| 54 | + </> |
| 55 | + } |
| 56 | + /> |
| 57 | + </Routes> |
| 58 | + </MemoryRouter> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +describe('UserProfile', () => { |
| 63 | + beforeEach(() => { |
| 64 | + vi.mocked(hackerNewsApi.fetchUser).mockReset(); |
| 65 | + mockNavigate.mockReset(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('shows loading while the user request is pending', () => { |
| 69 | + vi.mocked(hackerNewsApi.fetchUser).mockReturnValue(new Promise<User>(() => undefined)); |
| 70 | + |
| 71 | + renderUser(); |
| 72 | + |
| 73 | + expect(screen.getByText('Loading...')).toBeInTheDocument(); |
| 74 | + expect(document.querySelector('.profile')).not.toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('shows an error when the user request rejects', async () => { |
| 78 | + vi.mocked(hackerNewsApi.fetchUser).mockRejectedValueOnce(new Error('request failed')); |
| 79 | + |
| 80 | + renderUser(); |
| 81 | + |
| 82 | + expect(await screen.findByText('Could not load user a.')).toBeInTheDocument(); |
| 83 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 84 | + expect(document.querySelector('.profile')).not.toBeInTheDocument(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('renders the user profile', async () => { |
| 88 | + vi.mocked(hackerNewsApi.fetchUser).mockResolvedValueOnce(makeUser()); |
| 89 | + |
| 90 | + renderUser(); |
| 91 | + |
| 92 | + expect(await screen.findByText('a', { selector: '.name' })).toBeInTheDocument(); |
| 93 | + expect(screen.getByText('123 ★')).toHaveClass('right'); |
| 94 | + expect(screen.getByText('Created 2 years ago')).toHaveClass('age'); |
| 95 | + expect(screen.getByText('Profile: a')).toHaveClass('title-block'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('renders HTML in the about section', async () => { |
| 99 | + vi.mocked(hackerNewsApi.fetchUser).mockResolvedValueOnce(makeUser('a', '<b>hello</b> <pre>x</pre>')); |
| 100 | + |
| 101 | + renderUser(); |
| 102 | + |
| 103 | + await screen.findByText('a', { selector: '.name' }); |
| 104 | + |
| 105 | + const about = document.querySelector('.other-details p'); |
| 106 | + expect(about).not.toBeNull(); |
| 107 | + expect(about!.innerHTML).toBe('<b>hello</b> <pre>x</pre>'); |
| 108 | + expect(about!.querySelector('b')).toBeInTheDocument(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('does not render an about section for an empty string', async () => { |
| 112 | + vi.mocked(hackerNewsApi.fetchUser).mockResolvedValueOnce(makeUser('a', '')); |
| 113 | + |
| 114 | + renderUser(); |
| 115 | + |
| 116 | + await screen.findByText('a', { selector: '.name' }); |
| 117 | + |
| 118 | + expect(document.querySelector('.other-details')).not.toBeInTheDocument(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('does not render an about section when about is undefined', async () => { |
| 122 | + vi.mocked(hackerNewsApi.fetchUser).mockResolvedValueOnce({ |
| 123 | + ...makeUser(), |
| 124 | + about: undefined, |
| 125 | + } as unknown as User); |
| 126 | + |
| 127 | + renderUser(); |
| 128 | + |
| 129 | + await screen.findByText('a', { selector: '.name' }); |
| 130 | + |
| 131 | + expect(document.querySelector('.other-details')).not.toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('navigates back when the back button is clicked', async () => { |
| 135 | + vi.mocked(hackerNewsApi.fetchUser).mockResolvedValueOnce(makeUser()); |
| 136 | + const user = userEvent.setup(); |
| 137 | + |
| 138 | + renderUser(); |
| 139 | + |
| 140 | + await screen.findByText('a', { selector: '.name' }); |
| 141 | + await user.click(document.querySelector('.back-button')!); |
| 142 | + |
| 143 | + expect(mockNavigate).toHaveBeenCalledWith(-1); |
| 144 | + }); |
| 145 | + |
| 146 | + it('fetches the new user when the route ID changes', async () => { |
| 147 | + const userA = deferred<User>(); |
| 148 | + const userB = deferred<User>(); |
| 149 | + vi.mocked(hackerNewsApi.fetchUser).mockImplementation((id) => (id === 'a' ? userA.promise : userB.promise)); |
| 150 | + const user = userEvent.setup(); |
| 151 | + |
| 152 | + renderUser(['/user/a', '/user/b'], 0, true); |
| 153 | + |
| 154 | + await waitFor(() => expect(hackerNewsApi.fetchUser).toHaveBeenCalledWith('a')); |
| 155 | + await user.click(screen.getByRole('link', { name: 'go' })); |
| 156 | + await waitFor(() => expect(hackerNewsApi.fetchUser).toHaveBeenCalledWith('b')); |
| 157 | + expect(screen.getByText('Loading...')).toBeInTheDocument(); |
| 158 | + |
| 159 | + await act(async () => userB.resolve(makeUser('b'))); |
| 160 | + |
| 161 | + expect(await screen.findByText('b', { selector: '.name' })).toBeInTheDocument(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('fetches the exact route ID once', async () => { |
| 165 | + vi.mocked(hackerNewsApi.fetchUser).mockResolvedValueOnce(makeUser()); |
| 166 | + |
| 167 | + renderUser(); |
| 168 | + |
| 169 | + await screen.findByText('a', { selector: '.name' }); |
| 170 | + |
| 171 | + expect(hackerNewsApi.fetchUser).toHaveBeenCalledTimes(1); |
| 172 | + expect(hackerNewsApi.fetchUser).toHaveBeenCalledWith('a'); |
| 173 | + }); |
| 174 | + |
| 175 | + it('ignores a stale response after the route ID changes', async () => { |
| 176 | + const userA = deferred<User>(); |
| 177 | + const userB = deferred<User>(); |
| 178 | + vi.mocked(hackerNewsApi.fetchUser).mockImplementation((id) => (id === 'a' ? userA.promise : userB.promise)); |
| 179 | + const user = userEvent.setup(); |
| 180 | + |
| 181 | + renderUser(['/user/a', '/user/b'], 0, true); |
| 182 | + |
| 183 | + await waitFor(() => expect(hackerNewsApi.fetchUser).toHaveBeenCalledWith('a')); |
| 184 | + await user.click(screen.getByRole('link', { name: 'go' })); |
| 185 | + await waitFor(() => expect(hackerNewsApi.fetchUser).toHaveBeenCalledWith('b')); |
| 186 | + |
| 187 | + await act(async () => userA.resolve(makeUser('a'))); |
| 188 | + expect(document.querySelector('.profile')).not.toBeInTheDocument(); |
| 189 | + |
| 190 | + await act(async () => userB.resolve(makeUser('b'))); |
| 191 | + |
| 192 | + expect(await screen.findByText('b', { selector: '.name' })).toBeInTheDocument(); |
| 193 | + expect(document.querySelector('.name')).toHaveTextContent('b'); |
| 194 | + expect(document.querySelector('.name')).not.toHaveTextContent('a'); |
| 195 | + }); |
| 196 | +}); |
0 commit comments