Skip to content

Commit 7b38ae4

Browse files
committed
Merge #1241: test(router): internal test kit on production wiring (S1)
2 parents ff93b0b + 4dc422f commit 7b38ae4

22 files changed

Lines changed: 825 additions & 861 deletions

‎packages/rangojs-router/docs/internal/feature-file-map.md‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
createEventController,
3+
type EventController,
4+
} from "../../browser/event-controller.js";
5+
6+
const DEFAULT_URL = "http://localhost/";
7+
8+
interface KitEventControllerOptions {
9+
/** The committed location; a path resolves against http://localhost/. */
10+
url?: string | URL;
11+
/** Route params, as a commit's setParams writes them. */
12+
params?: Record<string, string>;
13+
/** Matched segment ids, as a commit's setHandleData writes them. */
14+
matched?: string[];
15+
}
16+
17+
/**
18+
* An EventController from createEventController, holding the committed
19+
* location, params and matched segments a test starts from. The seeding
20+
* notifications are flushed before it returns, so a listener a test
21+
* subscribes afterwards hears only what the test changes.
22+
*/
23+
export function makeEventController(
24+
options: KitEventControllerOptions = {},
25+
): EventController {
26+
const controller = createEventController({
27+
initialLocation: new URL(options.url ?? DEFAULT_URL, DEFAULT_URL),
28+
});
29+
if (options.params) controller.setParams(options.params);
30+
if (options.matched) controller.setHandleData({}, options.matched);
31+
controller.flushRouteState();
32+
return controller;
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
createNavigationStore,
3+
generateHistoryKey,
4+
type NavigationStoreConfig,
5+
} from "../../browser/navigation-store.js";
6+
import type { NavigationStore } from "../../browser/types.js";
7+
8+
const DEFAULT_URL = "http://localhost/";
9+
10+
interface KitNavigationStoreOptions extends Omit<
11+
NavigationStoreConfig,
12+
"initialLocation"
13+
> {
14+
/**
15+
* The document URL; a path resolves against http://localhost/. Without it
16+
* the store reads `window.location`, or http://localhost/ with no `window`,
17+
* as createNavigationStore does.
18+
*/
19+
url?: string;
20+
}
21+
22+
/**
23+
* A NavigationStore from createNavigationStore, booted the way
24+
* browser/rsc-router.tsx boots one: the history key comes from the document
25+
* URL and the on-screen segment ids from `initialSegments`, unless a test
26+
* passes its own. Cross-tab sync is off, as in testing/render-route.tsx: it
27+
* opens a BroadcastChannel no unit test hears.
28+
*
29+
* createNavigationStore reads `window.location` whenever a `window` exists, so
30+
* a test that stubs a `window` without `location.href` creates the store
31+
* first.
32+
*/
33+
export function makeNavigationStore(
34+
options: KitNavigationStoreOptions = {},
35+
): NavigationStore {
36+
const { url, initialHistoryKey, initialSegmentIds, crossTabSync, ...config } =
37+
options;
38+
const href = url === undefined ? undefined : new URL(url, DEFAULT_URL).href;
39+
return createNavigationStore({
40+
...config,
41+
initialLocation: href === undefined ? undefined : { href },
42+
initialHistoryKey: initialHistoryKey ?? generateHistoryKey(href),
43+
initialSegmentIds:
44+
initialSegmentIds ?? config.initialSegments?.map((s) => s.id),
45+
crossTabSync: crossTabSync ?? false,
46+
});
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
createRequestContext,
3+
type CreateRequestContextOptions,
4+
type RequestContext,
5+
} from "../../server/request-context.js";
6+
7+
const DEFAULT_URL = "http://localhost/";
8+
9+
interface KitRequestContextOptions<TEnv> extends Partial<
10+
Omit<CreateRequestContextOptions<TEnv>, "request" | "url">
11+
> {
12+
/** The request URL; a path resolves against http://localhost/. */
13+
url?: string | URL;
14+
/** Init for the Request built from `url`: headers, method, signal. */
15+
init?: RequestInit;
16+
}
17+
18+
/**
19+
* A RequestContext from createRequestContext, the constructor the RSC handler
20+
* uses. As in testing/internal/context.ts createTestRequestContext, the
21+
* Request is built from `url`, and `env` and `variables` default to empty.
22+
* Unlike it, nothing else is seeded: a state cookie name or router versions
23+
* reach the constructor only when a test passes them.
24+
*/
25+
export function makeRequestContext<TEnv = {}>(
26+
options: KitRequestContextOptions<TEnv> = {},
27+
): RequestContext<TEnv> {
28+
const {
29+
url = DEFAULT_URL,
30+
init,
31+
env = {} as TEnv,
32+
variables = {},
33+
...rest
34+
} = options;
35+
const request = new Request(new URL(url, DEFAULT_URL), init);
36+
return createRequestContext<TEnv>({
37+
...rest,
38+
env,
39+
request,
40+
url: new URL(request.url),
41+
variables,
42+
});
43+
}

0 commit comments

Comments
 (0)