Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 129 additions & 1 deletion src/lib/core/LocationLite.svelte.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
import { LocationLite } from "./LocationLite.svelte.js";
import type { Hash, HistoryApi } from "../types.js";
import type { ExtendedRoutingOptions, Hash, HistoryApi, PreserveQuery } from "../types.js";
import { setupBrowserMocks, ALL_HASHES } from "../../testing/test-utils.js";
import { SvelteURL } from "svelte/reactivity";
import { setLocation } from "./Location.js";
import { resetRoutingOptions, setRoutingOptions } from "./options.js";

describe("LocationLite", () => {
const initialUrl = "http://example.com/";
Expand All @@ -12,10 +14,12 @@ describe("LocationLite", () => {
beforeEach(() => {
browserMocks = setupBrowserMocks(initialUrl);
location = new LocationLite();
setLocation(location);
});

afterEach(() => {
location.dispose();
setLocation(null);
browserMocks.cleanup();
});

Expand Down Expand Up @@ -125,4 +129,128 @@ describe("LocationLite", () => {
expect(location.getState('abc')).toBe(abcHashState);
});
});
describe('goTo', () => {
test.each<{
qs: string;
preserveQuery: PreserveQuery;
text: string;
expectedQs: string;
}>([
{
qs: 'some=value',
preserveQuery: false,
text: 'not preserve',
expectedQs: '',
},
{
qs: 'some=value',
preserveQuery: true,
text: 'preserve',
expectedQs: '?some=value',
},
{
qs: 'some=value&plus=another',
preserveQuery: 'plus',
text: 'preserve',
expectedQs: '?plus=another',
},
{
qs: 'some=value&plus=another&extra=thing',
preserveQuery: ['plus', 'extra'],
text: 'preserve',
expectedQs: '?plus=another&extra=thing',
},
])
("Should $text the query string when instructed by the value $preserveQuery in the preserveQuery option.", ({ qs, preserveQuery, expectedQs }) => {
// Arrange.
location.url.search = window.location.search = qs;
const newPath = '/new/path';

// Act.
location.goTo(newPath, { preserveQuery });

// Assert.
expect(window.location.pathname).to.equal(newPath);
expect(window.location.search).to.equal(`${expectedQs}`);
});
});
describe('navigate', () => {
afterEach(() => {
resetRoutingOptions();
});

test.each<{
qs: string;
preserveQuery: PreserveQuery;
text: string;
expectedQs: string;
}>([
{
qs: 'some=value',
preserveQuery: false,
text: 'not preserve',
expectedQs: '',
},
{
qs: 'some=value',
preserveQuery: true,
text: 'preserve',
expectedQs: '?some=value',
},
{
qs: 'some=value&plus=another',
preserveQuery: 'plus',
text: 'preserve',
expectedQs: '?plus=another',
},
{
qs: 'some=value&plus=another&extra=thing',
preserveQuery: ['plus', 'extra'],
text: 'preserve',
expectedQs: '?plus=another&extra=thing',
},
])("Should $text the query string when instructed by the value $preserveQuery in the preserveQuery option.", ({ qs, preserveQuery, expectedQs }) => {
// Arrange.
location.url.search = window.location.search = qs;
const newPath = '/new/path';

// Act.
location.navigate(newPath, { preserveQuery });

// Assert.
expect(window.location.pathname).to.equal(newPath);
expect(window.location.search).to.equal(`${expectedQs}`);
});
test.each<{
hash: Hash;
desc: string;
options: ExtendedRoutingOptions;
}>([
{
hash: ALL_HASHES.path,
desc: 'path',
options: { disallowPathRouting: true}
},
{
hash: ALL_HASHES.single,
desc: 'hash',
options: { disallowHashRouting: true}
},
{
hash: ALL_HASHES.multi,
desc: 'multi hash',
options: { disallowMultiHashRouting: true}
},
])("Should throw an error when the hash option is $hash and $desc routing is disallowed.", ({ hash, options }) => {
// Arrange.
const newPath = '/new/path';
setRoutingOptions(options);

// Act.
const act = () => location.navigate(newPath, { hash });

// Assert.
expect(act).toThrow();
});
});
});
2 changes: 2 additions & 0 deletions src/lib/core/LocationLite.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { resolveHashValue } from "./resolveHashValue.js";
import { calculateHref } from "./calculateHref.js";
import { calculateState } from "./calculateState.js";
import { preserveQueryInUrl } from "./preserveQuery.js";
import { assertAllowedRoutingMode } from "$lib/utils.js";

/**
* A lite version of the location object. It does not support event listeners or state-setting call interceptions,
Expand Down Expand Up @@ -83,6 +84,7 @@ export class LocationLite implements Location {

navigate(url: string, options?: NavigateOptions): void {
const resolvedHash = resolveHashValue(options?.hash);
assertAllowedRoutingMode(resolvedHash);
if (url !== '') {
url = calculateHref({
...options,
Expand Down
1 change: 1 addition & 0 deletions src/testing/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export function createLocationMock(initialUrl = "http://example.com/") {
// Add other location properties as needed
get pathname() { return _url.pathname; },
get search() { return _url.search; },
set search(value: string) { _url.search = value; },
get hash() { return _url.hash; },
get origin() { return _url.origin; },
get protocol() { return _url.protocol; },
Expand Down