Skip to content

Commit 8b8b72c

Browse files
committed
chore(tests): Add implicit MHR universe to unit testing
1 parent 56b92ec commit 8b8b72c

File tree

7 files changed

+166
-45
lines changed

7 files changed

+166
-45
lines changed

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

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,17 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
458458
});
459459

460460
// Navigate to a matching path - determine URL format based on routing mode
461-
const shouldUseHash = (ru.defaultHash === true) || (hash === true) || (typeof hash === 'string');
462-
location.url.href = shouldUseHash ? "http://example.com/#/user/123" : "http://example.com/user/123";
461+
const url = (() => {
462+
if (hash === false) return "http://example.com/user/123"; // Path routing
463+
if (hash === true) return "http://example.com/#/user/123"; // Single hash routing
464+
if (typeof hash === 'string') return `http://example.com/#${hash}=/user/123`; // Multi-hash routing
465+
// Implicit routing
466+
if (ru.defaultHash === false) return "http://example.com/user/123"; // Implicit path routing
467+
if (ru.defaultHash === true) return "http://example.com/#/user/123"; // Implicit single hash routing
468+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/user/123`; // Implicit multi-hash routing
469+
return "http://example.com/user/123"; // Default to path routing
470+
})();
471+
location.url.href = url;
463472
await vi.waitFor(() => {});
464473

465474
// Assert.
@@ -494,8 +503,16 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
494503
});
495504

496505
// Navigate to a matching path - determine URL format based on routing mode
497-
const shouldUseHash = (ru.defaultHash === true) || (hash === true) || (typeof hash === 'string');
498-
location.url.href = shouldUseHash ? "http://example.com/#/about" : "http://example.com/about";
506+
const url = (() => {
507+
if (hash === false) return "http://example.com/about";
508+
if (hash === true) return "http://example.com/#/about";
509+
if (typeof hash === 'string') return `http://example.com/#${hash}=/about`;
510+
if (ru.defaultHash === false) return "http://example.com/about";
511+
if (ru.defaultHash === true) return "http://example.com/#/about";
512+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/about`;
513+
return "http://example.com/about";
514+
})();
515+
location.url.href = url;
499516
await vi.waitFor(() => {});
500517

501518
// Assert.
@@ -524,8 +541,16 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
524541
});
525542

526543
// Navigate to a non-matching path - determine URL format based on routing mode
527-
const shouldUseHash = (ru.defaultHash === true) || (hash === true) || (typeof hash === 'string');
528-
location.url.href = shouldUseHash ? "http://example.com/#/other" : "http://example.com/other";
544+
const url = (() => {
545+
if (hash === false) return "http://example.com/other";
546+
if (hash === true) return "http://example.com/#/other";
547+
if (typeof hash === 'string') return `http://example.com/#${hash}=/other`;
548+
if (ru.defaultHash === false) return "http://example.com/other";
549+
if (ru.defaultHash === true) return "http://example.com/#/other";
550+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/other`;
551+
return "http://example.com/other";
552+
})();
553+
location.url.href = url;
529554
await vi.waitFor(() => {});
530555

531556
// Assert.
@@ -552,8 +577,16 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
552577
});
553578

554579
// Navigate to first matching path - determine URL format based on routing mode
555-
const shouldUseHash = (ru.defaultHash === true) || (hash === true) || (typeof hash === 'string');
556-
location.url.href = shouldUseHash ? "http://example.com/#/user/123" : "http://example.com/user/123";
580+
const url = (() => {
581+
if (hash === false) return "http://example.com/user/123";
582+
if (hash === true) return "http://example.com/#/user/123";
583+
if (typeof hash === 'string') return `http://example.com/#${hash}=/user/123`;
584+
if (ru.defaultHash === false) return "http://example.com/user/123";
585+
if (ru.defaultHash === true) return "http://example.com/#/user/123";
586+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/user/123`;
587+
return "http://example.com/user/123";
588+
})();
589+
location.url.href = url;
557590
await vi.waitFor(() => {});
558591

559592
const firstParams = capturedParams;
@@ -567,7 +600,16 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
567600
expect(firstParams).toEqual({ id: 123 }); // Number due to auto-conversion
568601

569602
// Act - Navigate to different matching path
570-
location.url.href = shouldUseHash ? "http://example.com/#/user/456" : "http://example.com/user/456";
603+
const url2 = (() => {
604+
if (hash === false) return "http://example.com/user/456";
605+
if (hash === true) return "http://example.com/#/user/456";
606+
if (typeof hash === 'string') return `http://example.com/#${hash}=/user/456`;
607+
if (ru.defaultHash === false) return "http://example.com/user/456";
608+
if (ru.defaultHash === true) return "http://example.com/#/user/456";
609+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/user/456`;
610+
return "http://example.com/user/456";
611+
})();
612+
location.url.href = url2;
571613
await vi.waitFor(() => {});
572614

573615
// Assert.
@@ -595,8 +637,16 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
595637
});
596638

597639
// Navigate to a matching path - determine URL format based on routing mode
598-
const shouldUseHash = (ru.defaultHash === true) || (hash === true) || (typeof hash === 'string');
599-
location.url.href = shouldUseHash ? "http://example.com/#/user/123/post/456" : "http://example.com/user/123/post/456";
640+
const url = (() => {
641+
if (hash === false) return "http://example.com/user/123/post/456";
642+
if (hash === true) return "http://example.com/#/user/123/post/456";
643+
if (typeof hash === 'string') return `http://example.com/#${hash}=/user/123/post/456`;
644+
if (ru.defaultHash === false) return "http://example.com/user/123/post/456";
645+
if (ru.defaultHash === true) return "http://example.com/#/user/123/post/456";
646+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/user/123/post/456`;
647+
return "http://example.com/user/123/post/456";
648+
})();
649+
location.url.href = url;
600650
await vi.waitFor(() => {});
601651

602652
// Assert.
@@ -631,8 +681,16 @@ function routeBindingTestsForUniverse(setup: ReturnType<typeof createRouterTestS
631681
});
632682

633683
// Navigate to a matching path - determine URL format based on routing mode
634-
const shouldUseHash = (ru.defaultHash === true) || (hash === true) || (typeof hash === 'string');
635-
location.url.href = shouldUseHash ? "http://example.com/#/files/documents/readme.txt" : "http://example.com/files/documents/readme.txt";
684+
const url = (() => {
685+
if (hash === false) return "http://example.com/files/documents/readme.txt";
686+
if (hash === true) return "http://example.com/#/files/documents/readme.txt";
687+
if (typeof hash === 'string') return `http://example.com/#${hash}=/files/documents/readme.txt`;
688+
if (ru.defaultHash === false) return "http://example.com/files/documents/readme.txt";
689+
if (ru.defaultHash === true) return "http://example.com/#/files/documents/readme.txt";
690+
if (typeof ru.defaultHash === 'string') return `http://example.com/#${ru.defaultHash}=/files/documents/readme.txt`;
691+
return "http://example.com/files/documents/readme.txt";
692+
})();
693+
location.url.href = url;
636694
await vi.waitFor(() => {});
637695

638696
// Assert.

src/lib/kernel/LocationFull.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe("LocationFull", () => {
110110
// Assert.
111111
expect(location.getState(ALL_HASHES.path)).toEqual(state.path);
112112
expect(location.getState(ALL_HASHES.single)).toEqual(state.hash.single);
113-
expect(location.getState('p1')).toEqual(state.hash.p1);
113+
expect(location.getState('tp')).toEqual(state.hash.tp);
114114
});
115115
});
116116
});

src/lib/kernel/RouterEngine.svelte.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { routePatternsKey, RouterEngine } from "./RouterEngine.svelte.js";
33
import { init } from "../init.js";
44
import { registerRouter } from "./trace.svelte.js";
55
import { location } from "./Location.js";
6-
import type { State, RouteInfo, ExtendedRoutingOptions } from "../types.js";
6+
import type { State, RouteInfo, ExtendedRoutingOptions, PatternRouteInfo } from "../types.js";
77
import { setupBrowserMocks, addRoutes, ROUTING_UNIVERSES, ALL_HASHES } from "$test/test-utils.js";
88
import { resetRoutingOptions, setRoutingOptions } from "./options.js";
99

@@ -295,7 +295,13 @@ ROUTING_UNIVERSES.forEach(universe => {
295295
expectedState = state.hash[universe.hash];
296296
} else {
297297
// For implicit modes (hash === undefined), the behavior depends on defaultHash
298-
expectedState = universe.defaultHash === false ? state.path : state.hash.single;
298+
if (universe.defaultHash === false) {
299+
expectedState = state.path;
300+
} else if (universe.defaultHash === true) {
301+
expectedState = state.hash.single;
302+
} else if (typeof universe.defaultHash === 'string') {
303+
expectedState = state.hash[universe.defaultHash];
304+
}
299305
}
300306
expect(router.state).toBe(expectedState);
301307
});

src/lib/kernel/calculateHref.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("calculateHref", () => {
5353

5454
const basePath = "/base/path";
5555
const baseHash = universe.hashMode === 'multi'
56-
? "#p1=path/one;p2=path/two"
56+
? `#${universe.hash || universe.defaultHash}=path/one;p2=path/two`
5757
: "#base/hash";
5858

5959
beforeEach(() => {
@@ -69,6 +69,11 @@ describe("calculateHref", () => {
6969
if (universe.hash === ALL_HASHES.path) return '/sample/path';
7070
if (universe.hash === ALL_HASHES.single) return '#/sample/path';
7171
if (universe.hash === ALL_HASHES.implicit) {
72+
// Handle implicit routing
73+
if (universe.hashMode === 'multi' && typeof universe.defaultHash === 'string') {
74+
// IMHR - implicit multi-hash routing
75+
return `#${universe.defaultHash}=/sample/path;p2=path/two`;
76+
}
7277
return universe.defaultHash === false ? '/sample/path' : '#/sample/path';
7378
}
7479
// Multi-hash routing - preserves existing paths and adds/updates the specified hash
@@ -87,6 +92,11 @@ describe("calculateHref", () => {
8792
if (universe.hash === ALL_HASHES.path) return `/sample/path${baseHash}`;
8893
if (universe.hash === ALL_HASHES.single) return '#/sample/path';
8994
if (universe.hash === ALL_HASHES.implicit) {
95+
// Handle implicit routing
96+
if (universe.hashMode === 'multi' && typeof universe.defaultHash === 'string') {
97+
// IMHR - implicit multi-hash routing
98+
return `#${universe.defaultHash}=/sample/path;p2=path/two`;
99+
}
90100
return universe.defaultHash === false ? `/sample/path${baseHash}` : '#/sample/path';
91101
}
92102
// Multi-hash routing - preserveHash doesn't apply to hash routing
@@ -121,6 +131,11 @@ describe("calculateHref", () => {
121131
if (universe.hash === ALL_HASHES.path) return newPath;
122132
if (universe.hash === ALL_HASHES.single) return `#${newPath}`;
123133
if (universe.hash === ALL_HASHES.implicit) {
134+
// Handle implicit routing
135+
if (universe.hashMode === 'multi' && typeof universe.defaultHash === 'string') {
136+
// IMHR - implicit multi-hash routing
137+
return `#${universe.defaultHash}=${newPath};p2=path/two`;
138+
}
124139
return universe.defaultHash === false ? newPath : `#${newPath}`;
125140
}
126141
// Multi-hash routing
@@ -165,8 +180,8 @@ describe("calculateHref", () => {
165180
test("Should preserve all existing paths when updating an existing path", () => {
166181
// Arrange
167182
const newPath = "/sample/path";
168-
const existingHashId = 'p1';
169-
const expected = baseHash.replace(/(p1=).+;/i, `$1${newPath};`);
183+
const existingHashId = universe.hash || universe.defaultHash; // Use the universe's hash ID
184+
const expected = baseHash.replace(new RegExp(`(${existingHashId}=)[^;]+`), `$1${newPath}`);
170185

171186
// Act
172187
const href = calculateHref({ hash: existingHashId }, newPath);
@@ -182,7 +197,13 @@ describe("calculateHref", () => {
182197
test("Should resolve implicit hash according to defaultHash", () => {
183198
// Arrange
184199
const newPath = "/sample/path";
185-
const expectedHref = universe.defaultHash === false ? newPath : `#${newPath}`;
200+
const expectedHref = (() => {
201+
if (universe.hashMode === 'multi' && typeof universe.defaultHash === 'string') {
202+
// IMHR - implicit multi-hash routing
203+
return `#${universe.defaultHash}=${newPath};p2=path/two`;
204+
}
205+
return universe.defaultHash === false ? newPath : `#${newPath}`;
206+
})();
186207

187208
// Act
188209
const href = calculateHref({ hash: universe.hash }, newPath);

src/lib/kernel/calculateState.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ describe('calculateState', () => {
120120
const newState = calculateState(ALL_HASHES.path, { path: 'new' });
121121

122122
// Assert - All existing hash states should be preserved
123-
if (universe.text === 'IMP') {
124-
// IMP universe may not have existing state from setup
123+
if (universe.text === 'IPR') {
124+
// IPR universe may not have existing state from setup
125125
expect(newState).toEqual({
126126
path: { path: 'new' },
127127
hash: {}
@@ -145,8 +145,8 @@ describe('calculateState', () => {
145145
const newState = calculateState(ALL_HASHES.single, { single: 'new' });
146146

147147
// Assert - Path and other hash states should be preserved
148-
if (universe.text === 'IMP') {
149-
// IMP universe may not have existing state from setup
148+
if (universe.text === 'IPR') {
149+
// IPR universe may not have existing state from setup
150150
expect(newState).toEqual({
151151
path: undefined,
152152
hash: { single: { single: 'new' } }
@@ -235,13 +235,19 @@ describe('calculateState', () => {
235235
// Act - use single-parameter overload for implicit mode
236236
const newState = calculateState(testState);
237237

238-
// Assert - calculateState preserves existing state
238+
// Assert - calculateState preserves existing state and updates correct universe
239239
if (universe.defaultHash === false) {
240+
// Implicit resolves to path routing
240241
expect(newState.path).toEqual(testState);
241242
expect(newState.hash).toBeDefined(); // Hash state preserved
242-
} else {
243+
} else if (universe.defaultHash === true) {
244+
// Implicit resolves to single hash routing
243245
expect(newState.hash.single).toEqual(testState);
244246
expect(newState.path).toBeDefined(); // Path state preserved
247+
} else if (typeof universe.defaultHash === 'string') {
248+
// Implicit resolves to multi-hash routing with specific hash ID
249+
expect(newState.hash[universe.defaultHash]).toEqual(testState);
250+
expect(newState.path).toBeDefined(); // Path state preserved
245251
}
246252
});
247253
});

src/lib/kernel/resolveHashValue.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("resolveHashValue", () => {
2121
expect(result).toBe(newDefaultHash);
2222
});
2323
test.each<Hash>([
24-
'p1',
24+
'tp',
2525
false,
2626
true
2727
])("Should return the provided hash %s value when defined.", (hash) => {

0 commit comments

Comments
 (0)