mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
32837afe2e
msw 3 renames onUnhandledRequest to onUnhandledFrame and reports every network frame there; the setup judges the HTTP frames as before (another origin is an error, the page's own a warning). It also resolves a relative handler, and its fetch interceptor a relative URL, against location.href at the time of the request, while jsdom's XHR sends to the document's own address. Tests that replaced window.location with a stand-in on http://localhost while the page runs on http://localhost:3000 sent their requests past every handler. tests/helpers/location.ts builds such stand-ins on the page's own origin (locationAt, pageUrl), and every one of them now uses it. msw no longer escapes fake timers, and responses are timed differently, which surfaced tests that ended with work still running: a held request answering in the next test, a lookup still out when the assertions ran, a reload switched by counting reads. Each of those now waits for, or releases, what it started, and answers by what happened rather than by a count. The day panel suites share their setup through tests/helpers/dayDetailPanel.ts instead of a copy each, which keeps the larger one under its size limit.
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
// The page's own location, taken when this module loads, before any test
|
|
// replaces window.location with a stand-in.
|
|
const pageLocation = window.location;
|
|
const pageOrigin = new URL(document.baseURI).origin;
|
|
|
|
/** `path` as an absolute URL on the page's own origin, for asserting on location.href. */
|
|
export function pageUrl(path: string): string {
|
|
return new URL(path, pageOrigin).href;
|
|
}
|
|
|
|
/**
|
|
* A stand-in for window.location that shows `path` on the page's own origin.
|
|
*
|
|
* Requests have to stay on one origin for the HTTP mocks to answer them: msw
|
|
* resolves its relative handlers, and its fetch interceptor a relative URL,
|
|
* against location.href, while jsdom's XHR resolves against the document. A
|
|
* stand-in on another origin (`http://localhost/` while the page runs on
|
|
* `http://localhost:3000`) sends every request past its handler.
|
|
*/
|
|
export function locationAt(path: string, overrides: Partial<Location> = {}): Location {
|
|
const url = new URL(path, pageOrigin);
|
|
return {
|
|
...pageLocation,
|
|
href: url.href,
|
|
origin: url.origin,
|
|
protocol: url.protocol,
|
|
host: url.host,
|
|
hostname: url.hostname,
|
|
port: url.port,
|
|
pathname: url.pathname,
|
|
search: url.search,
|
|
hash: url.hash,
|
|
...overrides,
|
|
};
|
|
}
|