diff --git a/web/src/components/layout/Shell.test.tsx b/web/src/components/layout/Shell.test.tsx index 8819402..21192ba 100644 --- a/web/src/components/layout/Shell.test.tsx +++ b/web/src/components/layout/Shell.test.tsx @@ -1,5 +1,5 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { render, screen, within } from '@testing-library/react' +import { render, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { createMemoryRouter, RouterProvider } from 'react-router-dom' import { beforeEach, expect, test, vi } from 'vitest' @@ -13,21 +13,24 @@ beforeEach(async () => { await initI18n() }) -function renderAt(path: string) { +async function renderAt(path: string) { const router = createMemoryRouter(routes, { initialEntries: [path] }) render( , ) + // The pages are fetched when first opened: wait until the one asked for is in. + await waitFor(() => expect(router.state.initialized).toBe(true)) + await screen.findByRole('main') } function mainNav() { return screen.getByRole('navigation', { name: 'Main' }) } -test('at /artists the Artists link is the current page and the others are not', () => { - renderAt('/artists') +test('at /artists the Artists link is the current page and the others are not', async () => { + await renderAt('/artists') const nav = within(mainNav()) expect(nav.getByRole('link', { name: 'Artists' })).toHaveAttribute('aria-current', 'page') for (const name of ['Home', 'Search', 'Discover', 'Albums', 'Songs', 'Settings']) { @@ -40,16 +43,16 @@ const adminRoutes = { 'GET /api/v1/library/scan': () => ({ running: false }), } -test('an admin has an Admin link, and a user who is not one has none', () => { +test('an admin has an Admin link, and a user who is not one has none', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: false } }) - renderAt('/artists') + await renderAt('/artists') expect(within(mainNav()).queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument() }) test('the Admin link opens the libraries and stays current inside the admin area', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch(adminRoutes) - renderAt('/admin') + await renderAt('/admin') expect(await screen.findByRole('region', { name: 'Libraries' })).toBeInTheDocument() expect(within(mainNav()).getByRole('link', { name: 'Admin' })).toHaveAttribute('aria-current', 'page') }) @@ -57,7 +60,7 @@ test('the Admin link opens the libraries and stays current inside the admin area test('the admin area has a Users section next to Libraries', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch({ ...adminRoutes, 'GET /api/v1/users': () => ({ users: [{ id: 1, email: 'tom@example.org', is_admin: true }] }) }) - renderAt('/admin/users') + await renderAt('/admin/users') expect(await screen.findByRole('region', { name: 'Users' })).toBeInTheDocument() const sections = within(screen.getByRole('navigation', { name: 'Admin sections' })) expect(sections.getByRole('link', { name: 'Users' })).toHaveAttribute('aria-current', 'page') @@ -67,7 +70,7 @@ test('the admin area has a Users section next to Libraries', async () => { test('the admin area has a Providers section too', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch({ ...adminRoutes, 'GET /api/v1/providers': () => ({ providers: [] }) }) - renderAt('/admin/providers') + await renderAt('/admin/providers') expect(await screen.findByRole('region', { name: 'Providers' })).toBeInTheDocument() expect(within(screen.getByRole('navigation', { name: 'Admin sections' })).getByRole('link', { name: 'Providers' })).toHaveAttribute('aria-current', 'page') }) @@ -81,7 +84,7 @@ test('the admin area has a Jobs section too', async () => { 'GET /api/v1/library/artist-images': () => ({ running: false }), 'GET /api/v1/library/descriptions': () => ({ running: false }), }) - renderAt('/admin/jobs') + await renderAt('/admin/jobs') expect(await screen.findByRole('region', { name: 'Jobs' })).toBeInTheDocument() expect(within(screen.getByRole('navigation', { name: 'Admin sections' })).getByRole('link', { name: 'Jobs' })).toHaveAttribute('aria-current', 'page') }) @@ -89,8 +92,8 @@ test('the admin area has a Jobs section too', async () => { test('someone who is not an admin who opens the jobs address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch({ 'GET /api/v1/library/covers': () => ({ running: false }) }) - renderAt('/admin/jobs') - expect(screen.getByText('Only admins can see this page.')).toBeInTheDocument() + await renderAt('/admin/jobs') + expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls).toHaveLength(0) }) @@ -98,8 +101,8 @@ test('someone who is not an admin who opens the jobs address gets a notice and n test('someone who is not an admin who opens the providers address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch({ 'GET /api/v1/providers': () => ({ providers: [] }) }) - renderAt('/admin/providers') - expect(screen.getByText('Only admins can see this page.')).toBeInTheDocument() + await renderAt('/admin/providers') + expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls).toHaveLength(0) }) @@ -107,8 +110,8 @@ test('someone who is not an admin who opens the providers address gets a notice test('someone who is not an admin who opens the users address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch({ 'GET /api/v1/users': () => ({ users: [] }) }) - renderAt('/admin/users') - expect(screen.getByText('Only admins can see this page.')).toBeInTheDocument() + await renderAt('/admin/users') + expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls).toHaveLength(0) }) @@ -116,30 +119,30 @@ test('someone who is not an admin who opens the users address gets a notice and test('someone who is not an admin who opens the admin address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch(adminRoutes) - renderAt('/admin/libraries') - expect(screen.getByText('Only admins can see this page.')).toBeInTheDocument() + await renderAt('/admin/libraries') + expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls.filter((c) => c.url.pathname.includes('librar'))).toHaveLength(0) }) -test('Home is the current page only on /', () => { - renderAt('/') +test('Home is the current page only on /', async () => { + await renderAt('/') expect(within(mainNav()).getByRole('link', { name: 'Home' })).toHaveAttribute('aria-current', 'page') }) -test('an artist page keeps the Artists link current', () => { - renderAt('/artists/5') +test('an artist page keeps the Artists link current', async () => { + await renderAt('/artists/5') expect(within(mainNav()).getByRole('link', { name: 'Artists' })).toHaveAttribute('aria-current', 'page') }) -test('the page has one main landmark and says what the server does with data', () => { - renderAt('/songs') +test('the page has one main landmark and says what the server does with data', async () => { + await renderAt('/songs') expect(screen.getAllByRole('main')).toHaveLength(1) expect(screen.getByText('Nothing leaves this server unless an admin turns a provider on.')).toBeInTheDocument() }) -test('the home page opens with a greeting as its heading', () => { - renderAt('/') +test('the home page opens with a greeting as its heading', async () => { + await renderAt('/') expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent(/^Good (morning|afternoon|evening)$/) }) @@ -150,26 +153,26 @@ test.each([ ['/songs', 'Songs'], ['/discover', 'Discover'], ['/settings', 'Settings'], -])('the route %s opens a page headed %s', (path, heading) => { - renderAt(path) +])('the route %s opens a page headed %s', async (path, heading) => { + await renderAt(path) expect(screen.getByRole('heading', { level: 1, name: heading })).toBeInTheDocument() }) -test.each(['/artists/5', '/albums/5'])('the detail route %s opens inside the shell and starts loading', (path) => { +test.each(['/artists/5', '/albums/5'])('the detail route %s opens inside the shell and starts loading', async (path) => { mockFetch({}) - renderAt(path) + await renderAt(path) expect(within(screen.getByRole('main')).getByRole('status')).toHaveTextContent('Loading') }) -test('an unknown path shows the not-found page with a way back', () => { - renderAt('/no/such/page') +test('an unknown path shows the not-found page with a way back', async () => { + await renderAt('/no/such/page') expect(screen.getByRole('heading', { name: 'This page does not exist' })).toBeInTheDocument() expect(screen.getByRole('link', { name: 'Back to Home' })).toHaveAttribute('href', '/') }) test('a skip link comes first and moves the focus to the page', async () => { const user = userEvent.setup() - renderAt('/artists') + await renderAt('/artists') await user.tab() const skip = screen.getByRole('link', { name: 'Skip to content' }) expect(skip).toHaveFocus() diff --git a/web/src/router.tsx b/web/src/router.tsx index 2b86df4..bf01d2e 100644 --- a/web/src/router.tsx +++ b/web/src/router.tsx @@ -1,62 +1,52 @@ +import type { ComponentType } from 'react' +import { useTranslation } from 'react-i18next' import { Navigate, type RouteObject } from 'react-router-dom' import Shell from '@/components/layout/Shell' -import AlbumPage from '@/pages/AlbumPage' -import AlbumsPage from '@/pages/AlbumsPage' -import ArtistPage from '@/pages/ArtistPage' -import ArtistsPage from '@/pages/ArtistsPage' -import DiscoverPage from '@/pages/DiscoverPage' import HomePage from '@/pages/HomePage' -import MixesPage from '@/pages/MixesPage' -import MixPage from '@/pages/MixPage' -import AdminLayout from '@/pages/admin/AdminLayout' -import CostsPage from '@/pages/admin/CostsPage' -import JobsPage from '@/pages/admin/JobsPage' -import LibrariesPage from '@/pages/admin/LibrariesPage' -import ProvidersPage from '@/pages/admin/ProvidersPage' -import UsersPage from '@/pages/admin/UsersPage' import NotFoundPage from '@/pages/NotFoundPage' -import PlaylistPage from '@/pages/PlaylistPage' -import PlaylistsPage from '@/pages/PlaylistsPage' -import SearchPage from '@/pages/SearchPage' -import SmartEditorPage from '@/pages/SmartEditorPage' -import SmartPlaylistPage from '@/pages/SmartPlaylistPage' -import SmartPlaylistsPage from '@/pages/SmartPlaylistsPage' -import SettingsPage from '@/pages/SettingsPage' -import SongsPage from '@/pages/SongsPage' -import SuggestedSmartPage from '@/pages/SuggestedSmartPage' + +/** What is on screen while the first page of a lazy route is being fetched. */ +function Loading() { + const { t } = useTranslation() + return
+} + +/** A page that is fetched when somebody first opens it, so that the first screen does not carry the admin area. */ +const page = (load: () => Promise<{ default: ComponentType }>) => async () => ({ Component: (await load()).default }) export const routes: RouteObject[] = [ { element: , + HydrateFallback: Loading, children: [ { path: '/', element: }, - { path: '/search', element: }, - { path: '/discover', element: }, - { path: '/artists', element: }, - { path: '/artists/:id', element: }, - { path: '/albums', element: }, - { path: '/albums/:id', element: }, - { path: '/songs', element: }, - { path: '/mixes', element: }, - { path: '/mixes/:id', element: }, - { path: '/playlists', element: }, - { path: '/playlists/:id', element: }, - { path: '/smart-playlists', element: }, - { path: '/smart-playlists/new', element: }, - { path: '/smart-playlists/suggested/:key', element: }, - { path: '/smart-playlists/:id', element: }, - { path: '/smart-playlists/:id/edit', element: }, - { path: '/settings', element: }, + { path: '/search', lazy: page(() => import('@/pages/SearchPage')) }, + { path: '/discover', lazy: page(() => import('@/pages/DiscoverPage')) }, + { path: '/artists', lazy: page(() => import('@/pages/ArtistsPage')) }, + { path: '/artists/:id', lazy: page(() => import('@/pages/ArtistPage')) }, + { path: '/albums', lazy: page(() => import('@/pages/AlbumsPage')) }, + { path: '/albums/:id', lazy: page(() => import('@/pages/AlbumPage')) }, + { path: '/songs', lazy: page(() => import('@/pages/SongsPage')) }, + { path: '/mixes', lazy: page(() => import('@/pages/MixesPage')) }, + { path: '/mixes/:id', lazy: page(() => import('@/pages/MixPage')) }, + { path: '/playlists', lazy: page(() => import('@/pages/PlaylistsPage')) }, + { path: '/playlists/:id', lazy: page(() => import('@/pages/PlaylistPage')) }, + { path: '/smart-playlists', lazy: page(() => import('@/pages/SmartPlaylistsPage')) }, + { path: '/smart-playlists/new', lazy: page(() => import('@/pages/SmartEditorPage')) }, + { path: '/smart-playlists/suggested/:key', lazy: page(() => import('@/pages/SuggestedSmartPage')) }, + { path: '/smart-playlists/:id', lazy: page(() => import('@/pages/SmartPlaylistPage')) }, + { path: '/smart-playlists/:id/edit', lazy: page(() => import('@/pages/SmartEditorPage')) }, + { path: '/settings', lazy: page(() => import('@/pages/SettingsPage')) }, { path: '/admin', - element: , + lazy: page(() => import('@/pages/admin/AdminLayout')), children: [ { index: true, element: }, - { path: 'libraries', element: }, - { path: 'users', element: }, - { path: 'providers', element: }, - { path: 'jobs', element: }, - { path: 'costs', element: }, + { path: 'libraries', lazy: page(() => import('@/pages/admin/LibrariesPage')) }, + { path: 'users', lazy: page(() => import('@/pages/admin/UsersPage')) }, + { path: 'providers', lazy: page(() => import('@/pages/admin/ProvidersPage')) }, + { path: 'jobs', lazy: page(() => import('@/pages/admin/JobsPage')) }, + { path: 'costs', lazy: page(() => import('@/pages/admin/CostsPage')) }, ], }, { path: '*', element: },