Skip to content

Commit 1a6f8ec

Browse files
committed
feat!: Simplify RouteInfo type
1 parent b10784d commit 1a6f8ec

File tree

8 files changed

+94
-133
lines changed

8 files changed

+94
-133
lines changed

src/lib/Redirector.svelte.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, vi, type MockInstance
22
import { testWithEffect as test } from "$test/testWithEffect.svelte.js";
33
import { ALL_HASHES, ROUTING_UNIVERSES } from "$test/test-utils.js";
44
import { init } from "$lib/init.js";
5-
import type { Hash, PatternRouteInfo, RedirectedRouteInfo } from "$lib/types.js";
5+
import type { Hash, RouteInfo, RedirectedRouteInfo } from "$lib/types.js";
66
import { resolveHashValue } from "./kernel/resolveHashValue.js";
77
import { Redirector } from "./Redirector.svelte.js";
88
import { location } from "./kernel/Location.js";
@@ -48,27 +48,27 @@ ROUTING_UNIVERSES.forEach((universe) => {
4848
})[] = [
4949
{
5050
triggerUrl: '/old/path',
51-
pattern: '/old/path',
51+
path: '/old/path',
5252
href: '/new/path',
5353
expectedPath: '/new/path',
5454
text: "Static pattern; static href"
5555
},
5656
{
57-
pattern: '/old-path/:id',
57+
path: '/old-path/:id',
5858
triggerUrl: '/old-path/123',
5959
expectedPath: '/new-path/123',
6060
href: (rp) => `/new-path/${rp?.id}`,
6161
text: "Parameterized pattern; dynamic href"
6262
},
6363
{
64-
pattern: '/old-path/*',
64+
path: '/old-path/*',
6565
triggerUrl: '/old-path/any/number/of/segments',
6666
expectedPath: '/new-path/any/number/of/segments',
6767
href: (rp) => `/new-path${rp?.rest}`,
6868
text: "Rest parameter; dynamic href"
6969
},
7070
{
71-
pattern: '/conditional/:id',
71+
path: '/conditional/:id',
7272
triggerUrl: '/conditional/123',
7373
expectedPath: '/allowed/123',
7474
href: (rp) => `/allowed/${rp?.id}`,
@@ -104,7 +104,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
104104

105105
// Act.
106106
redirector.redirections.push({
107-
pattern: '/old-path',
107+
path: '/old-path',
108108
href: '/new-path',
109109
goTo: true,
110110
});
@@ -123,7 +123,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
123123

124124
// Act.
125125
redirector.redirections.push({
126-
pattern: '/conditional/:id',
126+
path: '/conditional/:id',
127127
href: '/not-allowed',
128128
and: (rp) => (rp?.id as number) > 100,
129129
});
@@ -143,11 +143,11 @@ ROUTING_UNIVERSES.forEach((universe) => {
143143
// Act.
144144
redirector.redirections.push(
145145
{
146-
pattern: '/multi/*',
146+
path: '/multi/*',
147147
href: '/first-match',
148148
},
149149
{
150-
pattern: '/multi/test',
150+
path: '/multi/test',
151151
href: '/second-match',
152152
}
153153
);
@@ -166,7 +166,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
166166

167167
// Act.
168168
redirector.redirections.push({
169-
pattern: '/test-replace',
169+
path: '/test-replace',
170170
href: '/replaced',
171171
});
172172
flushSync();
@@ -185,7 +185,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
185185

186186
// Act.
187187
redirector.redirections.push({
188-
pattern: '/with-options',
188+
path: '/with-options',
189189
href: '/target',
190190
options: { preserveQuery: true, state: { custom: 'data' } }
191191
});
@@ -205,15 +205,15 @@ ROUTING_UNIVERSES.forEach((universe) => {
205205

206206
// Add initial redirection that won't match
207207
redirector.redirections.push({
208-
pattern: '/different-path',
208+
path: '/different-path',
209209
href: '/not-relevant'
210210
});
211211
flushSync();
212212
navigateSpy.mockClear();
213213

214214
// Act.
215215
redirector.redirections.push({
216-
pattern: '/test-reactivity',
216+
path: '/test-reactivity',
217217
href: '/should-redirect'
218218
});
219219
flushSync();
@@ -227,14 +227,14 @@ ROUTING_UNIVERSES.forEach((universe) => {
227227
location.navigate('/test-reactivity', { hash: universe.hash });
228228
const redirector = new Redirector(universe.hash);
229229
redirector.redirections.push({
230-
pattern: '/different-path',
230+
path: '/different-path',
231231
href: '/punch-line'
232232
});
233233
flushSync();
234234
navigateSpy.mockClear();
235235

236236
// Act.
237-
(redirector.redirections[0] as PatternRouteInfo).pattern = '/test-reactivity';
237+
redirector.redirections[0].path = '/test-reactivity';
238238
flushSync();
239239

240240
// Assert.
@@ -251,7 +251,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
251251
// Act.
252252
const redirector = new Redirector(universe.hash, { replace: false });
253253
redirector.redirections.push({
254-
pattern: '/explicit-hash',
254+
path: '/explicit-hash',
255255
href: '/redirected-explicit'
256256
});
257257
flushSync();
@@ -273,7 +273,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
273273
// Act.
274274
const redirector = new Redirector(universe.hash, { replace: true });
275275
redirector.redirections.push({
276-
pattern: '/hash-resolution-test',
276+
path: '/hash-resolution-test',
277277
href: '/hash-resolved'
278278
});
279279
flushSync();
@@ -299,7 +299,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
299299
// Act.
300300
const redirector = new Redirector({ replace: false });
301301
redirector.redirections.push({
302-
pattern: '/default-hash',
302+
path: '/default-hash',
303303
href: '/redirected-default'
304304
});
305305
flushSync();
@@ -319,7 +319,7 @@ ROUTING_UNIVERSES.forEach((universe) => {
319319
// Act.
320320
const redirector = new Redirector({}); // Empty options object
321321
redirector.redirections.push({
322-
pattern: '/minimal-options',
322+
path: '/minimal-options',
323323
href: '/redirected-minimal'
324324
});
325325
flushSync();
@@ -373,7 +373,7 @@ describe("Options-Only Constructor with Matching Library Defaults", () => {
373373
// Act.
374374
const redirector = new Redirector({ replace: false });
375375
redirector.redirections.push({
376-
pattern: '/hash-default-test',
376+
path: '/hash-default-test',
377377
href: '/hash-redirected'
378378
});
379379
flushSync();
@@ -414,7 +414,7 @@ describe("Options-Only Constructor with Matching Library Defaults", () => {
414414
// Act.
415415
const redirector = new Redirector({ replace: false });
416416
redirector.redirections.push({
417-
pattern: '/multi-hash-default-test',
417+
path: '/multi-hash-default-test',
418418
href: '/multi-hash-redirected'
419419
});
420420
flushSync();
@@ -461,7 +461,7 @@ describe("Cross-universe Redirection", () => {
461461

462462
// Act.
463463
redirector.redirections.push({
464-
pattern: '/old-path-route',
464+
path: '/old-path-route',
465465
href: '/new-hash-route',
466466
options: { hash: true }
467467
});
@@ -482,7 +482,7 @@ describe("Cross-universe Redirection", () => {
482482

483483
// Act.
484484
redirector.redirections.push({
485-
pattern: '/old-hash-route',
485+
path: '/old-hash-route',
486486
href: '/new-path-route',
487487
options: { hash: false } // Target path universe
488488
});
@@ -555,7 +555,7 @@ describe("Cross-universe Redirection", () => {
555555

556556
// Act.
557557
redirector.redirections.push({
558-
pattern: '/old-path-route',
558+
path: '/old-path-route',
559559
href: '/new-hash-route',
560560
options: { hash: tc.destinationHash }
561561
});

src/lib/Route/Route.svelte

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,12 @@
128128
return;
129129
}
130130
// svelte-ignore ownership_invalid_mutation
131-
untrack(() => router.routes)[key] =
132-
path instanceof RegExp
133-
? { regex: path, and: and as AndUntyped, ignoreForFallback }
134-
: {
135-
pattern: path,
136-
and: and as AndUntyped,
137-
ignoreForFallback,
138-
caseSensitive
139-
};
131+
untrack(() => router.routes)[key] = {
132+
path,
133+
and: and as AndUntyped,
134+
ignoreForFallback,
135+
caseSensitive
136+
};
140137
return () => {
141138
// svelte-ignore ownership_invalid_mutation
142139
delete untrack(() => router.routes)[key];

src/lib/Route/Route.svelte.test.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { init } from "$lib/init.js";
77
import { location } from "$lib/kernel/Location.js";
88
import TestRouteWithRouter from "$test/TestRouteWithRouter.svelte";
99
import { resetRoutingOptions, setRoutingOptions } from "$lib/kernel/options.js";
10-
import type { ExtendedRoutingOptions, RouteChildrenContext } from "$lib/types.js";
10+
import type { ExtendedRoutingOptions, RouteChildrenContext, RouteParamsRecord } from "$lib/types.js";
11+
import type { RouterEngine } from "$lib/kernel/RouterEngine.svelte.js";
1112

1213
function basicRouteTests(setup: ReturnType<typeof createRouterTestSetup>) {
1314
beforeEach(() => {
@@ -88,7 +89,7 @@ function routePropsTests(setup: ReturnType<typeof createRouterTestSetup>) {
8889
test("Should register string pattern route.", async () => {
8990
// Arrange.
9091
const { hash, context } = setup;
91-
let routerInstance: any;
92+
let routerInstance: RouterEngine;
9293

9394
// Act.
9495
render(TestRouteWithRouter, {
@@ -103,16 +104,16 @@ function routePropsTests(setup: ReturnType<typeof createRouterTestSetup>) {
103104
});
104105

105106
// Assert.
106-
const route = routerInstance?.routes["pattern-route"];
107+
const route = routerInstance!?.routes["pattern-route"];
107108
expect(route).toBeDefined();
108-
expect(route.pattern).toBe("/user/:id");
109+
expect(route.path).toBe("/user/:id");
109110
});
110111

111112
test("Should register regex route.", async () => {
112113
// Arrange.
113114
const { hash, context } = setup;
114115
const regex = /^\/user\/(?<id>\d+)$/;
115-
let routerInstance: any;
116+
let routerInstance: RouterEngine;
116117

117118
// Act.
118119
render(TestRouteWithRouter, {
@@ -127,9 +128,9 @@ function routePropsTests(setup: ReturnType<typeof createRouterTestSetup>) {
127128
});
128129

129130
// Assert.
130-
const route = routerInstance?.routes["regex-route"];
131+
const route = routerInstance!?.routes["regex-route"];
131132
expect(route).toBeDefined();
132-
expect(route.regex).toBe(regex);
133+
expect(route.path).toBe(regex);
133134
});
134135

135136
test("Should register route with and function.", async () => {
@@ -216,7 +217,7 @@ function routeParamsTests(setup: ReturnType<typeof createRouterTestSetup>) {
216217
test("Should bind route parameters.", async () => {
217218
// Arrange.
218219
const { hash, context } = setup;
219-
let routerInstance: any;
220+
let routerInstance: RouterEngine;
220221

221222
// Act.
222223
render(TestRouteWithRouter, {
@@ -231,15 +232,15 @@ function routeParamsTests(setup: ReturnType<typeof createRouterTestSetup>) {
231232
});
232233

233234
// Assert - Route should be registered with parameter pattern
234-
const route = routerInstance?.routes["param-route"];
235+
const route = routerInstance!?.routes["param-route"];
235236
expect(route).toBeDefined();
236-
expect(route.pattern).toBe("/user/:id");
237+
expect(route.path).toBe("/user/:id");
237238
});
238239

239240
test("Should handle route with rest parameter.", async () => {
240241
// Arrange.
241242
const { hash, context } = setup;
242-
let routerInstance: any;
243+
let routerInstance: RouterEngine;
243244

244245
// Act.
245246
render(TestRouteWithRouter, {
@@ -254,9 +255,9 @@ function routeParamsTests(setup: ReturnType<typeof createRouterTestSetup>) {
254255
});
255256

256257
// Assert.
257-
const route = routerInstance?.routes["rest-route"];
258+
const route = routerInstance!?.routes["rest-route"];
258259
expect(route).toBeDefined();
259-
expect(route.pattern).toBe("/files/*");
260+
expect(route.path).toBe("/files/*");
260261
});
261262
}
262263

@@ -274,7 +275,7 @@ function routeReactivityTests(setup: ReturnType<typeof createRouterTestSetup>) {
274275
const { hash, context } = setup;
275276
const initialPath = "/initial";
276277
const updatedPath = "/updated";
277-
let routerInstance: any;
278+
let routerInstance: RouterEngine;
278279

279280
const { rerender } = render(TestRouteWithRouter, {
280281
props: {
@@ -287,8 +288,8 @@ function routeReactivityTests(setup: ReturnType<typeof createRouterTestSetup>) {
287288
context
288289
});
289290

290-
const initialRoute = routerInstance?.routes["reactive-route"];
291-
expect(initialRoute?.pattern).toBe(initialPath);
291+
const initialRoute = routerInstance!?.routes["reactive-route"];
292+
expect(initialRoute?.path).toBe(initialPath);
292293

293294
// Act.
294295
await rerender({
@@ -300,8 +301,8 @@ function routeReactivityTests(setup: ReturnType<typeof createRouterTestSetup>) {
300301
});
301302

302303
// Assert.
303-
const updatedRoute = routerInstance?.routes["reactive-route"];
304-
expect(updatedRoute?.pattern).toBe(updatedPath);
304+
const updatedRoute = routerInstance!?.routes["reactive-route"];
305+
expect(updatedRoute?.path).toBe(updatedPath);
305306
});
306307

307308
test("Should update ignoreForFallback when prop changes (rerender).", async () => {
@@ -665,18 +666,17 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
665666
test("Should bind rest parameter correctly.", async () => {
666667
// Arrange.
667668
const { hash, context } = setup;
668-
let capturedParams: any;
669+
let capturedParams: RouteParamsRecord;
669670
const paramsSetter = vi.fn((value) => { capturedParams = value; });
670671

671672
// Act.
672-
render(TestRouteWithRouter, {
673+
render(Route, {
673674
props: {
674675
hash,
675-
routeKey: "test-route",
676-
routePath: "/files/*",
676+
key: "test-route",
677+
path: "/files/*",
677678
get params() { return capturedParams; },
678679
set params(value) { paramsSetter(value); },
679-
children: createTestSnippet('<div>File path: {params?.rest}</div>')
680680
},
681681
context
682682
});
@@ -692,18 +692,18 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
692692
return "http://example.com/files/documents/readme.txt";
693693
})();
694694
location.url.href = url;
695-
await vi.waitFor(() => { });
695+
flushSync();
696696

697697
// Assert.
698-
expect(paramsSetter).toHaveBeenCalled();
698+
expect(paramsSetter).toHaveBeenCalledTimes(2);
699699

700700
// Multi-hash routing (MHR) has different behavior and may not work with simple URLs in tests
701701
if (ru.text === 'MHR') {
702702
// Skip assertion for MHR as it requires more complex setup
703703
return;
704704
}
705705

706-
expect(capturedParams).toEqual({ rest: "/documents/readme.txt" });
706+
expect(capturedParams!).toEqual({ rest: "/documents/readme.txt" });
707707
});
708708
}
709709

0 commit comments

Comments
 (0)