Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,48 @@ for (const key of GLOBALS_TO_COPY) {
}
}

// ---------------------------------------------------------------------------
// Default user-preference fetches to the "never set" envelope.
//
// Admin surfaces load user preferences on mount (e.g. the module-inserter
// favourites via `useModuleInserterPreference` → `getUserPreference`), which
// fires a real `fetch` to `/admin/api/cms/me/preferences/<key>`. In tests that
// hits `http://localhost` and rejects with ECONNREFUSED. Because the
// preference store is a module-level singleton whose load promise can outlive
// the component that triggered it, that rejection surfaces during a LATER
// test's `cleanup()` as an `AggregateError` — a cross-test flake that depends
// on render timing (it was masked while canvas frames mounted slowly, and
// reappeared once they mount synchronously).
//
// Returning the server's "never set" signal (`{ value: null }`) makes every
// such load resolve cleanly to the caller's own default, with no network call.
// Tests that need specific preference data still override `globalThis.fetch`
// in their own setup; `getUserPreference` callers that inject a `fetchImpl`
// bypass this entirely.
{
const realFetch = globalThis.fetch
const PREFERENCES_PATH = '/admin/api/cms/me/preferences/'
;(globalThis as Record<string, unknown>).fetch = (async (
input: RequestInfo | URL,
init?: RequestInit,
): Promise<Response> => {
const url =
typeof input === 'string'
? input
: input instanceof URL
? input.href
: (input as Request).url
const method = (init?.method ?? (input as Request).method ?? 'GET').toUpperCase()
if (method === 'GET' && url.includes(PREFERENCES_PATH)) {
return new Response(JSON.stringify({ value: null }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}
return realFetch(input, init)
}) as typeof globalThis.fetch
}

// ---------------------------------------------------------------------------
// Tame DOM serialization in failure output.
//
Expand Down