|
| 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 | +} |
0 commit comments