Public Access
test: data router, one render helper, fetchRoutes in every test
This commit is contained in:
1 parent
d0ea9e900d
commit
2a22789435
37 files changed
+454
-425
No files matched your search
+16
-19
@@ -1,9 +1,9 @@
|
||||
// @vitest-environment jsdom
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {act, cleanup, fireEvent, render, screen, waitFor} from '@testing-library/react';
|
||||
import {MemoryRouter, useLocation} from 'react-router-dom';
|
||||
import {act, cleanup, fireEvent, screen, waitFor} from '@testing-library/react';
|
||||
import App from './App.jsx';
|
||||
import {PreferencesProvider} from './context/PreferencesContext.jsx';
|
||||
import {fetchRoutes, json} from './test/fetchRoutes.js';
|
||||
import {renderWithProviders} from './test/renderWithProviders.jsx';
|
||||
|
||||
vi.mock('react-i18next', () => import('./test/i18n-mock.js'));
|
||||
vi.mock('./hooks/useLibrarySync.js', () => ({default: () => ({status: 'OK', lastSyncedAt: null, isRunning: false})}));
|
||||
@@ -14,16 +14,8 @@ const page = {content: [{appid: 220, name: 'Half-Life 2', playtimeMinutes: 0, st
|
||||
|
||||
const wait = (ms) => act(() => new Promise((resolve) => setTimeout(resolve, ms)));
|
||||
|
||||
const current = {location: null};
|
||||
|
||||
function Spy() {
|
||||
current.location = useLocation();
|
||||
return null;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.setItem('tbp_session', '1');
|
||||
globalThis.fetch = vi.fn(async () => ({ok: true, status: 200, json: async () => page}));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -33,19 +25,24 @@ afterEach(() => {
|
||||
|
||||
describe('App', () => {
|
||||
it('ends the session on a 401 that the refresh cannot fix', async () => {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/library']}>
|
||||
<PreferencesProvider><App/><Spy/></PreferencesProvider>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
let expired = false;
|
||||
const library = () => (expired ? json(401, {}) : json(200, page));
|
||||
fetchRoutes({
|
||||
'GET /api/users/me': json(200, {settings: {}}),
|
||||
'GET /api/library/games': library,
|
||||
'GET /api/library/games-count': json(200, {hasSteamId: true}),
|
||||
'GET /api/library/steam-profile': json(200, null),
|
||||
'POST /api/auth/refresh': json(401, {}),
|
||||
});
|
||||
const {router} = renderWithProviders(<App/>, {entries: ['/library']});
|
||||
await waitFor(() => expect(screen.getByText('Half-Life 2')).toBeTruthy());
|
||||
|
||||
fetch.mockImplementation(async () => ({ok: false, status: 401, json: async () => ({})}));
|
||||
expired = true;
|
||||
fireEvent.change(screen.getByRole('searchbox'), {target: {value: 'half'}});
|
||||
await wait(DEBOUNCE_WAIT_MS);
|
||||
|
||||
await waitFor(() => expect(current.location.pathname).toBe('/login'));
|
||||
expect(current.location.state).toEqual({sessionExpired: true, from: '/library?q=half&page=0'});
|
||||
await waitFor(() => expect(router.state.location.pathname).toBe('/login'));
|
||||
expect(router.state.location.state).toEqual({sessionExpired: true, from: '/library?q=half&page=0'});
|
||||
expect(localStorage.getItem('tbp_session')).toBeNull();
|
||||
expect(fetch).toHaveBeenCalledWith(expect.stringContaining('/api/auth/refresh'), expect.objectContaining({method: 'POST'}));
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user