forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislands_test.ts
491 lines (416 loc) · 14.7 KB
/
islands_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import { assert, assertEquals, assertStringIncludes, Page } from "./deps.ts";
import {
assertNoPageComments,
assertNotSelector,
clickWhenListenerReady,
getErrorOverlay,
parseHtml,
waitForText,
withFakeServe,
withPageName,
} from "./test_utils.ts";
Deno.test("island tests", async (t) => {
await withPage(async (page, address) => {
async function counterTest(counterId: string, originalValue: number) {
const pElem = await page.waitForSelector(`#${counterId} > p`);
const value = await pElem?.evaluate((el) => el.textContent);
assert(value === `${originalValue}`, `${counterId} first value`);
await clickWhenListenerReady(page, `#b-${counterId}`);
await waitForText(page, `#${counterId} > p`, String(originalValue + 1));
}
await page.goto(`${address}/islands`);
await t.step("Ensure 5 islands on 1 page are revived", async () => {
await counterTest("counter1", 3);
await counterTest("counter2", 10);
await counterTest("folder-counter", 3);
await counterTest("subfolder-counter", 3);
await counterTest("kebab-case-file-counter", 5);
});
await t.step("Ensure an island revive an img 'hash' path", async () => {
// Ensure src path has __frsh_c=
const pElem = await page.waitForSelector(`#img-in-island`);
const srcString = (await pElem?.getProperty("src"))?.toString()!;
assertStringIncludes(srcString, "image.png?__frsh_c=");
// Ensure src path is the same as server rendered
const resp = await fetch(new Request(`${address}/islands`));
const body = await resp.text();
const imgFilePath = body.match(/img id="img-in-island" src="(.*?)"/)
?.[1]!;
assertStringIncludes(srcString, imgFilePath);
});
});
});
Deno.test("multiple islands exported from one file", async (t) => {
await withPage(async (page, address) => {
async function counterTest(counterId: string, originalValue: number) {
const pElem = await page.waitForSelector(`#${counterId} > p`);
const value = await pElem?.evaluate((el) => el.textContent);
assert(value === `${originalValue}`, `${counterId} first value`);
await clickWhenListenerReady(page, `#b-${counterId}`);
await waitForText(page, `#${counterId} > p`, String(originalValue + 1));
}
await page.goto(`${address}/islands/multiple_island_exports`);
await t.step("Ensure 3 islands on 1 page are revived", async () => {
await counterTest("counter0", 4);
await counterTest("counter1", 3);
await counterTest("counter2", 10);
});
});
});
function withPage(fn: (page: Page, address: string) => Promise<void>) {
return withPageName("./tests/fixture/main.ts", fn);
}
Deno.test("island tests with </script>", async (t) => {
await withPage(async (page, address) => {
page.on("dialog", () => {
assert(false, "There is XSS");
});
await page.goto(`${address}/evil`, {
waitUntil: "networkidle2",
});
await t.step("prevent XSS on Island", async () => {
const bodyElem = await page.waitForSelector(`body`);
const value = await bodyElem?.evaluate((el) => el.getInnerHTML());
assertStringIncludes(
value,
`{"message":"\\u003c/script\\u003e\\u003cscript\\u003ealert('test')\\u003c/script\\u003e"}`,
`XSS is not escaped`,
);
});
});
});
Deno.test("island with json import", async () => {
await withPage(async (page, address) => {
await page.goto(`${address}/island_json`, {
waitUntil: "networkidle2",
});
const json = await page.$eval("pre", (el) => el.textContent);
assertEquals(JSON.parse(json), { foo: "it works" });
});
});
Deno.test("island with fragment as root", async () => {
await withPage(async (page, address) => {
await page.goto(`${address}/islands/root_fragment`);
const clickableSelector = "#root-fragment-click-me";
await page.waitForSelector(clickableSelector);
await waitForText(page, `#island-parent`, "HelloWorld");
await clickWhenListenerReady(page, clickableSelector);
await waitForText(page, `#island-parent`, "HelloWorldI'm rendered now");
});
});
Deno.test(
"island with fragment as root and conditional child first",
async () => {
await withPage(async (page, address) => {
await page.goto(
`${address}/islands/root_fragment_conditional_first`,
);
const clickableSelector = "#root-fragment-conditional-first-click-me";
await page.waitForSelector(clickableSelector);
await waitForText(page, "#island-parent", "HelloWorld");
await clickWhenListenerReady(page, clickableSelector);
await waitForText(
page,
"#island-parent",
"I'm rendered on topHelloWorld",
);
});
},
);
Deno.test("island that returns `null`", async () => {
await withPage(async (page, address) => {
await page.goto(`${address}/islands/returning_null`);
await page.waitForSelector(".added-by-use-effect");
});
});
Deno.test("island using `npm:` specifiers", async () => {
await withPageName("./tests/fixture_npm/main.ts", async (page, address) => {
await page.setJavaScriptEnabled(false);
await page.goto(address);
await page.waitForSelector("#server-true");
await page.setJavaScriptEnabled(true);
await page.reload();
await page.waitForSelector("#browser-true");
});
});
Deno.test("pass single JSX child to island", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_jsx_child`);
await page.waitForSelector(".island");
await waitForText(page, ".island", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("pass multiple JSX children to island", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_jsx_children`);
await page.waitForSelector(".island");
await waitForText(page, ".island", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("pass multiple text JSX children to island", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_jsx_text`);
await page.waitForSelector(".island");
await waitForText(page, ".island", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("render island in island", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_in_island`);
await page.waitForSelector(".island");
await waitForText(page, ".island .island p", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("render island inside island definition", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_in_island_definition`);
await page.waitForSelector(".island");
await waitForText(page, ".island .island p", "it works");
// Check that there is no duplicated content which could happen
// when islands aren't initialized correctly
await waitForText(page, "#page", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("render island inside island with conditional children", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/dropdown`);
await page.waitForSelector("button");
await assertNoPageComments(page);
const doc = parseHtml(await page.content());
assertNotSelector(doc, ".result");
await page.click("button");
await page.waitForSelector(".result");
await assertNoPageComments(page);
},
);
});
Deno.test(
"render island with JSX children that render another island with JSX children",
async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_jsx_island_jsx`);
await page.waitForSelector(".island");
await waitForText(
page,
".island .server .island .server p",
"it works",
);
await assertNoPageComments(page);
},
);
},
);
Deno.test("render sibling islands", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_siblings`);
await page.waitForSelector(".island");
await waitForText(page, ".island .a", "it works");
await waitForText(page, ".island + .island .b", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("render sibling islands that render nothing initially", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_conditional`);
await page.waitForSelector(".island");
await assertNoPageComments(page);
await page.click("button");
// Button text is matched too, but this allows us
// to assert correct ordering. The "island content" should
// be left of "Toggle"
await waitForText(page, "#page", "island contentToggle");
await assertNoPageComments(page);
},
);
});
Deno.test("serialize inner island props", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_nested_props`);
await page.waitForSelector(".island");
await waitForText(page, ".island .island p", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("render island inside island when passed as fn child", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_fn_child`);
await page.waitForSelector(".island");
await waitForText(page, "#page", "it works");
await assertNoPageComments(page);
},
);
});
Deno.test("render nested islands in correct order", async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_order`);
await page.waitForSelector(".island");
await waitForText(page, "#page", "leftcenterright");
await assertNoPageComments(page);
},
);
});
Deno.test({
name: "render nested islands with server children conditionally",
async fn() {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_conditional_lazy`);
await waitForText(page, ".island p", "island content");
await assertNoPageComments(page);
await page.click("button");
await waitForText(page, ".island p", "server rendered");
await assertNoPageComments(page);
},
);
},
});
Deno.test(
"revive island in lazy server rendered children conditionally",
async () => {
await withPageName(
"./tests/fixture_island_nesting/main.ts",
async (page, address) => {
await page.goto(`${address}/island_conditional_lazy_island`);
await waitForText(page, ".island p", "island content");
await page.waitForSelector(".mounted");
await assertNoPageComments(page);
await page.click("button");
await waitForText(page, ".island .server", "server rendered");
await assertNoPageComments(page);
await page.click("button.counter");
await waitForText(page, ".island .count", "1");
await assertNoPageComments(page);
},
);
},
);
Deno.test("revive boolean attributes", async () => {
await withPageName(
"./tests/fixture/main.ts",
async (page, address) => {
await page.goto(`${address}/preact/boolean_attrs`);
await waitForText(page, ".form-revived", "Revived: true");
await assertNoPageComments(page);
const checked = await page.$eval(
"input[type=checkbox]",
(el) => el.checked,
);
assertEquals(checked, true, "Checkbox is not checked");
const required = await page.$eval(
"input[type=text]",
(el) => el.required,
);
assertEquals(required, true, "Text input is not marked as required");
const radioChecked = await page.$eval(
"input[type=radio][value='2']",
(el) => el.checked,
);
assertEquals(
radioChecked,
true,
"Text input is not marked as required",
);
const selected = await page.$eval(
"select",
(el) => el.options[el.selectedIndex].text,
);
assertEquals(selected, "bar", "'bar' value is not selected");
},
);
});
Deno.test("throws when passing non-jsx children to an island", async (t) => {
await withFakeServe(
"./tests/fixture_island_nesting/dev.ts",
async (server) => {
const overlay1 = await getErrorOverlay(
server,
"/island_invalid_children",
);
assertStringIncludes(
overlay1.title,
"Invalid JSX child passed to island",
);
const overlay2 = await getErrorOverlay(
server,
"/island_invalid_children_fn",
);
assertStringIncludes(
overlay2.title,
"Invalid JSX child passed to island",
);
await t.step("should not throw on valid children", async () => {
const doc2 = await server.getHtml(`/island_valid_children`);
assertNotSelector(doc2, "#fresh-error-overlay");
});
},
);
});
Deno.test("serves multiple islands in one file", async () => {
await withPageName(
"./tests/fixture_islands_multiple/dev.ts",
async (page, address) => {
await page.goto(address, { waitUntil: "networkidle2" });
await page.click(".single button");
await waitForText(page, ".single p", "Single Island: 1");
await page.click(".multiple1 button");
await waitForText(page, ".multiple1 p", "Multiple1 Island: 1");
await page.click(".multiple2 button");
await waitForText(page, ".multiple2 p", "Multiple2 Island: 1");
await page.click(".multipledefault button");
await waitForText(
page,
".multipledefault p",
"MultipleDefault Island: 1",
);
await page.click(".multipledefault1 button");
await waitForText(
page,
".multipledefault1 p",
"MultipleDefault1 Island: 1",
);
await page.click(".multipledefault2 button");
await waitForText(
page,
".multipledefault2 p",
"MultipleDefault2 Island: 1",
);
},
);
});