|
1 | | -import { describe, test, expect, beforeAll, afterAll, vi } from "vitest"; |
| 1 | +import { describe, test, expect, beforeAll, afterAll, vi, beforeEach } from "vitest"; |
2 | 2 | import { routePatternsKey, RouterEngine } from "./RouterEngine.svelte.js"; |
3 | 3 | import { init, type Hash, type RouteInfo } from "$lib/index.js"; |
4 | 4 | import { registerRouter } from "./trace.svelte.js"; |
@@ -39,7 +39,7 @@ describe("RouterEngine", () => { |
39 | 39 | }); |
40 | 40 | }); |
41 | 41 |
|
42 | | -describe("RouterEngine", () => { |
| 42 | +describe("RouterEngine (default init)", () => { |
43 | 43 | let _href: string; |
44 | 44 | let cleanup: () => void; |
45 | 45 | let interceptedState: any = null; |
@@ -73,6 +73,9 @@ describe("RouterEngine", () => { |
73 | 73 | replaceState: replaceStateMock |
74 | 74 | }; |
75 | 75 | }); |
| 76 | + beforeEach(() => { |
| 77 | + location.url.href = globalThis.window.location.href = "http://example.com"; |
| 78 | + }); |
76 | 79 | afterAll(() => { |
77 | 80 | cleanup(); |
78 | 81 | }); |
@@ -497,9 +500,72 @@ describe("RouterEngine", () => { |
497 | 500 | }); |
498 | 501 | }); |
499 | 502 | }); |
| 503 | + describe('noMatches', () => { |
| 504 | + test("Should be true whenever there are no routes registered.", () => { |
| 505 | + // Act. |
| 506 | + const router = new RouterEngine(); |
| 507 | + |
| 508 | + // Assert. |
| 509 | + expect(router.noMatches).toBe(true); |
| 510 | + }); |
| 511 | + test("Should be true whenever there are no matching routes.", () => { |
| 512 | + // Act. |
| 513 | + const router = new RouterEngine(); |
| 514 | + router.routes['route'] = { |
| 515 | + pattern: '/:one/:two?', |
| 516 | + caseSensitive: false, |
| 517 | + }; |
| 518 | + |
| 519 | + // Assert. |
| 520 | + expect(router.noMatches).toBe(true); |
| 521 | + }); |
| 522 | + test.each([ |
| 523 | + { |
| 524 | + text: "is", |
| 525 | + routeCount: 1, |
| 526 | + totalRoutes: 5 |
| 527 | + }, |
| 528 | + { |
| 529 | + text: "are", |
| 530 | + routeCount: 2, |
| 531 | + totalRoutes: 5 |
| 532 | + }, |
| 533 | + { |
| 534 | + text: "are", |
| 535 | + routeCount: 5, |
| 536 | + totalRoutes: 5 |
| 537 | + }, |
| 538 | + ])("Should be false whenever there $text $routeCount matching route(s) out of $totalRoutes route(s).", ({ routeCount, totalRoutes }) => { |
| 539 | + // Act. |
| 540 | + const router = new RouterEngine(); |
| 541 | + for (let i = 0; i < routeCount; i++) { |
| 542 | + router.routes[`route${i}`] = { |
| 543 | + and: () => i < routeCount |
| 544 | + }; |
| 545 | + } |
| 546 | + |
| 547 | + // Assert. |
| 548 | + expect(router.noMatches).toBe(false); |
| 549 | + }); |
| 550 | + test.each([ |
| 551 | + 1, 2, 5 |
| 552 | + ])("Should be true whenever the %d matching route(s) are ignored for fallback.", (routeCount) => { |
| 553 | + // Act. |
| 554 | + const router = new RouterEngine(); |
| 555 | + for (let i = 0; i < routeCount; i++) { |
| 556 | + router.routes[`route${i}`] = { |
| 557 | + and: () => true, |
| 558 | + ignoreForFallback: true |
| 559 | + }; |
| 560 | + } |
| 561 | + |
| 562 | + // Assert. |
| 563 | + expect(router.noMatches).toBe(true); |
| 564 | + }); |
| 565 | + }); |
500 | 566 | }); |
501 | 567 |
|
502 | | -describe("RouterEngine", () => { |
| 568 | +describe("RouterEngine (multi hash)", () => { |
503 | 569 | let _href: string; |
504 | 570 | let cleanup: () => void; |
505 | 571 | let interceptedState: any = null; |
|
0 commit comments