-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.ts
200 lines (155 loc) · 4.29 KB
/
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
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
import {
assertEquals,
assertNotEquals,
assertThrowsAsync,
} from "https://deno.land/std@0.113.0/testing/asserts.ts";
import * as mf from "./mod.ts";
Deno.test({
name: "install() replaces fetch, uninstall() resets it",
fn() {
const fetchPreInstall = globalThis.fetch;
mf.install();
const fetchPostInstall = globalThis.fetch;
assertEquals(fetchPostInstall, mf.mockedFetch);
assertNotEquals(fetchPreInstall, fetchPostInstall);
mf.uninstall();
const fetchPostUninstall = globalThis.fetch;
assertEquals(fetchPreInstall, fetchPostUninstall);
},
});
Deno.test({
name: "fetch fails when the fetched route isn't mocked",
async fn() {
mf.install();
await assertThrowsAsync(async () => {
await fetch("https://localhost:8181/");
});
mf.uninstall();
},
});
Deno.test({
name: "fetch returns the reponse object when route matches",
async fn() {
mf.install();
const routeResponse = new Response("Hello, world!", {
status: 203,
});
mf.mock("/hello", () => routeResponse);
const res = await fetch("https://0.0.0.0/hello");
assertEquals(res, routeResponse);
// Let's also make sure that other routes still fail
await assertThrowsAsync(async () => {
await fetch("https://0.0.0.0/sup");
});
mf.uninstall();
},
});
Deno.test({
name: "query parameters are ignored when routing",
async fn() {
mf.install();
mf.mock("/", (req) => {
const search = new URL(req.url).searchParams.get("query");
assertEquals(search, "test");
return new Response(search);
});
const res = await fetch("https://api.example.com/?query=test");
assertEquals(await res.text(), "test");
mf.uninstall();
},
});
Deno.test({
name: "handlers have access to the match object",
async fn() {
mf.install();
mf.mock("DELETE@/lights/:id", (_req, match) => {
assertEquals(match["id"], "2");
return new Response();
});
await fetch("https://api.home.tld/lights/2", {
method: "DELETE",
});
mf.uninstall();
},
});
Deno.test({
name: "reset() removes all handlers",
async fn() {
mf.install();
assertThrowsAsync(async () => {
await fetch("https://localhost/a");
});
assertThrowsAsync(async () => {
await fetch("https://localhost/b");
});
mf.mock("/a", () => new Response());
mf.mock("/b", () => new Response());
await fetch("https://localhost/a");
await fetch("https://localhost/b");
mf.reset();
assertThrowsAsync(async () => {
await fetch("https://localhost/a");
});
assertThrowsAsync(async () => {
await fetch("https://localhost/b");
});
mf.uninstall();
},
});
Deno.test({
name: "remove() removes one handler",
async fn() {
mf.install();
assertThrowsAsync(async () => {
await fetch("https://localhost/a");
});
assertThrowsAsync(async () => {
await fetch("https://localhost/b");
});
mf.mock("/a", () => new Response());
mf.mock("/b", () => new Response());
await fetch("https://localhost/a");
await fetch("https://localhost/b");
mf.remove("/b");
await fetch("https://localhost/a");
assertThrowsAsync(async () => {
await fetch("https://localhost/b");
});
mf.uninstall();
},
});
Deno.test({
name: "state isn't shared",
async fn() {
mf.install();
const fg = { fetch, mock: mf.mock, remove: mf.remove, reset: mf.reset };
const f1 = mf.sandbox();
const f2 = mf.sandbox();
fg.mock("/", () => new Response("global"));
f1.mock("/", () => new Response("1"));
f2.mock("/", () => new Response("2"));
const responses = await Promise.all([
fg.fetch("https://wow.cool/"),
f1.fetch("https://wow.cool/"),
f2.fetch("https://wow.cool/"),
]);
const [tg, t1, t2] = await Promise.all(responses.map((res) => res.text()));
assertEquals(tg, "global");
assertEquals(t1, "1");
assertEquals(t2, "2");
mf.uninstall();
},
});
Deno.test({
name: "uninstall resets handlers",
async fn() {
mf.install();
mf.mock("/", () => new Response());
// don't need the response, just need to know this doesn't throw
await fetch("https://nice.dev/");
mf.uninstall();
await assertThrowsAsync(async () => {
await fetch("https://nice.dev/");
});
},
});