mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
6c63a9bb60
* refactor(client): opt into the v7 router behaviour while still on v6 Turning the behaviour on separately from the version bump keeps the two failure modes apart: red here means a behaviour change, red on the bump means the import rewrite. The suite is unchanged either way — 527 files, 11099 tests. Only the two flags a non-data router understands are set; the rest belong to createBrowserRouter, which this app doesn't use. Both are applied at all six router mounts, so the tests exercise the same behaviour as production rather than validating v6 while prod runs v7. * fix(client): move to react-router 7.18.2 6.30.4 is affected by GHSA-wrjc-x8rr-h8h6 — open redirect through a backslash in Link and useNavigate, which this app uses in 41 places — and the 6.x line has no fix: react-router-dom 6.30.2 to 6.30.4 carries GHSA-jjmj-jmhj-qwj2 with no patched version at all. Staying is not an option, and 7.0 to 7.17 only swaps it for GHSA-chx6-hx7r-mcp5. The package rename comes with it: react-router-dom is a re-export shim in v7, so using it would only defer the same rewrite to the v8 jump. 77 occurrences across 60 files, all of them import specifiers and vi.mock strings. The surface is small — useNavigate, useParams, useSearchParams, useLocation, Routes, Route, Link, Navigate, useMatch, MemoryRouter, BrowserRouter — no data router, no loaders or actions. Nothing else had to change; the future flags from the previous commit are simply how v7 behaves, so they come back out. npm audit still reports GHSA-qwww-vcr4-c8h2 on react-router (RSC mode CSRF, fixed in 8.3.0). It needs RSC mode, which needs a server; this is a static SPA. Closing it means the v8 major, which is its own piece of work.
151 lines
5.8 KiB
TypeScript
151 lines
5.8 KiB
TypeScript
// FE-PAGE-HELP-001 to FE-PAGE-HELP-012
|
|
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
import { helpApi, type HelpNavSection, type HelpPageData } from '../../api/client';
|
|
import { useHelp } from './useHelp';
|
|
|
|
let routeParams: { slug?: string } = {};
|
|
vi.mock('react-router', () => ({ useParams: () => routeParams }));
|
|
|
|
const SECTIONS: HelpNavSection[] = [
|
|
{ title: 'Getting started', pages: [{ slug: 'Home', title: 'Welcome' }, { slug: 'Install', title: 'Installation' }] },
|
|
{ title: 'Guides', pages: [{ slug: 'Trips', title: 'Planning a trip' }, { slug: 'Budget', title: 'Splitting costs' }] },
|
|
] as HelpNavSection[];
|
|
|
|
const PAGE = { slug: 'Home', title: 'Welcome', body: '# Welcome' } as unknown as HelpPageData;
|
|
|
|
beforeEach(() => {
|
|
routeParams = {};
|
|
// jsdom has no scrollTo and logs a "Not implemented" error for every call.
|
|
vi.stubGlobal('scrollTo', vi.fn());
|
|
vi.spyOn(helpApi, 'index').mockResolvedValue({ sections: SECTIONS } as never);
|
|
vi.spyOn(helpApi, 'page').mockResolvedValue(PAGE as never);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('useHelp', () => {
|
|
it('FE-PAGE-HELP-001: loads the navigation and the landing page', async () => {
|
|
const { result } = renderHook(() => useHelp());
|
|
expect(result.current.loading).toBe(true);
|
|
|
|
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
expect(result.current.filtered).toEqual(SECTIONS);
|
|
expect(result.current.page).toEqual(PAGE);
|
|
expect(result.current.pageError).toBe(false);
|
|
});
|
|
|
|
it('FE-PAGE-HELP-002: defaults to the first page in the index when the route has no slug', async () => {
|
|
renderHook(() => useHelp());
|
|
await waitFor(() => expect(helpApi.page).toHaveBeenLastCalledWith('Home'));
|
|
});
|
|
|
|
it('FE-PAGE-HELP-003: falls back to the Home slug while the index is still loading', () => {
|
|
renderHook(() => useHelp());
|
|
expect(helpApi.page).toHaveBeenCalledWith('Home');
|
|
});
|
|
|
|
it('FE-PAGE-HELP-004: honours the slug from the route', async () => {
|
|
routeParams = { slug: 'Budget' };
|
|
const { result } = renderHook(() => useHelp());
|
|
|
|
await waitFor(() => expect(result.current.activeSlug).toBe('Budget'));
|
|
expect(helpApi.page).toHaveBeenCalledWith('Budget');
|
|
});
|
|
|
|
it('FE-PAGE-HELP-005: keeps an empty navigation when the index request fails', async () => {
|
|
vi.mocked(helpApi.index).mockRejectedValue(new Error('offline'));
|
|
const { result } = renderHook(() => useHelp());
|
|
|
|
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
expect(result.current.filtered).toEqual([]);
|
|
});
|
|
|
|
it('FE-PAGE-HELP-006: flags a page that could not be fetched', async () => {
|
|
vi.mocked(helpApi.page).mockRejectedValue(new Error('404'));
|
|
const { result } = renderHook(() => useHelp());
|
|
|
|
await waitFor(() => expect(result.current.pageError).toBe(true));
|
|
expect(result.current.loading).toBe(false);
|
|
expect(result.current.page).toBeNull();
|
|
});
|
|
|
|
it('FE-PAGE-HELP-007: filters the navigation by page title, case-insensitively', async () => {
|
|
const { result } = renderHook(() => useHelp());
|
|
await waitFor(() => expect(result.current.filtered).toHaveLength(2));
|
|
|
|
act(() => result.current.setQuery(' PLAN '));
|
|
|
|
expect(result.current.filtered).toEqual([
|
|
{ title: 'Guides', pages: [{ slug: 'Trips', title: 'Planning a trip' }] },
|
|
]);
|
|
});
|
|
|
|
it('FE-PAGE-HELP-008: drops sections that no longer have a matching page', async () => {
|
|
const { result } = renderHook(() => useHelp());
|
|
await waitFor(() => expect(result.current.filtered).toHaveLength(2));
|
|
|
|
act(() => result.current.setQuery('zzz'));
|
|
expect(result.current.filtered).toEqual([]);
|
|
});
|
|
|
|
it('FE-PAGE-HELP-009: an empty query restores the full navigation', async () => {
|
|
const { result } = renderHook(() => useHelp());
|
|
await waitFor(() => expect(result.current.filtered).toHaveLength(2));
|
|
|
|
act(() => result.current.setQuery('install'));
|
|
expect(result.current.filtered).toHaveLength(1);
|
|
|
|
act(() => result.current.setQuery(' '));
|
|
expect(result.current.filtered).toEqual(SECTIONS);
|
|
});
|
|
|
|
it('FE-PAGE-HELP-010: scrolls back to the top and closes the mobile nav on navigation', async () => {
|
|
const scrollTo = vi.mocked(window.scrollTo);
|
|
const { result, rerender } = renderHook(() => useHelp());
|
|
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
|
|
act(() => result.current.setNavOpen(true));
|
|
expect(result.current.navOpen).toBe(true);
|
|
|
|
routeParams = { slug: 'Trips' };
|
|
rerender();
|
|
|
|
await waitFor(() => expect(result.current.navOpen).toBe(false));
|
|
expect(scrollTo).toHaveBeenCalledWith({ top: 0 });
|
|
});
|
|
|
|
it('FE-PAGE-HELP-011: scrolls the content pane when the ref is attached', async () => {
|
|
const { result, rerender } = renderHook(() => useHelp());
|
|
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
|
|
const scrollTo = vi.fn();
|
|
(result.current.contentRef as { current: unknown }).current = { scrollTo };
|
|
|
|
routeParams = { slug: 'Install' };
|
|
rerender();
|
|
|
|
await waitFor(() => expect(scrollTo).toHaveBeenCalledWith({ top: 0 }));
|
|
});
|
|
|
|
it('FE-PAGE-HELP-012: a page arriving after a slug change does not overwrite the newer one', async () => {
|
|
let resolveFirst: ((p: HelpPageData) => void) | undefined;
|
|
vi.mocked(helpApi.page).mockImplementationOnce(
|
|
() => new Promise(res => { resolveFirst = res as (p: HelpPageData) => void; }) as never,
|
|
);
|
|
|
|
const { result, rerender } = renderHook(() => useHelp());
|
|
|
|
routeParams = { slug: 'Trips' };
|
|
rerender();
|
|
await waitFor(() => expect(result.current.page).toEqual(PAGE));
|
|
|
|
const stale = { slug: 'Home', title: 'Stale', body: '' } as unknown as HelpPageData;
|
|
await act(async () => { resolveFirst!(stale); });
|
|
|
|
expect(result.current.page).toEqual(PAGE);
|
|
});
|
|
});
|