~james/core

core/web/src/lib/navPulse.test.ts -rw-r--r-- 1.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { describe, expect, it, vi } from "vitest";
import { navigatingTo } from "#lib/navPulse.ts";

const mockNavigating = vi.hoisted(() => ({
	to: null as { url: URL } | null
}));

vi.mock("$app/state", () => ({
	navigating: mockNavigating
}));

describe("navigatingTo", () => {
	it("returns false when there is no active navigation", () => {
		mockNavigating.to = null;
		expect(navigatingTo("/alice/repo")).toBe(false);
	});

	it("returns true when navigating to the matching href path", () => {
		mockNavigating.to = { url: new URL("https://tangled.org/alice/repo") };
		expect(navigatingTo("/alice/repo")).toBe(true);
		expect(navigatingTo("https://tangled.org/alice/repo")).toBe(true);
	});

	it("returns false when navigating to a different path", () => {
		mockNavigating.to = { url: new URL("https://tangled.org/alice/repo/tree/main") };
		expect(navigatingTo("/alice/repo")).toBe(false);
		expect(navigatingTo("/alice/other-repo")).toBe(false);
	});

	it("matches pathnames regardless of search params or hashes in href or to", () => {
		mockNavigating.to = { url: new URL("https://tangled.org/alice/repo?tab=overview#section") };
		expect(navigatingTo("/alice/repo")).toBe(true);
		expect(navigatingTo("/alice/repo?tab=repos")).toBe(true);
	});
});