@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/action-context.js +1 -1
- package/dist/actions.d.ts +7 -0
- package/dist/actions.js +34 -10
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/boundary.js +1 -1
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/cache-adapter.js +1 -1
- package/dist/cache-scope.d.ts +8 -0
- package/dist/cache-scope.js +14 -1
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +496 -91
- package/dist/dom.d.ts +4 -0
- package/dist/dom.js +62 -16
- package/dist/effects.js +43 -10
- package/dist/events.d.ts +2 -0
- package/dist/events.js +18 -0
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/handler-html.js +4 -4
- package/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +9 -190
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +18 -4
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +93 -6
- package/dist/highlight-bind.js +1 -1
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +17 -12
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +147 -74
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/island-swap.d.ts +47 -0
- package/dist/island-swap.js +635 -0
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-types.d.ts +99 -3
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +407 -80
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +1 -1
- package/dist/krate-f-highlight.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +455 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/krate-suspense.js +1 -1
- package/dist/list.d.ts +14 -4
- package/dist/list.js +30 -23
- package/dist/metadata.js +5 -2
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/node-server.js +3 -3
- package/dist/og.js +1 -1
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/react-compat.d.ts +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/region-wire.d.ts +10 -0
- package/dist/region-wire.js +154 -0
- package/dist/request-context.js +1 -1
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/resp.js +1 -1
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.d.ts +1 -1
- package/dist/router.js +675 -100
- package/dist/server-handler.d.ts +69 -1
- package/dist/server-handler.js +515 -96
- package/dist/server-renderer.js +16 -7
- package/dist/server.d.ts +73 -16
- package/dist/server.js +619 -102
- package/dist/sha256.js +9 -9
- package/dist/signal.js +47 -20
- package/dist/site-config.d.ts +15 -0
- package/dist/site-config.js +28 -0
- package/dist/slot.d.ts +12 -0
- package/dist/slot.js +23 -0
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +12 -4
- package/dist/suspense.js +34 -2
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +90 -0
- package/dist/use-id.js +1 -1
- package/package.json +28 -3
- package/dist/action-context.test.d.ts +0 -1
- package/dist/action-context.test.js +0 -72
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -477
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -244
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -233
- package/dist/class-component.test.d.ts +0 -1
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.d.ts +0 -1
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -57
- package/dist/derived.test.d.ts +0 -1
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.d.ts +0 -1
- package/dist/dev-overlay.test.js +0 -152
- package/dist/discrete.test.d.ts +0 -1
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.d.ts +0 -1
- package/dist/dom.test.js +0 -31
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -163
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -447
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -256
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -217
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -131
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -344
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -492
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -98
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -96
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -464
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -535
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -266
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -88
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -169
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -620
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- package/dist/suspense.test.js +0 -518
package/dist/router-nav.test.js
DELETED
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
-
import { initRouter, destroyRouter, krateRouter, navigate, getHistoryState, subscribeNavigation, invalidatePrefetchCache, } from './router';
|
|
3
|
-
function page(title, body, head = '') {
|
|
4
|
-
return `<!doctype html><html><head><title>${title}</title>${head}</head><body><div id="root">${body}</div></body></html>`;
|
|
5
|
-
}
|
|
6
|
-
let routes;
|
|
7
|
-
let fetchMock;
|
|
8
|
-
let hardNavs;
|
|
9
|
-
function installFetch() {
|
|
10
|
-
fetchMock = vi.fn(async (input, init) => {
|
|
11
|
-
const u = new URL(input, location.href);
|
|
12
|
-
const r = routes[u.pathname + u.search] ?? routes[u.pathname];
|
|
13
|
-
if (!r) {
|
|
14
|
-
return mockResponse({ status: 404, body: 'nope', type: 'text/plain' }, u.href);
|
|
15
|
-
}
|
|
16
|
-
if (r.gate)
|
|
17
|
-
await r.gate;
|
|
18
|
-
if (init?.signal?.aborted) {
|
|
19
|
-
const e = new Error('aborted');
|
|
20
|
-
e.name = 'AbortError';
|
|
21
|
-
throw e;
|
|
22
|
-
}
|
|
23
|
-
if (r.fail)
|
|
24
|
-
throw r.fail;
|
|
25
|
-
return mockResponse(r, r.redirect ? new URL(r.redirect, location.href).href : u.href);
|
|
26
|
-
});
|
|
27
|
-
vi.stubGlobal('fetch', fetchMock);
|
|
28
|
-
}
|
|
29
|
-
function mockResponse(r, url) {
|
|
30
|
-
const status = r.status ?? 200;
|
|
31
|
-
return {
|
|
32
|
-
ok: status >= 200 && status < 300,
|
|
33
|
-
status,
|
|
34
|
-
url,
|
|
35
|
-
redirected: !!r.redirect,
|
|
36
|
-
headers: new Headers({ 'content-type': r.type ?? 'text/html; charset=utf-8' }),
|
|
37
|
-
text: async () => r.body ?? '',
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function link(href, text, attrs = '') {
|
|
41
|
-
return `<a data-krate-link href="${href}" ${attrs}>${text}</a>`;
|
|
42
|
-
}
|
|
43
|
-
function click(el) {
|
|
44
|
-
el.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 }));
|
|
45
|
-
}
|
|
46
|
-
async function settle() {
|
|
47
|
-
for (let i = 0; i < 10; i++)
|
|
48
|
-
await Promise.resolve();
|
|
49
|
-
await new Promise((r) => setTimeout(r, 0));
|
|
50
|
-
for (let i = 0; i < 10; i++)
|
|
51
|
-
await Promise.resolve();
|
|
52
|
-
}
|
|
53
|
-
function events(name) {
|
|
54
|
-
const got = [];
|
|
55
|
-
window.addEventListener(name, (e) => got.push(e.detail));
|
|
56
|
-
return got;
|
|
57
|
-
}
|
|
58
|
-
beforeEach(() => {
|
|
59
|
-
destroyRouter();
|
|
60
|
-
globalThis.__KRATE_CFG__ = { viewTransitions: false };
|
|
61
|
-
history.replaceState(null, '', '/');
|
|
62
|
-
document.title = 'Home';
|
|
63
|
-
document.head.innerHTML = '<title>Home</title>';
|
|
64
|
-
document.body.innerHTML = `<div id="root"><h1>Home</h1>${link('/about', 'About')}${link('/docs', 'Docs')}${link('/docs-old', 'Old')}</div>`;
|
|
65
|
-
routes = {
|
|
66
|
-
'/about': { body: page('About', `<h1>About</h1>${link('/', 'Home')}`) },
|
|
67
|
-
'/docs': { body: page('Docs', `<h1>Docs</h1>${link('/docs', 'Docs')}${link('/docs/intro', 'Intro')}`) },
|
|
68
|
-
'/docs/intro': { body: page('Intro', `<h1>Intro</h1>${link('/docs', 'Docs')}${link('/docs-old', 'Old')}`) },
|
|
69
|
-
};
|
|
70
|
-
installFetch();
|
|
71
|
-
hardNavs = [];
|
|
72
|
-
globalThis.__krateHardNavigate = (url, replace) => hardNavs.push([url, replace]);
|
|
73
|
-
initRouter();
|
|
74
|
-
});
|
|
75
|
-
afterEach(() => {
|
|
76
|
-
destroyRouter();
|
|
77
|
-
vi.unstubAllGlobals();
|
|
78
|
-
delete globalThis.__krateHardNavigate;
|
|
79
|
-
delete globalThis.__KRATE_CFG__;
|
|
80
|
-
delete document.startViewTransition;
|
|
81
|
-
});
|
|
82
|
-
describe('SPA navigation', () => {
|
|
83
|
-
it('swaps the app root, title, history and announces the route', async () => {
|
|
84
|
-
const nav = events('krate:navigate');
|
|
85
|
-
click(document.querySelector('a[href="/about"]'));
|
|
86
|
-
await settle();
|
|
87
|
-
expect(location.pathname).toBe('/about');
|
|
88
|
-
expect(document.querySelector('#root h1').textContent).toBe('About');
|
|
89
|
-
expect(document.title).toBe('About');
|
|
90
|
-
expect(nav.length).toBe(1);
|
|
91
|
-
expect(document.getElementById('__krate-route-announcer').textContent).toBe('About');
|
|
92
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
93
|
-
});
|
|
94
|
-
it('ignores modified clicks and target=_blank links', async () => {
|
|
95
|
-
// Keep happy-dom from performing the default (real) navigation.
|
|
96
|
-
const stop = (e) => e.preventDefault();
|
|
97
|
-
window.addEventListener('click', stop);
|
|
98
|
-
document.getElementById('root').innerHTML = link('/about', 'x', 'target="_blank"');
|
|
99
|
-
click(document.querySelector('a'));
|
|
100
|
-
const a = document.createElement('a');
|
|
101
|
-
a.setAttribute('data-krate-link', '');
|
|
102
|
-
a.href = '/about';
|
|
103
|
-
document.body.appendChild(a);
|
|
104
|
-
a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, button: 0, metaKey: true }));
|
|
105
|
-
await settle();
|
|
106
|
-
window.removeEventListener('click', stop);
|
|
107
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
108
|
-
});
|
|
109
|
-
it('stores NavigateOptions.state on the history entry', async () => {
|
|
110
|
-
await krateRouter.push('/about', { state: { from: 'test' } });
|
|
111
|
-
expect(getHistoryState()).toEqual({ from: 'test' });
|
|
112
|
-
expect(location.pathname).toBe('/about');
|
|
113
|
-
});
|
|
114
|
-
it('replace() does not add a history entry', async () => {
|
|
115
|
-
const before = history.length;
|
|
116
|
-
await krateRouter.replace('/about');
|
|
117
|
-
expect(history.length).toBe(before);
|
|
118
|
-
expect(location.pathname).toBe('/about');
|
|
119
|
-
});
|
|
120
|
-
it('navigate() drives the SPA router and returns false', async () => {
|
|
121
|
-
expect(navigate('/about')).toBe(false);
|
|
122
|
-
await settle();
|
|
123
|
-
expect(document.title).toBe('About');
|
|
124
|
-
});
|
|
125
|
-
it('follows the final URL after a redirect', async () => {
|
|
126
|
-
routes['/old'] = { body: page('About', '<h1>About</h1>'), redirect: '/about?moved=1' };
|
|
127
|
-
await krateRouter.push('/old');
|
|
128
|
-
expect(location.pathname + location.search).toBe('/about?moved=1');
|
|
129
|
-
});
|
|
130
|
-
it('hard-navigates for non-HTML responses', async () => {
|
|
131
|
-
routes['/file.json'] = { body: '{}', type: 'application/json' };
|
|
132
|
-
await krateRouter.push('/file.json');
|
|
133
|
-
expect(hardNavs).toEqual([['http://localhost:3000/file.json', false]]);
|
|
134
|
-
expect(location.pathname).toBe('/');
|
|
135
|
-
});
|
|
136
|
-
it('hard-navigates when a redirect leaves the origin', async () => {
|
|
137
|
-
routes['/out'] = { body: page('x', ''), redirect: 'https://example.com/elsewhere' };
|
|
138
|
-
await krateRouter.push('/out');
|
|
139
|
-
expect(hardNavs[0][0]).toBe('https://example.com/elsewhere');
|
|
140
|
-
});
|
|
141
|
-
it('cancels a navigation when krate:navigate-start is prevented', async () => {
|
|
142
|
-
const stop = (e) => e.preventDefault();
|
|
143
|
-
window.addEventListener('krate:navigate-start', stop);
|
|
144
|
-
await krateRouter.push('/about');
|
|
145
|
-
window.removeEventListener('krate:navigate-start', stop);
|
|
146
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
147
|
-
expect(location.pathname).toBe('/');
|
|
148
|
-
});
|
|
149
|
-
it('notifies subscribers with pathname, search and status', async () => {
|
|
150
|
-
const seen = [];
|
|
151
|
-
const off = subscribeNavigation((s) => seen.push(`${s.status}:${s.pathname}${s.search}`));
|
|
152
|
-
await krateRouter.push('/docs?x=1');
|
|
153
|
-
off();
|
|
154
|
-
expect(seen).toContain('loading:/');
|
|
155
|
-
expect(seen[seen.length - 1]).toBe('idle:/docs?x=1');
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
describe('view transitions', () => {
|
|
159
|
-
it('updates history and focus only after the transition callback ran', async () => {
|
|
160
|
-
globalThis.__KRATE_CFG__ = { viewTransitions: 'auto' };
|
|
161
|
-
const order = [];
|
|
162
|
-
let release;
|
|
163
|
-
const gate = new Promise((r) => (release = r));
|
|
164
|
-
document.startViewTransition = (cb) => {
|
|
165
|
-
// The browser runs the update callback asynchronously.
|
|
166
|
-
const updateCallbackDone = gate.then(() => {
|
|
167
|
-
order.push('mutate');
|
|
168
|
-
cb();
|
|
169
|
-
});
|
|
170
|
-
return { updateCallbackDone, finished: updateCallbackDone };
|
|
171
|
-
};
|
|
172
|
-
const origPush = history.pushState.bind(history);
|
|
173
|
-
const spy = vi.spyOn(history, 'pushState').mockImplementation((...args) => {
|
|
174
|
-
order.push('push:' + document.title);
|
|
175
|
-
origPush(...args);
|
|
176
|
-
});
|
|
177
|
-
const p = krateRouter.push('/about');
|
|
178
|
-
await settle();
|
|
179
|
-
expect(order).toEqual([]);
|
|
180
|
-
release();
|
|
181
|
-
await p;
|
|
182
|
-
spy.mockRestore();
|
|
183
|
-
expect(order).toEqual(['mutate', 'push:About']);
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
describe('view transition names', () => {
|
|
187
|
-
it('names elements and applies their transition class after a swap', async () => {
|
|
188
|
-
routes['/vt'] = {
|
|
189
|
-
body: page('VT', '<h1 id="hero" data-view-transition="hero" data-view-transition-class="slow-fade">x</h1>'),
|
|
190
|
-
};
|
|
191
|
-
await krateRouter.push('/vt');
|
|
192
|
-
const hero = document.getElementById('hero');
|
|
193
|
-
expect(hero.style.viewTransitionName).toBe('hero');
|
|
194
|
-
expect(hero.style.getPropertyValue('view-transition-class')).toBe('slow-fade');
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
describe('hash navigation', () => {
|
|
198
|
-
it('handles hash-only links in place without fetching', async () => {
|
|
199
|
-
document.getElementById('root').innerHTML = `${link('#sec', 'Sec')}<div id="sec">x</div>`;
|
|
200
|
-
const scrolled = vi.fn();
|
|
201
|
-
document.getElementById('sec').scrollIntoView = scrolled;
|
|
202
|
-
click(document.querySelector('a'));
|
|
203
|
-
await settle();
|
|
204
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
205
|
-
expect(location.hash).toBe('#sec');
|
|
206
|
-
expect(scrolled).toHaveBeenCalled();
|
|
207
|
-
});
|
|
208
|
-
it('does not refetch on a hash-only popstate', async () => {
|
|
209
|
-
history.pushState(history.state, '', '/#a');
|
|
210
|
-
window.dispatchEvent(new PopStateEvent('popstate', { state: history.state }));
|
|
211
|
-
await settle();
|
|
212
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
213
|
-
});
|
|
214
|
-
it('a same-page link with a hash scrolls instead of fetching', async () => {
|
|
215
|
-
await krateRouter.push('/docs');
|
|
216
|
-
fetchMock.mockClear();
|
|
217
|
-
await krateRouter.push('/docs#part');
|
|
218
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
219
|
-
expect(location.href).toBe('http://localhost:3000/docs#part');
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
describe('scroll restoration', () => {
|
|
223
|
-
it('restores the scroll position of the entry being returned to', async () => {
|
|
224
|
-
routes['/'] = { body: page('Home', '<h1>Home</h1>') };
|
|
225
|
-
const homeState = history.state;
|
|
226
|
-
window.scrollTo(0, 420);
|
|
227
|
-
await krateRouter.push('/about');
|
|
228
|
-
expect(window.scrollY).toBe(0);
|
|
229
|
-
const aboutState = history.state;
|
|
230
|
-
window.scrollTo(0, 77);
|
|
231
|
-
// Simulate the browser's history step back to "/": the URL and state
|
|
232
|
-
// change first, then popstate fires.
|
|
233
|
-
history.replaceState(homeState, '', '/');
|
|
234
|
-
window.dispatchEvent(new PopStateEvent('popstate', { state: homeState }));
|
|
235
|
-
await settle();
|
|
236
|
-
expect(document.title).toBe('Home');
|
|
237
|
-
expect(window.scrollY).toBe(420);
|
|
238
|
-
history.replaceState(aboutState, '', '/about');
|
|
239
|
-
window.dispatchEvent(new PopStateEvent('popstate', { state: aboutState }));
|
|
240
|
-
await settle();
|
|
241
|
-
expect(document.title).toBe('About');
|
|
242
|
-
expect(window.scrollY).toBe(77);
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
describe('error handling', () => {
|
|
246
|
-
it('shows a server-rendered 404 document in place and reports the status', async () => {
|
|
247
|
-
const errs = events('krate:navigate-error');
|
|
248
|
-
routes['/missing'] = { status: 404, body: page('Not found', '<h1>Not found</h1>') };
|
|
249
|
-
await krateRouter.push('/missing');
|
|
250
|
-
expect(document.title).toBe('Not found');
|
|
251
|
-
expect(location.pathname).toBe('/missing');
|
|
252
|
-
expect(errs).toEqual([{ url: 'http://localhost:3000/missing', status: 404 }]);
|
|
253
|
-
expect(hardNavs).toEqual([]);
|
|
254
|
-
});
|
|
255
|
-
it('falls back to <basePath>/404.html when the 404 response is not HTML', async () => {
|
|
256
|
-
globalThis.__KRATE_CFG__ = { viewTransitions: false, basePath: '/app' };
|
|
257
|
-
routes['/app/gone'] = { status: 404, body: 'nope', type: 'text/plain' };
|
|
258
|
-
routes['/app/404.html'] = { body: page('404', '<h1>Lost</h1>') };
|
|
259
|
-
await krateRouter.push('/app/gone');
|
|
260
|
-
expect(document.querySelector('#root h1').textContent).toBe('Lost');
|
|
261
|
-
expect(fetchMock.mock.calls.map((c) => new URL(c[0], location.href).pathname)).toContain('/app/404.html');
|
|
262
|
-
});
|
|
263
|
-
it('uses /500.html for server errors without an HTML body', async () => {
|
|
264
|
-
routes['/boom'] = { status: 503, body: '', type: 'text/plain' };
|
|
265
|
-
routes['/500.html'] = { body: page('Error', '<h1>Broken</h1>') };
|
|
266
|
-
await krateRouter.push('/boom');
|
|
267
|
-
expect(document.querySelector('#root h1').textContent).toBe('Broken');
|
|
268
|
-
});
|
|
269
|
-
it('does not show the 404 page for 403 responses', async () => {
|
|
270
|
-
routes['/secret'] = { status: 403, body: 'forbidden', type: 'text/plain' };
|
|
271
|
-
routes['/404.html'] = { body: page('404', '<h1>Lost</h1>') };
|
|
272
|
-
await krateRouter.push('/secret');
|
|
273
|
-
expect(hardNavs).toEqual([['http://localhost:3000/secret', false]]);
|
|
274
|
-
expect(document.querySelector('#root h1').textContent).toBe('Home');
|
|
275
|
-
});
|
|
276
|
-
it('emits krate:navigate-error on network failure, then hard-navigates', async () => {
|
|
277
|
-
const errs = events('krate:navigate-error');
|
|
278
|
-
routes['/net'] = { fail: new TypeError('Failed to fetch') };
|
|
279
|
-
await krateRouter.push('/net');
|
|
280
|
-
expect(errs.length).toBe(1);
|
|
281
|
-
expect(errs[0].status).toBe(0);
|
|
282
|
-
expect(hardNavs).toEqual([['http://localhost:3000/net', false]]);
|
|
283
|
-
});
|
|
284
|
-
it('lets the app cancel the hard-reload fallback', async () => {
|
|
285
|
-
const stop = (e) => e.preventDefault();
|
|
286
|
-
window.addEventListener('krate:navigate-error', stop);
|
|
287
|
-
routes['/net'] = { fail: new TypeError('Failed to fetch') };
|
|
288
|
-
await krateRouter.push('/net');
|
|
289
|
-
window.removeEventListener('krate:navigate-error', stop);
|
|
290
|
-
expect(hardNavs).toEqual([]);
|
|
291
|
-
expect(location.pathname).toBe('/');
|
|
292
|
-
});
|
|
293
|
-
it('never uses document.write', async () => {
|
|
294
|
-
const spy = vi.spyOn(document, 'write');
|
|
295
|
-
routes['/missing'] = { status: 404, body: page('Not found', '<h1>Not found</h1>') };
|
|
296
|
-
await krateRouter.push('/missing');
|
|
297
|
-
expect(spy).not.toHaveBeenCalled();
|
|
298
|
-
});
|
|
299
|
-
});
|
|
300
|
-
describe('races', () => {
|
|
301
|
-
it('a slower superseded navigation never overwrites a newer one', async () => {
|
|
302
|
-
let open;
|
|
303
|
-
routes['/slow'] = { body: page('Slow', '<h1>Slow</h1>'), gate: new Promise((r) => (open = r)) };
|
|
304
|
-
const first = krateRouter.push('/slow');
|
|
305
|
-
const second = krateRouter.push('/about');
|
|
306
|
-
await second;
|
|
307
|
-
open();
|
|
308
|
-
await first;
|
|
309
|
-
await settle();
|
|
310
|
-
expect(document.title).toBe('About');
|
|
311
|
-
expect(location.pathname).toBe('/about');
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
describe('prefetch cache', () => {
|
|
315
|
-
it('serves a prefetched document once, then refetches', async () => {
|
|
316
|
-
await krateRouter.prefetch('/about');
|
|
317
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
318
|
-
await krateRouter.push('/about');
|
|
319
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
320
|
-
routes['/'] = { body: page('Home', '<h1>Home</h1>') };
|
|
321
|
-
await krateRouter.push('/');
|
|
322
|
-
await krateRouter.push('/about');
|
|
323
|
-
// The cache hit was consumed: revisiting fetches fresh HTML.
|
|
324
|
-
expect(fetchMock.mock.calls.filter((c) => String(c[0]).endsWith('/about')).length).toBe(2);
|
|
325
|
-
});
|
|
326
|
-
it('keeps the fragment of the link when the document was prefetched', async () => {
|
|
327
|
-
routes['/about'] = { body: page('About', '<h1>About</h1><h2 id="team">Team</h2>') };
|
|
328
|
-
const original = Element.prototype.scrollIntoView;
|
|
329
|
-
const scrolled = vi.fn();
|
|
330
|
-
Element.prototype.scrollIntoView = scrolled;
|
|
331
|
-
try {
|
|
332
|
-
await krateRouter.prefetch('/about#team');
|
|
333
|
-
await krateRouter.push('/about#team');
|
|
334
|
-
await settle();
|
|
335
|
-
expect(location.pathname + location.hash).toBe('/about#team');
|
|
336
|
-
expect(scrolled).toHaveBeenCalled();
|
|
337
|
-
}
|
|
338
|
-
finally {
|
|
339
|
-
Element.prototype.scrollIntoView = original;
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
it('expires entries after the TTL', async () => {
|
|
343
|
-
globalThis.__KRATE_CFG__ = { viewTransitions: false, prefetchTTL: 0 };
|
|
344
|
-
await krateRouter.prefetch('/about');
|
|
345
|
-
await new Promise((r) => setTimeout(r, 5));
|
|
346
|
-
await krateRouter.push('/about');
|
|
347
|
-
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
348
|
-
});
|
|
349
|
-
it('invalidatePrefetchCache drops entries', async () => {
|
|
350
|
-
await krateRouter.prefetch('/about');
|
|
351
|
-
invalidatePrefetchCache('/about');
|
|
352
|
-
await krateRouter.push('/about');
|
|
353
|
-
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
354
|
-
});
|
|
355
|
-
it('does not cache error responses', async () => {
|
|
356
|
-
routes['/missing'] = { status: 404, body: page('Not found', '') };
|
|
357
|
-
await krateRouter.prefetch('/missing');
|
|
358
|
-
await krateRouter.push('/missing');
|
|
359
|
-
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
describe('refresh and revalidation', () => {
|
|
363
|
-
it('refresh() refetches the current route bypassing the cache and keeps history', async () => {
|
|
364
|
-
await krateRouter.push('/about');
|
|
365
|
-
routes['/about'] = { body: page('About v2', '<h1>About v2</h1>') };
|
|
366
|
-
await krateRouter.prefetch('/about');
|
|
367
|
-
const len = history.length;
|
|
368
|
-
window.scrollTo(0, 33);
|
|
369
|
-
await krateRouter.refresh();
|
|
370
|
-
expect(document.title).toBe('About v2');
|
|
371
|
-
expect(history.length).toBe(len);
|
|
372
|
-
expect(window.scrollY).toBe(33);
|
|
373
|
-
const last = fetchMock.mock.calls[fetchMock.mock.calls.length - 1];
|
|
374
|
-
expect(last[1].cache).toBe('no-cache');
|
|
375
|
-
});
|
|
376
|
-
it('krate:revalidate refreshes the current route', async () => {
|
|
377
|
-
await krateRouter.push('/about');
|
|
378
|
-
routes['/about'] = { body: page('Fresh', '<h1>Fresh</h1>') };
|
|
379
|
-
window.dispatchEvent(new CustomEvent('krate:revalidate', { detail: { paths: ['/about'] } }));
|
|
380
|
-
await settle();
|
|
381
|
-
expect(document.title).toBe('Fresh');
|
|
382
|
-
});
|
|
383
|
-
});
|
|
384
|
-
describe('active links', () => {
|
|
385
|
-
it('marks exact matches with aria-current and segments separately', async () => {
|
|
386
|
-
await krateRouter.push('/docs/intro');
|
|
387
|
-
const docs = document.querySelector('a[href="/docs"]');
|
|
388
|
-
const old = document.querySelector('a[href="/docs-old"]');
|
|
389
|
-
expect(docs.getAttribute('aria-current')).toBeNull();
|
|
390
|
-
expect(docs.getAttribute('data-krate-active')).toBe('segment');
|
|
391
|
-
expect(old.hasAttribute('data-krate-active')).toBe(false);
|
|
392
|
-
await krateRouter.push('/docs');
|
|
393
|
-
expect(document.querySelector('a[href="/docs"]').getAttribute('aria-current')).toBe('page');
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
describe('forms', () => {
|
|
397
|
-
it('intercepts GET form submissions', async () => {
|
|
398
|
-
routes['/search'] = { body: page('Search', '<h1>Results</h1>') };
|
|
399
|
-
document.getElementById('root').innerHTML =
|
|
400
|
-
'<form action="/search"><input name="q" value="krate"><button name="go" value="1">Go</button></form>';
|
|
401
|
-
const form = document.querySelector('form');
|
|
402
|
-
const ev = new Event('submit', { bubbles: true, cancelable: true });
|
|
403
|
-
ev.submitter = form.querySelector('button');
|
|
404
|
-
form.dispatchEvent(ev);
|
|
405
|
-
expect(ev.defaultPrevented).toBe(true);
|
|
406
|
-
await settle();
|
|
407
|
-
expect(location.pathname + location.search).toBe('/search?q=krate&go=1');
|
|
408
|
-
expect(document.title).toBe('Search');
|
|
409
|
-
});
|
|
410
|
-
it('leaves POST forms alone', async () => {
|
|
411
|
-
document.getElementById('root').innerHTML = '<form method="post" action="/search"><input name="q"></form>';
|
|
412
|
-
const ev = new Event('submit', { bubbles: true, cancelable: true });
|
|
413
|
-
document.querySelector('form').dispatchEvent(ev);
|
|
414
|
-
expect(ev.defaultPrevented).toBe(false);
|
|
415
|
-
});
|
|
416
|
-
});
|
|
417
|
-
describe('focus', () => {
|
|
418
|
-
it('focuses the root temporarily and removes tabindex on blur', async () => {
|
|
419
|
-
await krateRouter.push('/about');
|
|
420
|
-
const root = document.getElementById('root');
|
|
421
|
-
expect(document.activeElement).toBe(root);
|
|
422
|
-
expect(root.getAttribute('tabindex')).toBe('-1');
|
|
423
|
-
root.blur();
|
|
424
|
-
expect(root.hasAttribute('tabindex')).toBe(false);
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
describe('fallback fade and reduced motion', () => {
|
|
428
|
-
function stubReducedMotion(reduce) {
|
|
429
|
-
vi.stubGlobal('matchMedia', (q) => ({
|
|
430
|
-
matches: reduce && q.includes('prefers-reduced-motion'),
|
|
431
|
-
media: q,
|
|
432
|
-
addEventListener() { },
|
|
433
|
-
removeEventListener() { },
|
|
434
|
-
addListener() { },
|
|
435
|
-
removeListener() { },
|
|
436
|
-
}));
|
|
437
|
-
window.matchMedia = globalThis.matchMedia;
|
|
438
|
-
}
|
|
439
|
-
async function rootStyleDuringFetch() {
|
|
440
|
-
let release;
|
|
441
|
-
routes['/about'].gate = new Promise((r) => (release = r));
|
|
442
|
-
const p = krateRouter.push('/about');
|
|
443
|
-
await settle();
|
|
444
|
-
const el = document.getElementById('root');
|
|
445
|
-
const seen = { transition: el.style.transition, opacity: el.style.opacity };
|
|
446
|
-
release();
|
|
447
|
-
await p;
|
|
448
|
-
await settle();
|
|
449
|
-
return seen;
|
|
450
|
-
}
|
|
451
|
-
it('applies the opacity fade without View Transitions', async () => {
|
|
452
|
-
stubReducedMotion(false);
|
|
453
|
-
const seen = await rootStyleDuringFetch();
|
|
454
|
-
expect(seen.opacity).toBe('0');
|
|
455
|
-
expect(seen.transition).toContain('opacity');
|
|
456
|
-
});
|
|
457
|
-
it('applies no fade under prefers-reduced-motion', async () => {
|
|
458
|
-
stubReducedMotion(true);
|
|
459
|
-
const seen = await rootStyleDuringFetch();
|
|
460
|
-
expect(seen.opacity).toBe('');
|
|
461
|
-
expect(seen.transition).toBe('');
|
|
462
|
-
expect(document.querySelector('#root h1').textContent).toBe('About');
|
|
463
|
-
});
|
|
464
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
-
import { initRouter, destroyRouter, krateRouter, matchRoutePattern, compareRouteSpecificity } from './router';
|
|
3
|
-
import { disposeAll, krateDisposeExcept, krateDisposeScopes, krateKeepScopes } from './signal';
|
|
4
|
-
const KEY = '_sabc_modal';
|
|
5
|
-
function chain(src, scope) {
|
|
6
|
-
const info = {
|
|
7
|
-
v: 1,
|
|
8
|
-
chain: [{ id: '_lroot_', k: 'layout', d: 0, p: true, s: [{ n: 'modal', key: KEY, scope, src, i: ['photo/[id]'] }] }],
|
|
9
|
-
};
|
|
10
|
-
return `<script type="application/json" id="__krate_route">${JSON.stringify(info)}</script>`;
|
|
11
|
-
}
|
|
12
|
-
function shell(children, slot) {
|
|
13
|
-
return `<nav id="nav">nav</nav><main><!--kl:_lroot_-->${children}<!--/kl:_lroot_--></main><aside id="slot"><!--ks:${KEY}-->${slot}<!--/ks:${KEY}--></aside>`;
|
|
14
|
-
}
|
|
15
|
-
function doc(title, root, info) {
|
|
16
|
-
return `<!doctype html><html><head><title>${title}</title></head><body><div id="root">${root}</div>${info}</body></html>`;
|
|
17
|
-
}
|
|
18
|
-
function fragment(id) {
|
|
19
|
-
const info = { v: 1, frag: { key: KEY, scope: '_lfrag_', target: 'photo/[id]' } };
|
|
20
|
-
return `<!doctype html><html><head><title>x</title></head><body><div id="root"><!--ks:${KEY}--><div id="modal">Modal ${id}</div><!--/ks:${KEY}--></div><script type="application/json" id="__krate_route">${JSON.stringify(info)}</script></body></html>`;
|
|
21
|
-
}
|
|
22
|
-
let routes;
|
|
23
|
-
let fetchMock;
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
destroyRouter();
|
|
26
|
-
disposeAll();
|
|
27
|
-
const g = globalThis;
|
|
28
|
-
g.__KRATE_CFG__ = { viewTransitions: false };
|
|
29
|
-
g.krateDisposeExcept = krateDisposeExcept;
|
|
30
|
-
g.krateDisposeScopes = krateDisposeScopes;
|
|
31
|
-
g.krateKeepScopes = krateKeepScopes;
|
|
32
|
-
g.disposeAll = disposeAll;
|
|
33
|
-
g.__krateHardNavigate = () => { };
|
|
34
|
-
routes = {
|
|
35
|
-
'/_krate/i/_sabc_modal/photo/1': { body: fragment('1') },
|
|
36
|
-
'/_krate/i/_sabc_modal/photo/2': { body: fragment('2') },
|
|
37
|
-
'/about': { body: doc('About', shell('<p id="about">about</p>', '<i id="def"></i>'), chain('default', '_ldef_')) },
|
|
38
|
-
'/photo/1': { body: doc('Photo', shell('<h1 id="photo">photo</h1>', '<i id="def"></i>'), chain('default', '_ldef_')) },
|
|
39
|
-
};
|
|
40
|
-
fetchMock = vi.fn(async (input) => {
|
|
41
|
-
const u = new URL(input, location.href);
|
|
42
|
-
const r = routes[u.pathname];
|
|
43
|
-
const status = r ? (r.status ?? 200) : 404;
|
|
44
|
-
return {
|
|
45
|
-
ok: status < 300,
|
|
46
|
-
status,
|
|
47
|
-
url: u.href,
|
|
48
|
-
headers: new Headers({ 'content-type': r ? 'text/html' : 'text/plain' }),
|
|
49
|
-
text: async () => (r ? r.body : 'missing'),
|
|
50
|
-
};
|
|
51
|
-
});
|
|
52
|
-
vi.stubGlobal('fetch', fetchMock);
|
|
53
|
-
history.replaceState(null, '', '/');
|
|
54
|
-
document.head.innerHTML = '<title>Feed</title>';
|
|
55
|
-
document.body.innerHTML = `<div id="root">${shell('<section id="feed">feed</section>', '<i id="def"></i>')}</div>${chain('default', '_ldef_')}`;
|
|
56
|
-
initRouter();
|
|
57
|
-
});
|
|
58
|
-
afterEach(() => {
|
|
59
|
-
destroyRouter();
|
|
60
|
-
vi.unstubAllGlobals();
|
|
61
|
-
const g = globalThis;
|
|
62
|
-
for (const k of ['__KRATE_CFG__', 'krateDisposeExcept', 'krateDisposeScopes', 'krateKeepScopes', 'disposeAll', '__krateHardNavigate'])
|
|
63
|
-
delete g[k];
|
|
64
|
-
});
|
|
65
|
-
async function settle() {
|
|
66
|
-
for (let i = 0; i < 20; i++)
|
|
67
|
-
await Promise.resolve();
|
|
68
|
-
await new Promise((r) => setTimeout(r, 0));
|
|
69
|
-
}
|
|
70
|
-
function pop(state, url) {
|
|
71
|
-
history.replaceState(state, '', url);
|
|
72
|
-
window.dispatchEvent(new PopStateEvent('popstate', { state }));
|
|
73
|
-
}
|
|
74
|
-
describe('intercepting routes', () => {
|
|
75
|
-
it('soft navigation renders the intercepting fragment into the slot', async () => {
|
|
76
|
-
const feed = document.getElementById('feed');
|
|
77
|
-
await krateRouter.push('/photo/1');
|
|
78
|
-
expect(fetchMock.mock.calls.map((c) => new URL(c[0]).pathname)).toEqual(['/_krate/i/_sabc_modal/photo/1']);
|
|
79
|
-
expect(location.pathname).toBe('/photo/1');
|
|
80
|
-
expect(document.getElementById('modal').textContent).toBe('Modal 1');
|
|
81
|
-
expect(document.getElementById('feed')).toBe(feed);
|
|
82
|
-
expect(document.getElementById('def')).toBeNull();
|
|
83
|
-
});
|
|
84
|
-
it('back restores the slot content without refetching; forward reopens it', async () => {
|
|
85
|
-
const homeState = history.state;
|
|
86
|
-
const def = document.getElementById('def');
|
|
87
|
-
await krateRouter.push('/photo/1');
|
|
88
|
-
const photoState = history.state;
|
|
89
|
-
fetchMock.mockClear();
|
|
90
|
-
pop(homeState, '/');
|
|
91
|
-
await settle();
|
|
92
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
93
|
-
expect(document.getElementById('modal')).toBeNull();
|
|
94
|
-
expect(document.getElementById('def')).toBe(def);
|
|
95
|
-
pop(photoState, '/photo/1');
|
|
96
|
-
await settle();
|
|
97
|
-
expect(document.getElementById('modal').textContent).toBe('Modal 1');
|
|
98
|
-
});
|
|
99
|
-
it('chained intercepts replace the fragment', async () => {
|
|
100
|
-
await krateRouter.push('/photo/1');
|
|
101
|
-
await krateRouter.push('/photo/2');
|
|
102
|
-
expect(document.getElementById('modal').textContent).toBe('Modal 2');
|
|
103
|
-
expect(document.querySelectorAll('#modal').length).toBe(1);
|
|
104
|
-
});
|
|
105
|
-
it('a soft navigation without a slot match keeps the slot content', async () => {
|
|
106
|
-
await krateRouter.push('/photo/1');
|
|
107
|
-
await krateRouter.push('/about');
|
|
108
|
-
expect(document.getElementById('about')).not.toBeNull();
|
|
109
|
-
expect(document.getElementById('modal').textContent).toBe('Modal 1');
|
|
110
|
-
});
|
|
111
|
-
it('a slot page match on the new route replaces the slot', async () => {
|
|
112
|
-
routes['/settings'] = {
|
|
113
|
-
body: doc('Settings', shell('<p id="settings">s</p>', '<b id="slot-settings"></b>'), chain('page', '_lset_')),
|
|
114
|
-
};
|
|
115
|
-
await krateRouter.push('/photo/1');
|
|
116
|
-
await krateRouter.push('/settings');
|
|
117
|
-
expect(document.getElementById('modal')).toBeNull();
|
|
118
|
-
expect(document.getElementById('slot-settings')).not.toBeNull();
|
|
119
|
-
});
|
|
120
|
-
it('falls back to the real page (with an error event) when the fragment is missing', async () => {
|
|
121
|
-
delete routes['/_krate/i/_sabc_modal/photo/1'];
|
|
122
|
-
const errs = [];
|
|
123
|
-
const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
124
|
-
window.addEventListener('krate:navigate-error', (e) => errs.push(e.detail));
|
|
125
|
-
await krateRouter.push('/photo/1');
|
|
126
|
-
await settle();
|
|
127
|
-
warn.mockRestore();
|
|
128
|
-
expect(errs.length).toBeGreaterThan(0);
|
|
129
|
-
expect(document.getElementById('photo')).not.toBeNull();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
describe('route patterns', () => {
|
|
133
|
-
it('matches catch-alls like the compiler', () => {
|
|
134
|
-
expect(matchRoutePattern('/docs/a/b', 'docs/[...slug]')).toEqual({ slug: 'a/b' });
|
|
135
|
-
expect(matchRoutePattern('/docs', 'docs/[...slug]')).toBeNull();
|
|
136
|
-
expect(matchRoutePattern('/shop', 'shop/[[...f]]')).toEqual({ f: '' });
|
|
137
|
-
expect(matchRoutePattern('/photo/7', 'photo/[id]')).toEqual({ id: '7' });
|
|
138
|
-
});
|
|
139
|
-
it('orders static > dynamic > catch-all > optional catch-all', () => {
|
|
140
|
-
const list = ['shop/[[...f]]', 'shop/[...f]', 'shop/[id]', 'shop/cart'];
|
|
141
|
-
list.sort(compareRouteSpecificity);
|
|
142
|
-
expect(list).toEqual(['shop/cart', 'shop/[id]', 'shop/[...f]', 'shop/[[...f]]']);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, afterEach, vi } from 'vitest';
|
|
2
|
-
import { initRouter, destroyRouter } from './router';
|
|
3
|
-
function boot(head) {
|
|
4
|
-
destroyRouter();
|
|
5
|
-
globalThis.__KRATE_CFG__ = { viewTransitions: false };
|
|
6
|
-
document.head.innerHTML = '<title>x</title>' + head;
|
|
7
|
-
document.body.innerHTML = '<div id="root"></div>';
|
|
8
|
-
initRouter();
|
|
9
|
-
}
|
|
10
|
-
describe('router speculation rules', () => {
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
destroyRouter();
|
|
13
|
-
vi.unstubAllGlobals();
|
|
14
|
-
});
|
|
15
|
-
it('injects the rules when the browser supports them and no CSP meta exists', () => {
|
|
16
|
-
HTMLScriptElement.supports = (t) => t === 'speculationrules';
|
|
17
|
-
boot('');
|
|
18
|
-
expect(document.querySelector('script[type="speculationrules"]')).not.toBeNull();
|
|
19
|
-
});
|
|
20
|
-
it('skips the inline rules under a CSP meta', () => {
|
|
21
|
-
HTMLScriptElement.supports = (t) => t === 'speculationrules';
|
|
22
|
-
boot('<meta http-equiv="Content-Security-Policy" content="script-src \'self\'">');
|
|
23
|
-
expect(document.querySelector('script[type="speculationrules"]')).toBeNull();
|
|
24
|
-
});
|
|
25
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|