This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.spec.js
214 lines (153 loc) · 4.32 KB
/
tests.spec.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* eslint-env jest
* @jest-environment node
*/
const micro = require("micro");
const testListen = require("test-listen");
const { get, post } = require("axios");
const {
Router,
useUrlParams,
useQueryString,
createHook,
useBasePath
} = require("./");
let server = null;
beforeAll(() => {
jest.spyOn(console, "error").mockImplementation(() => {});
});
afterAll(() => {
console.error.mockRestore();
});
afterEach(() => {
console.error.mockClear();
if (server) server.close();
});
function listen(srv) {
server = srv;
return testListen(srv);
}
it("should handle requests", async () => {
const app = Router();
app.get("/:name", req => {
const { name } = useUrlParams(req);
return `Hello ${name}`;
});
const url = await listen(micro(app));
const { data: body } = await get(`${url}/bobby`);
expect(body).toEqual("Hello bobby");
});
it("should handle multiple routes", async () => {
const app = Router();
app.get("/:name", req => {
const { name } = useUrlParams(req);
return `Hello ${name}`;
});
app.post("/:name", req => {
const { name } = useUrlParams(req);
return `Posted to ${name}`;
});
app.post("/", req => {
return "post";
});
const url = await listen(micro(app));
const { data: body } = await post(url);
expect(body).toEqual("post");
});
it("should hit the first qualified route", async () => {
const app = Router();
app.get("/", req => {
return `first`;
});
app.get("/abc", req => {
return `last`;
});
const url = await listen(micro(app));
const { data: body } = await get(`${url}/abc`);
expect(body).toEqual("last");
});
it("should allow nesting routers", async () => {
const subApp = Router();
subApp.get("/:age", req => {
const { age } = useUrlParams(req);
return `age is ${age}`;
});
const app = Router();
app.use("/sub", subApp);
const url = await listen(micro(app));
const { data: body } = await get(`${url}/sub/123`);
expect(body).toEqual("age is 123");
});
it("should allow nesting deep routers", async () => {
const subApp2 = Router();
subApp2.get("/3rd", () => {
return "3rd";
});
const subApp1 = Router();
const app = Router();
subApp1.use("/2nd", subApp2);
app.use("/1st", subApp1);
const url = await listen(micro(app));
const { data: body } = await get(`${url}/1st/2nd/3rd`);
expect(body).toEqual("3rd");
});
it("should 404 if no route is found", async () => {
const app = Router();
const url = await listen(micro(app));
const { status } = await get(`${url}/bobby`).catch(r => r.response);
expect(status).toEqual(404);
});
it("should allow getting the search query", async () => {
const app = Router();
app.get("/", req => {
const { name } = useQueryString(req);
return `Hello ${name}`;
});
const url = await listen(micro(app));
const { data: body } = await get(`${url}?name=bobby`);
expect(body).toEqual("Hello bobby");
});
it("should allow getting the base path", async () => {
const subApp = Router();
subApp.get("/", req => {
return useBasePath(req);
});
const app = Router();
app.use("/sub", subApp);
const url = await listen(micro(app));
const { data: body } = await get(`${url}/sub`);
expect(body).toEqual("/sub");
});
describe("custom hooks", () => {
const useUrl = createHook(req => req.url);
const useUrlAsync = createHook(async req => await req.url);
it("should allow sync hooks", async () => {
const app = Router();
app.get("/", req => {
return useUrl(req);
});
const url = await listen(micro(app));
const { data: body } = await get(url);
expect(typeof body === "string").toEqual(true);
});
it("should allow async hooks", async () => {
const app = Router();
app.get("/", async req => {
return await useUrlAsync(req);
});
const url = await listen(micro(app));
const { data: body } = await get(url);
expect(typeof body === "string").toEqual(true);
});
it("should cache requests to get the hooks", async () => {
const app = Router();
app.get("/", req => {
const query1 = useQueryString(req);
const query2 = useQueryString(req);
return query1 === query2 ? "equal" : "not-equal";
});
const url = await listen(micro(app));
const { data: body } = await get(`${url}?name="bob`);
expect(body).toEqual("equal");
});
});