-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
841006f
commit e355f25
Showing
5 changed files
with
77 additions
and
91 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
refactor: clean up pages routing |
49 changes: 34 additions & 15 deletions
49
packages/wrangler/pages/functions/filepath-routing.test.ts
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,47 +1,66 @@ | ||
import { toUrlPath } from "../../src/paths"; | ||
import { compareRoutes } from "./filepath-routing"; | ||
import type { HTTPMethod, RouteConfig } from "./routes"; | ||
|
||
describe("compareRoutes()", () => { | ||
const url = toUrlPath; | ||
test("routes / last", () => { | ||
expect(compareRoutes([url("/")], [url("/foo")])).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes([url("/")], [url("/:foo")])).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes([url("/")], [url("/:foo*")])).toBeGreaterThanOrEqual( | ||
1 | ||
); | ||
expect( | ||
compareRoutes(routeConfig("/"), routeConfig("/foo")) | ||
).toBeGreaterThanOrEqual(1); | ||
expect( | ||
compareRoutes(routeConfig("/"), routeConfig("/:foo")) | ||
).toBeGreaterThanOrEqual(1); | ||
expect( | ||
compareRoutes(routeConfig("/"), routeConfig("/:foo*")) | ||
).toBeGreaterThanOrEqual(1); | ||
}); | ||
|
||
test("routes with fewer segments come after those with more segments", () => { | ||
expect( | ||
compareRoutes([url("/foo")], [url("/foo/bar")]) | ||
compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar")) | ||
).toBeGreaterThanOrEqual(1); | ||
expect( | ||
compareRoutes([url("/foo")], [url("/foo/bar/cat")]) | ||
compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat")) | ||
).toBeGreaterThanOrEqual(1); | ||
}); | ||
|
||
test("routes with wildcard segments come after those without", () => { | ||
expect(compareRoutes([url("/:foo*")], [url("/foo")])).toBe(1); | ||
expect(compareRoutes([url("/:foo*")], [url("/:foo")])).toBe(1); | ||
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1); | ||
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(1); | ||
}); | ||
|
||
test("routes with dynamic segments come after those without", () => { | ||
expect(compareRoutes([url("/:foo")], [url("/foo")])).toBe(1); | ||
expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1); | ||
}); | ||
|
||
test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => { | ||
expect(compareRoutes([url("/foo/:id/bar")], [url("/foo/bar/:id")])).toBe(1); | ||
expect( | ||
compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id")) | ||
).toBe(1); | ||
}); | ||
|
||
test("routes with no HTTP method come after those specifying a method", () => { | ||
expect(compareRoutes([url("/foo")], [url("/foo"), "GET"])).toBe(1); | ||
expect(compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))).toBe( | ||
1 | ||
); | ||
}); | ||
|
||
test("two equal routes are sorted according to their original position in the list", () => { | ||
expect(compareRoutes([url("/foo"), "GET"], [url("/foo"), "GET"])).toBe(0); | ||
expect( | ||
compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET")) | ||
).toBe(0); | ||
}); | ||
|
||
test("it returns -1 if the first argument should appear first in the list", () => { | ||
expect(compareRoutes([url("/foo"), "GET"], [url("/foo")])).toBe(-1); | ||
expect(compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))).toBe( | ||
-1 | ||
); | ||
}); | ||
}); | ||
|
||
function routeConfig(routePath: string, method?: string): RouteConfig { | ||
return { | ||
routePath: toUrlPath(routePath), | ||
method: method as HTTPMethod, | ||
}; | ||
} |
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
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