Files
TREK/client/src/components/SystemNotices/noticeActions.test.ts
T
Maurice 6c63a9bb60 fix(client): move to react-router 7.18.2 (#1804)
* 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.
2026-08-06 23:01:58 +02:00

66 lines
2.0 KiB
TypeScript

// FE-W4SNA-001 to FE-W4SNA-005
import { describe, it, expect, vi, afterEach } from 'vitest'
import type { NavigateFunction } from 'react-router'
import { registerNoticeAction, runNoticeAction } from './noticeActions'
const navigate = vi.fn() as unknown as NavigateFunction
const ctx = { navigate }
afterEach(() => {
vi.restoreAllMocks()
})
describe('noticeActions', () => {
it('FE-W4SNA-001: runs a registered handler with the navigate context', () => {
const handler = vi.fn(() => {})
registerNoticeAction('open-settings', handler)
runNoticeAction('open-settings', ctx)
expect(handler).toHaveBeenCalledWith(ctx)
})
it('FE-W4SNA-002: logs and does nothing for an unknown id', () => {
const err = vi.spyOn(console, 'error').mockImplementation(() => {})
expect(() => runNoticeAction('never-registered', ctx)).not.toThrow()
expect(err).toHaveBeenCalledTimes(1)
expect(String(err.mock.calls[0][0])).toContain('never-registered')
})
it('FE-W4SNA-003: a later registration replaces the handler for the same id', () => {
const first = vi.fn(() => {})
const second = vi.fn(() => {})
registerNoticeAction('dup', first)
registerNoticeAction('dup', second)
runNoticeAction('dup', ctx)
expect(first).not.toHaveBeenCalled()
expect(second).toHaveBeenCalledOnce()
})
it('FE-W4SNA-004: fires an async handler without blocking the caller', async () => {
let resolved = false
registerNoticeAction('async-cta', async () => {
await Promise.resolve()
resolved = true
})
expect(runNoticeAction('async-cta', ctx)).toBeUndefined()
expect(resolved).toBe(false)
await Promise.resolve()
await Promise.resolve()
expect(resolved).toBe(true)
})
it('FE-W4SNA-005: hands the real navigate through to the handler', () => {
registerNoticeAction('go-billing', c => { c.navigate('/settings/billing') })
runNoticeAction('go-billing', ctx)
expect(navigate).toHaveBeenCalledWith('/settings/billing')
})
})