fix: conditionally rendered element under fragment in island does not render (#1172)
This might be fixing #502, but I am not sure. All tests pass. I've added additional test cases for comments from #502 --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
+170
-104
@@ -2,6 +2,7 @@ import {
|
||||
assert,
|
||||
assertStringIncludes,
|
||||
delay,
|
||||
Page,
|
||||
puppeteer,
|
||||
TextLineStream,
|
||||
} from "./deps.ts";
|
||||
@@ -9,133 +10,198 @@ import {
|
||||
Deno.test({
|
||||
name: "island tests",
|
||||
async fn(t) {
|
||||
// Preparation
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
}).spawn();
|
||||
await withPage(async (page) => {
|
||||
async function counterTest(counterId: string, originalValue: number) {
|
||||
const pElem = await page.waitForSelector(`#${counterId} > p`);
|
||||
|
||||
const decoder = new TextDecoderStream();
|
||||
const lines = serverProcess.stdout
|
||||
.pipeThrough(decoder)
|
||||
.pipeThrough(new TextLineStream());
|
||||
let value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue}`, `${counterId} first value`);
|
||||
|
||||
let started = false;
|
||||
for await (const line of lines) {
|
||||
if (line.includes("Listening on http://")) {
|
||||
started = true;
|
||||
break;
|
||||
const buttonPlus = await page.$(`#b-${counterId}`);
|
||||
await buttonPlus?.click();
|
||||
|
||||
await delay(100);
|
||||
|
||||
value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue + 1}`, `${counterId} click`);
|
||||
}
|
||||
}
|
||||
if (!started) {
|
||||
|
||||
await page.goto("http://localhost:8000/islands", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("Ensure 3 islands on 1 page are revived", async () => {
|
||||
await counterTest("counter1", 3);
|
||||
await counterTest("counter2", 10);
|
||||
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("http://localhost:8000/islands"));
|
||||
const body = await resp.text();
|
||||
|
||||
const imgFilePath = body.match(/img id="img-in-island" src="(.*?)"/)
|
||||
?.[1]!;
|
||||
assertStringIncludes(srcString, imgFilePath);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
async function withPage(fn: (page: Page) => Promise<void>) {
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
}).spawn();
|
||||
|
||||
const textDecoderStream = new TextDecoderStream();
|
||||
const textLineStream = new TextLineStream();
|
||||
|
||||
const lines = serverProcess.stdout
|
||||
.pipeThrough(textDecoderStream)
|
||||
.pipeThrough(textLineStream);
|
||||
|
||||
try {
|
||||
if (!await didServerStart(lines)) {
|
||||
throw new Error("Server didn't start up");
|
||||
}
|
||||
|
||||
await delay(100);
|
||||
|
||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||
const page = await browser.newPage();
|
||||
|
||||
async function counterTest(counterId: string, originalValue: number) {
|
||||
const pElem = await page.waitForSelector(`#${counterId} > p`);
|
||||
let value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue}`, `${counterId} first value`);
|
||||
|
||||
const buttonPlus = await page.$(`#b-${counterId}`);
|
||||
await buttonPlus?.click();
|
||||
await delay(100);
|
||||
value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue + 1}`, `${counterId} click`);
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await fn(page);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
await page.goto("http://localhost:8000/islands", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("Ensure 3 islands on 1 page are revived", async () => {
|
||||
await counterTest("counter1", 3);
|
||||
await counterTest("counter2", 10);
|
||||
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("http://localhost:8000/islands"));
|
||||
const body = await resp.text();
|
||||
const imgFilePath = body.match(/img id="img-in-island" src="(.*?)"/)
|
||||
?.[1]!;
|
||||
assertStringIncludes(srcString, imgFilePath);
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
|
||||
} finally {
|
||||
await lines.cancel();
|
||||
|
||||
serverProcess.kill("SIGTERM");
|
||||
|
||||
// Wait until the process exits
|
||||
await serverProcess.status;
|
||||
}
|
||||
}
|
||||
|
||||
async function didServerStart(
|
||||
stdoutLines: ReadableStream<string>,
|
||||
): Promise<boolean> {
|
||||
for await (const line of stdoutLines) {
|
||||
if (line.includes("Listening on http://")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Deno.test({
|
||||
name: "island tests with </script>",
|
||||
|
||||
async fn(t) {
|
||||
await withPage(async (page) => {
|
||||
page.on("dialog", () => {
|
||||
assert(false, "There is XSS");
|
||||
});
|
||||
|
||||
await page.goto("http://localhost:8000/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`,
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "island tests with </script>",
|
||||
async fn(t) {
|
||||
// Preparation
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
}).spawn();
|
||||
name: "island with fragment as root",
|
||||
|
||||
const decoder = new TextDecoderStream();
|
||||
const lines = serverProcess.stdout
|
||||
.pipeThrough(decoder)
|
||||
.pipeThrough(new TextLineStream());
|
||||
async fn(_t) {
|
||||
await withPage(async (page) => {
|
||||
await page.goto("http://localhost:8000/islands/root_fragment", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
let started = false;
|
||||
for await (const line of lines) {
|
||||
if (line.includes("Listening on http://")) {
|
||||
started = true;
|
||||
break;
|
||||
const clickableSelector = "#root-fragment-click-me";
|
||||
|
||||
await page.waitForSelector(clickableSelector);
|
||||
|
||||
const contentBeforeClick = await getIslandParentTextContent();
|
||||
assert(contentBeforeClick === "HelloWorld");
|
||||
|
||||
await page.click(clickableSelector);
|
||||
await delay(100);
|
||||
|
||||
const contentAfterClick = await getIslandParentTextContent();
|
||||
assert(contentAfterClick === "HelloWorldI'm rendered now");
|
||||
|
||||
async function getIslandParentTextContent() {
|
||||
return await page.$eval(
|
||||
"#island-parent",
|
||||
(el: Element) => el.textContent,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!started) {
|
||||
throw new Error("Server didn't start up");
|
||||
}
|
||||
|
||||
await delay(100);
|
||||
|
||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||
const page = await browser.newPage();
|
||||
page.on("dialog", () => {
|
||||
assert(false, "There is XSS");
|
||||
});
|
||||
|
||||
await page.goto("http://localhost:8000/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`,
|
||||
);
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
|
||||
await lines.cancel();
|
||||
serverProcess.kill("SIGTERM");
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "island with fragment as root and conditional child first",
|
||||
|
||||
async fn(_t) {
|
||||
await withPage(async (page) => {
|
||||
await page.goto(
|
||||
"http://localhost:8000/islands/root_fragment_conditional_first",
|
||||
{
|
||||
waitUntil: "networkidle2",
|
||||
},
|
||||
);
|
||||
|
||||
const clickableSelector = "#root-fragment-conditional-first-click-me";
|
||||
await page.waitForSelector(clickableSelector);
|
||||
|
||||
const contentBeforeClick = await getIslandParentTextContent(page);
|
||||
assert(contentBeforeClick === "HelloWorld");
|
||||
|
||||
await page.click(clickableSelector);
|
||||
await delay(100);
|
||||
|
||||
const contentAfterClick = await getIslandParentTextContent(page);
|
||||
assert(contentAfterClick === "I'm rendered on topHelloWorld");
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
async function getIslandParentTextContent(page: Page) {
|
||||
return await page.$eval("#island-parent", (el: Element) => el.textContent);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user