test(measurements): drive the measurement form in a real browser

Type into the form, press Save and read the request body off the fetch
boundary: blood pressure as one batch, a weight typed in lb stored in
kg, a decimal comma, glucose typed in mmol/L stored in mg/dL with its
meal context, a sleep typed as 7:30 stored as 450 minutes. Native
required validation, the pending state, a refused write's localised
message, the module-off type list, the last-used type and the menu's
Reset are covered too.

The unit-label and glucose-unit markup tests are deleted; their source
pins are replaced by the wire assertions. The module-types file keeps
its pure ownership checks and drops its render.

The browser helper gains a Date-only clock pin, clears localStorage
between tests, carries an envelope's meta, and answers the providers'
locale mirror without counting it as the component's request.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 13:07:29 +02:00
1 parent cc36f46f10
commit 3ab85f85f9
9 files changed
+356 -220

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
/**
* Pin `Date` for a browser test, and nothing else.
*
* Forms default their timestamp to "now" and refuse a future one, so a test
* that asserts the instant a form sends has to say what now is. Only `Date`
* is faked: timers, animation frames and the microtask queue keep running,
* which is what Radix, TanStack Query and the URL-state queue wait on.
* `setup.ts` restores the real clock after every test.
*/
import { vi } from "vitest";
export function pinDate(iso: string): void {
vi.useFakeTimers({ toFake: ["Date"], now: new Date(iso) });
}
+19 -2
View File
@@ -14,6 +14,11 @@
* state. A request no route matches fails with a 404 envelope and is recorded
* too, so an unexpected call is visible rather than hanging.
*
* One request is ambient: `I18nProvider` mirrors the active locale to
* `PUT /api/auth/me/locale` on every mount, as it does in the app. The stub
* answers it and leaves it out of `requests`, so "nothing was sent" means
* nothing the component under test sent.
*
* `setup.ts` restores the real `fetch` after every test.
*/
export interface RecordedRequest {
@@ -30,6 +35,8 @@ export interface StubResponse {
/** The envelope's `data`. Ignored when `error` is set. */
data?: unknown;
error?: string;
/** The envelope's `meta` (an error's `errorCode`, a list's totals). */
meta?: Record<string, unknown>;
}
type Responder = (
@@ -65,6 +72,9 @@ export interface FetchStub {
let realFetch: typeof fetch | null = null;
/** Sent by the providers on every mount, not by the component under test. */
const AMBIENT = [{ method: "PUT", path: "/api/auth/me/locale" }];
function matches(route: Route, method: string, path: string): boolean {
if (route.method !== method) return false;
const bare = path.split("?")[0];
@@ -73,8 +83,11 @@ function matches(route: Route, method: string, path: string): boolean {
: route.path.test(path);
}
function envelope({ status = 200, data = null, error }: StubResponse) {
const body = error ? { data: null, error } : { data, error: null };
function envelope({ status = 200, data = null, error, meta }: StubResponse) {
const body = {
...(error ? { data: null, error } : { data, error: null }),
...(meta ? { meta } : {}),
};
return new Response(JSON.stringify(body), {
status: error && status === 200 ? 400 : status,
headers: { "content-type": "application/json" },
@@ -110,6 +123,10 @@ export function stubFetch(): FetchStub {
window.fetch = async (input, init) => {
const request = await record(input, init);
const ambient = AMBIENT.some(
(a) => a.method === request.method && a.path === request.path,
);
if (ambient) return envelope({ data: request.body ?? null });
requests.push(request);
// The newest route wins, so a test can override a default it set up.
const route = [...routes]
+6 -3
View File
@@ -8,13 +8,14 @@
* - The EN catalog, the fallback floor of every `t()` chain, loaded through
* the production loader. `renderWithApp` loads any other locale it is
* asked for.
* - After every test: the real `fetch` and the page's starting address come
* back, so a stub or a URL write never reaches the next test. Rendered
* - After every test: the real `fetch`, the real clock, the page's starting
* address and an empty `localStorage` come back, so a stub, a pinned date,
* a URL write or a remembered preference never reaches the next test. Rendered
* trees are unmounted by `vitest-browser-react` itself.
*/
import "@/app/globals.css";
import { afterEach, beforeAll, beforeEach } from "vitest";
import { afterEach, beforeAll, beforeEach, vi } from "vitest";
import { loadMessages } from "@/lib/i18n/load-locale";
@@ -33,5 +34,7 @@ beforeEach(() => {
afterEach(() => {
restoreFetch();
vi.useRealTimers();
restoreAddress();
window.localStorage.clear();
});