-
Notifications
You must be signed in to change notification settings - Fork 595
/
plugin_test.ts
285 lines (253 loc) · 7.56 KB
/
plugin_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { type FreshConfig, ServerContext, STATUS_CODE } from "../server.ts";
import {
assert,
assertEquals,
assertMatch,
assertStringIncludes,
delay,
join,
puppeteer,
} from "./deps.ts";
import manifest from "./fixture_plugin/fresh.gen.ts";
import config from "./fixture_plugin/fresh.config.ts";
import {
clickWhenListenerReady,
runBuild,
startFreshServer,
withFakeServe,
withPageName,
} from "./test_utils.ts";
import routePlugin from "./fixture_plugin/utils/route-plugin.ts";
import secondMiddlewarePlugin from "./fixture_plugin/utils/second-middleware-plugin.ts";
const ctx = await ServerContext.fromManifest(manifest, config);
const handler = ctx.handler();
const router = (req: Request) => {
return handler(req, {
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
Deno.test("/static page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/static"));
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(body, '<style id="abc">body { color: red; }</style>');
assert(!body.includes(`>{"v":[[],[]]}</script>`));
assert(!body.includes(`import`));
assertStringIncludes(
body,
'<style id="def">h1 { text-decoration: underline; }</style>',
);
assertStringIncludes(body, '<link rel="stylesheet" href="styles.css"/>');
assertStringIncludes(
body,
'<link rel="stylesheet" href="print.css" media="print"/>',
);
});
Deno.test("/with-island prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/with-island"));
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
'<style id="abc">body { color: red; } h1 { color: blue; }</style>',
);
assertStringIncludes(body, `>{"v":[[{}],["JS injected!"]]}</script>`);
assertStringIncludes(body, `/plugin-js-inject-main.js"`);
assertStringIncludes(
body,
'<style id="def">h1 { text-decoration: underline; } h1 { font-style: italic; }</style>',
);
});
Deno.test("plugin routes and middleware", async () => {
const resp = await router(new Request("https://fresh.deno.dev/test"));
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<h1>look, i'm set from a plugin!</h1>`,
);
assertStringIncludes(
body,
`<title>Title Set From Plugin Config</title>`,
);
});
Deno.test("plugin routes and middleware -- async _app", async () => {
const ctx = await ServerContext.fromManifest(manifest, {
plugins: [
routePlugin({ title: "Title Set From Plugin Config", async: true }),
secondMiddlewarePlugin(),
],
} as FreshConfig);
const handler = ctx.handler();
const router = (req: Request) => {
return handler(req, {
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
const resp = await router(new Request("https://fresh.deno.dev/test"));
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<h1>look, i'm set from a plugin!</h1>`,
);
assertStringIncludes(
body,
`<title>Title Set From Plugin Config</title>`,
);
});
Deno.test("plugin middleware multiple handlers", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/lots-of-middleware"),
);
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<h1>3</h1>`,
);
});
Deno.test("plugin route no leading slash", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/no-leading-slash-here"),
);
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<div>Hello</div>`,
);
});
Deno.test("plugin async route", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/async-route"),
);
assert(resp);
assertEquals(resp.status, STATUS_CODE.OK);
const body = await resp.text();
assertStringIncludes(
body,
`<div>this is an async route!</div>`,
);
});
Deno.test({
name: "plugin supports islands",
async fn(t) {
await withPageName(
"./tests/fixture_plugin/main.ts",
async (page, address) => {
async function idTest(id: string) {
const elem = await page.waitForSelector(`#${id}`);
const value = await elem?.evaluate((el) => el.textContent);
assert(value === `${id}`, `value ${value} not equal to id ${id}`);
}
await page.goto(`${address}/pluginroutewithisland`, {
waitUntil: "networkidle2",
});
await t.step("verify tags", async () => {
await idTest("csr");
await idTest("csr_alt_folder");
});
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "/with-island hydration",
async fn(t) {
// Preparation
const { lines, serverProcess, address } = await startFreshServer({
args: ["run", "-A", "./tests/fixture_plugin/main.ts"],
});
await delay(100);
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto(`${address}/with-island`, {
waitUntil: "networkidle2",
});
await t.step("island is revived", async () => {
await page.waitForSelector("#csr");
});
await t.step("title was updated", async () => {
const title = await page.title();
assertEquals(title, "JS injected!");
});
await browser.close();
serverProcess.kill("SIGTERM");
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
},
});
Deno.test("calls buildStart() and buildEnd()", async () => {
const result = await runBuild("./tests/fixture_plugin_lifecycle/dev.ts");
const out = result.stdout.split("\n").filter((line) =>
line.startsWith("Plugin")
);
assertEquals(out, [
"Plugin a: configResolved",
"Plugin b: configResolved",
"Plugin c: configResolved",
"Plugin a: buildStart",
"Plugin b: buildStart",
`Plugin c: ${join("tests", "fixture_plugin_lifecycle", "_fresh")}`,
"Plugin a: buildEnd",
"Plugin b: buildEnd",
]);
});
Deno.test("calls configResolved() in dev", async () => {
await withFakeServe(
"./tests/fixture_plugin_resolved_dev/dev.ts",
async (server) => {
const res = await server.get("/");
await res.text();
assertEquals(res.headers.get("X-Plugin-A"), "true");
},
{ loadConfig: true },
);
});
Deno.test("plugin script doesn't halt island execution", async () => {
await withPageName(
"./tests/fixture_plugin_error/main.ts",
async (page, address) => {
let error;
page.on("pageerror", (err) => {
error = err;
});
await page.goto(address);
await page.waitForSelector("#ready");
let text = await page.$eval("p", (el) => el.textContent!);
assertEquals(text, "0");
await clickWhenListenerReady(page, "button");
text = await page.$eval("p", (el) => el.textContent!);
assertEquals(text, "1");
assertMatch(String(error), /Error thrown/);
},
);
});
Deno.test("supports returning htmlText", async () => {
await withFakeServe(
"./tests/fixture_plugin_html/main.ts",
async (server) => {
const doc = await server.getHtml("/");
assertEquals(doc.body.textContent, "it works");
},
{ loadConfig: true },
);
});