|
| 1 | +import { writeFileSync } from "fs"; |
| 2 | +import { runInTempDir } from "../../src/__tests__/helpers/run-in-tmp"; |
1 | 3 | import { toUrlPath } from "../../src/paths";
|
2 |
| -import { compareRoutes } from "./filepath-routing"; |
| 4 | +import { compareRoutes, generateConfigFromFileTree } from "./filepath-routing"; |
| 5 | +import type { UrlPath } from "../../src/paths"; |
3 | 6 | import type { HTTPMethod, RouteConfig } from "./routes";
|
4 | 7 |
|
5 |
| -describe("compareRoutes()", () => { |
6 |
| - test("routes / last", () => { |
7 |
| - expect( |
8 |
| - compareRoutes(routeConfig("/"), routeConfig("/foo")) |
9 |
| - ).toBeGreaterThanOrEqual(1); |
10 |
| - expect( |
11 |
| - compareRoutes(routeConfig("/"), routeConfig("/:foo")) |
12 |
| - ).toBeGreaterThanOrEqual(1); |
13 |
| - expect( |
14 |
| - compareRoutes(routeConfig("/"), routeConfig("/:foo*")) |
15 |
| - ).toBeGreaterThanOrEqual(1); |
16 |
| - }); |
| 8 | +describe("filepath-routing", () => { |
| 9 | + describe("compareRoutes()", () => { |
| 10 | + test("routes / last", () => { |
| 11 | + expect( |
| 12 | + compareRoutes(routeConfig("/"), routeConfig("/foo")) |
| 13 | + ).toBeGreaterThanOrEqual(1); |
| 14 | + expect( |
| 15 | + compareRoutes(routeConfig("/"), routeConfig("/:foo")) |
| 16 | + ).toBeGreaterThanOrEqual(1); |
| 17 | + expect( |
| 18 | + compareRoutes(routeConfig("/"), routeConfig("/:foo*")) |
| 19 | + ).toBeGreaterThanOrEqual(1); |
| 20 | + }); |
17 | 21 |
|
18 |
| - test("routes with fewer segments come after those with more segments", () => { |
19 |
| - expect( |
20 |
| - compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar")) |
21 |
| - ).toBeGreaterThanOrEqual(1); |
22 |
| - expect( |
23 |
| - compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat")) |
24 |
| - ).toBeGreaterThanOrEqual(1); |
25 |
| - }); |
| 22 | + test("routes with fewer segments come after those with more segments", () => { |
| 23 | + expect( |
| 24 | + compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar")) |
| 25 | + ).toBeGreaterThanOrEqual(1); |
| 26 | + expect( |
| 27 | + compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat")) |
| 28 | + ).toBeGreaterThanOrEqual(1); |
| 29 | + }); |
26 | 30 |
|
27 |
| - test("routes with wildcard segments come after those without", () => { |
28 |
| - expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1); |
29 |
| - expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(1); |
30 |
| - }); |
| 31 | + test("routes with wildcard segments come after those without", () => { |
| 32 | + expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1); |
| 33 | + expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe( |
| 34 | + 1 |
| 35 | + ); |
| 36 | + }); |
31 | 37 |
|
32 |
| - test("routes with dynamic segments come after those without", () => { |
33 |
| - expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1); |
34 |
| - }); |
| 38 | + test("routes with dynamic segments come after those without", () => { |
| 39 | + expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1); |
| 40 | + }); |
35 | 41 |
|
36 |
| - test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => { |
37 |
| - expect( |
38 |
| - compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id")) |
39 |
| - ).toBe(1); |
40 |
| - }); |
| 42 | + test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => { |
| 43 | + expect( |
| 44 | + compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id")) |
| 45 | + ).toBe(1); |
| 46 | + }); |
41 | 47 |
|
42 |
| - test("routes with no HTTP method come after those specifying a method", () => { |
43 |
| - expect(compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))).toBe( |
44 |
| - 1 |
45 |
| - ); |
46 |
| - }); |
| 48 | + test("routes with no HTTP method come after those specifying a method", () => { |
| 49 | + expect( |
| 50 | + compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET")) |
| 51 | + ).toBe(1); |
| 52 | + }); |
| 53 | + |
| 54 | + test("two equal routes are sorted according to their original position in the list", () => { |
| 55 | + expect( |
| 56 | + compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET")) |
| 57 | + ).toBe(0); |
| 58 | + }); |
47 | 59 |
|
48 |
| - test("two equal routes are sorted according to their original position in the list", () => { |
49 |
| - expect( |
50 |
| - compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET")) |
51 |
| - ).toBe(0); |
| 60 | + test("it returns -1 if the first argument should appear first in the list", () => { |
| 61 | + expect( |
| 62 | + compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo")) |
| 63 | + ).toBe(-1); |
| 64 | + }); |
52 | 65 | });
|
53 | 66 |
|
54 |
| - test("it returns -1 if the first argument should appear first in the list", () => { |
55 |
| - expect(compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))).toBe( |
56 |
| - -1 |
57 |
| - ); |
| 67 | + describe("generateConfigFromFileTree", () => { |
| 68 | + runInTempDir(); |
| 69 | + |
| 70 | + it("should generate a route entry for each file in the tree", async () => { |
| 71 | + writeFileSync( |
| 72 | + "foo.ts", |
| 73 | + ` |
| 74 | + export function onRequestGet() {} |
| 75 | + export function onRequestPost() {} |
| 76 | + ` |
| 77 | + ); |
| 78 | + writeFileSync( |
| 79 | + "bar.ts", |
| 80 | + ` |
| 81 | + export function onRequestPut() {} |
| 82 | + export function onRequestDelete() {} |
| 83 | + ` |
| 84 | + ); |
| 85 | + |
| 86 | + const entries = await generateConfigFromFileTree({ |
| 87 | + baseDir: ".", |
| 88 | + baseURL: "/base" as UrlPath, |
| 89 | + }); |
| 90 | + expect(entries).toEqual({ |
| 91 | + routes: [ |
| 92 | + { |
| 93 | + method: "PUT", |
| 94 | + module: ["bar.ts:onRequestPut"], |
| 95 | + routePath: "/base/bar", |
| 96 | + }, |
| 97 | + { |
| 98 | + method: "DELETE", |
| 99 | + module: ["bar.ts:onRequestDelete"], |
| 100 | + routePath: "/base/bar", |
| 101 | + }, |
| 102 | + { |
| 103 | + method: "GET", |
| 104 | + module: ["foo.ts:onRequestGet"], |
| 105 | + routePath: "/base/foo", |
| 106 | + }, |
| 107 | + { |
| 108 | + method: "POST", |
| 109 | + module: ["foo.ts:onRequestPost"], |
| 110 | + routePath: "/base/foo", |
| 111 | + }, |
| 112 | + ], |
| 113 | + }); |
| 114 | + }); |
58 | 115 | });
|
59 | 116 | });
|
60 | 117 |
|
|
0 commit comments