Skip to content

Commit

Permalink
fix: make path merging more forgiving and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erictooth committed Oct 9, 2022
1 parent 6cf4ff8 commit 43a3e04
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
28 changes: 21 additions & 7 deletions src/__tests__/combinePaths.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { combinePaths } from "../combinePaths";

it("merges two simple paths correctly", () => {
expect(combinePaths("subpath", "/basepath/").pathname).toEqual(
"/basepath/subpath"
);
});
const testPaths: [paths: [base: string, sub: string], expected: string][] = [
// adds leading and trailing slash to basepath
[["basepath", "subpath"], "/basepath/subpath"],

// drops leading slash on subpath
[["/basepath/", "/subpath"], "/basepath/subpath"],

// preserves trailing slash on subpath
[["/basepath/", "subpath/"], "/basepath/subpath/"],
];

it("drops the basepath if subpath has a leading slash", () => {
expect(combinePaths("/subpath", "/basepath/").pathname).toEqual("/subpath");
it("matches testPaths results", () => {
testPaths.forEach((testConfig) => {
const [paths, expected] = testConfig;
expect(combinePaths(paths[1], paths[0]).pathname).toEqual(expected);
});
});

it("preserves search params from both subpath and basepath", () => {
expect(
combinePaths("subpath?subParam=1", "/basepath/?baseParam=2").search
).toEqual("?subParam=1&baseParam=2");
});

it("preserves the hash from only the subpath", () => {
expect(
combinePaths("subpath?subParam=1#testHash", "/basepath#ignoredHash").hash
).toEqual("#testHash");
});
24 changes: 24 additions & 0 deletions src/__tests__/defineRoute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineRoute } from "../defineRoute";

const testParams = {
paramA: "1234",
paramB: "4567",
};

const testConfig = (params: typeof testParams) =>
`/base/sub/${params.paramA}/${params.paramB}?query=8910#hash`;

it("templates route property correctly", () => {
// Query and hash should be dropped, and paramProxy should properly template the params
expect(defineRoute(testConfig).route).toEqual("/base/sub/:paramA/:paramB");
});

it("extends a base route properly", () => {
const route = defineRoute(testConfig).extend(
(params: { paramC: string }) => `/extended/${params.paramC}`
);

expect(route.link({ ...testParams, paramC: "1121" })).toEqual(
`/base/sub/${testParams.paramA}/${testParams.paramB}/extended/1121?query=8910`
);
});
5 changes: 0 additions & 5 deletions src/__tests__/paramProxy.test.ts

This file was deleted.

7 changes: 6 additions & 1 deletion src/combinePaths.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { addTrailingSlash, removeLeadingSlash } from "./pathUtils";

export const combinePaths = (path: string, basePath: string): URL => {
const baseUrl = new URL(basePath, window.location.origin);

const url = new URL(path, baseUrl);
baseUrl.pathname = addTrailingSlash(baseUrl.pathname);

const url = new URL(removeLeadingSlash(path), baseUrl);

baseUrl.searchParams.forEach((value, key) => {
url.searchParams.set(key, value);
});
Expand Down
5 changes: 5 additions & 0 deletions src/pathUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const addTrailingSlash = (s: string): string =>
s.charAt(s.length - 1) === "/" ? s : s + "/";

export const removeLeadingSlash = (s: string): string =>
s.charAt(0) === "/" ? s.slice(1) : s;

0 comments on commit 43a3e04

Please sign in to comment.