-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make path merging more forgiving and add tests
- Loading branch information
Showing
5 changed files
with
56 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |