forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defineRoutes-test.ts
166 lines (154 loc) · 4.97 KB
/
defineRoutes-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { defineRoutes } from "../config/routes";
describe("defineRoutes", () => {
it("returns an array of routes", () => {
let routes = defineRoutes((route) => {
route("/", "routes/home.js");
route("inbox", "routes/inbox.js", () => {
route("/", "routes/inbox/index.js", { index: true });
route(":messageId", "routes/inbox/$messageId.js");
route("archive", "routes/inbox/archive.js");
});
});
expect(routes).toMatchInlineSnapshot(`
Object {
"routes/home": Object {
"caseSensitive": undefined,
"file": "routes/home.js",
"id": "routes/home",
"index": undefined,
"parentId": undefined,
"path": "/",
},
"routes/inbox": Object {
"caseSensitive": undefined,
"file": "routes/inbox.js",
"id": "routes/inbox",
"index": undefined,
"parentId": undefined,
"path": "inbox",
},
"routes/inbox/$messageId": Object {
"caseSensitive": undefined,
"file": "routes/inbox/$messageId.js",
"id": "routes/inbox/$messageId",
"index": undefined,
"parentId": "routes/inbox",
"path": ":messageId",
},
"routes/inbox/archive": Object {
"caseSensitive": undefined,
"file": "routes/inbox/archive.js",
"id": "routes/inbox/archive",
"index": undefined,
"parentId": "routes/inbox",
"path": "archive",
},
"routes/inbox/index": Object {
"caseSensitive": undefined,
"file": "routes/inbox/index.js",
"id": "routes/inbox/index",
"index": true,
"parentId": "routes/inbox",
"path": "/",
},
}
`);
});
it("works with async data", async () => {
// Read everything *before* calling defineRoutes.
let fakeDirectory = await Promise.resolve(["one.md", "two.md"]);
let routes = defineRoutes((route) => {
for (let file of fakeDirectory) {
route(file.replace(/\.md$/, ""), file);
}
});
expect(routes).toMatchInlineSnapshot(`
Object {
"one": Object {
"caseSensitive": undefined,
"file": "one.md",
"id": "one",
"index": undefined,
"parentId": undefined,
"path": "one",
},
"two": Object {
"caseSensitive": undefined,
"file": "two.md",
"id": "two",
"index": undefined,
"parentId": undefined,
"path": "two",
},
}
`);
});
it("allows multiple routes with the same route module", () => {
let routes = defineRoutes((route) => {
route("/user/:id", "routes/index.tsx", { id: "user-by-id" });
route("/user", "routes/index.tsx", { id: "user" });
route("/other", "routes/other-route.tsx");
});
expect(routes).toMatchInlineSnapshot(`
Object {
"routes/other-route": Object {
"caseSensitive": undefined,
"file": "routes/other-route.tsx",
"id": "routes/other-route",
"index": undefined,
"parentId": undefined,
"path": "/other",
},
"user": Object {
"caseSensitive": undefined,
"file": "routes/index.tsx",
"id": "user",
"index": undefined,
"parentId": undefined,
"path": "/user",
},
"user-by-id": Object {
"caseSensitive": undefined,
"file": "routes/index.tsx",
"id": "user-by-id",
"index": undefined,
"parentId": undefined,
"path": "/user/:id",
},
}
`);
});
it("throws an error on route id collisions", () => {
// Two conflicting custom id's
let defineNonUniqueRoutes = () => {
defineRoutes((route) => {
route("/user/:id", "routes/user.tsx", { id: "user" });
route("/user", "routes/user.tsx", { id: "user" });
route("/other", "routes/other-route.tsx");
});
};
expect(defineNonUniqueRoutes).toThrowErrorMatchingInlineSnapshot(
`"Unable to define routes with duplicate route id: \\"user\\""`
);
// Custom id conflicting with a later-defined auto-generated id
defineNonUniqueRoutes = () => {
defineRoutes((route) => {
route("/user/:id", "routes/user.tsx", { id: "routes/user" });
route("/user", "routes/user.tsx");
});
};
expect(defineNonUniqueRoutes).toThrowErrorMatchingInlineSnapshot(
`"Unable to define routes with duplicate route id: \\"routes/user\\""`
);
// Custom id conflicting with an earlier-defined auto-generated id
defineNonUniqueRoutes = () => {
defineRoutes((route) => {
route("/user", "routes/user.tsx");
route("/user/:id", "routes/user.tsx", { id: "routes/user" });
});
};
expect(defineNonUniqueRoutes).toThrowErrorMatchingInlineSnapshot(
`"Unable to define routes with duplicate route id: \\"routes/user\\""`
);
});
});