diff --git a/tests/cli_test.ts b/tests/cli_test.ts index c61bc2bf..6f177d77 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -12,7 +12,11 @@ import { puppeteer, retry, } from "./deps.ts"; -import { startFreshServer } from "./test_utils.ts"; +import { + clickWhenListenerReady, + startFreshServer, + waitForText, +} from "./test_utils.ts"; const assertFileExistence = async (files: string[], dirname: string) => { for (const filePath of files) { @@ -85,12 +89,12 @@ Deno.test({ let counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "3"); - const buttonPlus = await page.$( + await clickWhenListenerReady( + page, "body > div > div > div > button:nth-child(3)", ); - await buttonPlus?.click(); - await delay(100); + await waitForText(page, "body > div > div > div > p", "4"); counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "4"); @@ -99,7 +103,7 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - await delay(100); + await serverProcess.status; }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); @@ -183,7 +187,7 @@ Deno.test({ ); await buttonPlus?.click(); - await delay(100); + await waitForText(page, "body > div > div > div > p", "4"); counterValue = await counter?.evaluate((el) => el.textContent); assert(counterValue === "4"); @@ -192,7 +196,7 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - await delay(100); + await serverProcess.status; }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); @@ -322,7 +326,7 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - await delay(100); + await serverProcess.status; }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); diff --git a/tests/islands_test.ts b/tests/islands_test.ts index f083ae28..324d6f6d 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -5,7 +5,11 @@ import { delay, Page, } from "./deps.ts"; -import { withPageName } from "./test_utils.ts"; +import { + clickWhenListenerReady, + waitForText, + withPageName, +} from "./test_utils.ts"; Deno.test({ name: "island tests", @@ -14,16 +18,11 @@ Deno.test({ async function counterTest(counterId: string, originalValue: number) { const pElem = await page.waitForSelector(`#${counterId} > p`); - let value = await pElem?.evaluate((el) => el.textContent); + const 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`); + await clickWhenListenerReady(page, `#b-${counterId}`); + await waitForText(page, `#${counterId} > p`, String(originalValue + 1)); } await page.goto(`${address}/islands`, { @@ -66,16 +65,11 @@ Deno.test({ async function counterTest(counterId: string, originalValue: number) { const pElem = await page.waitForSelector(`#${counterId} > p`); - let value = await pElem?.evaluate((el) => el.textContent); + const 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`); + await clickWhenListenerReady(page, `#b-${counterId}`); + await waitForText(page, `#${counterId} > p`, String(originalValue + 1)); } await page.goto(`${address}/islands/multiple_island_exports`, { @@ -141,21 +135,10 @@ Deno.test({ await page.waitForSelector(clickableSelector); - const contentBeforeClick = await getIslandParentTextContent(); - assert(contentBeforeClick === "HelloWorld"); + await waitForText(page, `#island-parent`, "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, - ); - } + await clickWhenListenerReady(page, clickableSelector); + await waitForText(page, `#island-parent`, "HelloWorldI'm rendered now"); }); }, @@ -178,14 +161,14 @@ Deno.test({ const clickableSelector = "#root-fragment-conditional-first-click-me"; await page.waitForSelector(clickableSelector); - const contentBeforeClick = await getIslandParentTextContent(page); - assert(contentBeforeClick === "HelloWorld"); + await waitForText(page, "#island-parent", "HelloWorld"); - await page.click(clickableSelector); - await delay(100); - - const contentAfterClick = await getIslandParentTextContent(page); - assert(contentAfterClick === "I'm rendered on topHelloWorld"); + await clickWhenListenerReady(page, clickableSelector); + await waitForText( + page, + "#island-parent", + "I'm rendered on topHelloWorld", + ); }); }, @@ -193,10 +176,6 @@ Deno.test({ sanitizeResources: false, }); -async function getIslandParentTextContent(page: Page) { - return await page.$eval("#island-parent", (el: Element) => el.textContent); -} - Deno.test({ name: "island that returns `null`", @@ -244,10 +223,7 @@ Deno.test({ waitUntil: "networkidle2", }); await page.waitForSelector("#foo"); - - await delay(100); - const text = await page.$eval("#foo", (el) => el.textContent); - assertEquals(text, "it works"); + await waitForText(page, "#foo", "it works"); }, ); }, @@ -267,10 +243,7 @@ Deno.test({ waitUntil: "networkidle2", }); await page.waitForSelector(".island"); - - await delay(100); - const text = await page.$eval(".island", (el) => el.textContent); - assertEquals(text, "it works"); + await waitForText(page, ".island", "it works"); }, ); }, @@ -336,13 +309,7 @@ Deno.test({ waitUntil: "networkidle2", }); await page.waitForSelector(".island"); - - await delay(100); - const text = await page.$eval( - ".island .island p", - (el) => el.textContent, - ); - assertEquals(text, "it works"); + await waitForText(page, ".island .island p", "it works"); }, ); }, @@ -363,17 +330,11 @@ Deno.test({ }); await page.waitForSelector(".island"); - await delay(100); - const text = await page.$eval( - ".island .island p", - (el) => el.textContent, - ); - assertEquals(text, "it works"); + await waitForText(page, ".island .island p", "it works"); // Check that there is no duplicated content which could happen // when islands aren't initialized correctly - const pageText = await page.$eval("#page", (el) => el.textContent); - assertEquals(pageText, "it works"); + await waitForText(page, "#page", "it works"); }, ); }, @@ -395,12 +356,11 @@ Deno.test({ }); await page.waitForSelector(".island"); - await delay(100); - const text = await page.$eval( + await waitForText( + page, ".island .server .island .server p", - (el) => el.textContent, + "it works", ); - assertEquals(text, "it works"); }, ); }, @@ -421,18 +381,8 @@ Deno.test({ }); await page.waitForSelector(".island"); - await delay(100); - const text = await page.$eval( - ".island .a", - (el) => el.textContent, - ); - assertEquals(text, "it works"); - - const text2 = await page.$eval( - ".island + .island .b", - (el) => el.textContent, - ); - assertEquals(text2, "it works"); + await waitForText(page, ".island .a", "it works"); + await waitForText(page, ".island + .island .b", "it works"); }, ); }, @@ -484,12 +434,7 @@ Deno.test({ }); await page.waitForSelector(".island"); - await delay(100); - const text = await page.$eval( - ".island .island p", - (el) => el.textContent, - ); - assertEquals(text, "it works"); + await waitForText(page, ".island .island p", "it works"); }, ); }, diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 693a379d..66ef05b5 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -60,6 +60,50 @@ export async function startFreshServerExpectErrors( return output; } +/** + * Click on an element once it has an attached click listener + */ +export async function clickWhenListenerReady(page: Page, selector: string) { + await page.waitForSelector(selector); + await page.waitForFunction( + (sel) => { + const el = document.querySelector(sel)!; + + // Wait for Preact to have attached either a captured or non-captured + // click event + // deno-lint-ignore no-explicit-any + const preactListener = (el as any).l as Record | null; + if ( + !preactListener || typeof preactListener !== "object" || + (!preactListener.clickfalse && !preactListener.clicktrue) + ) { + return false; + } + + return true; + }, + {}, + selector, + ); + await page.click(selector); +} + +export async function waitForText( + page: Page, + selector: string, + text: string, +) { + await page.waitForSelector(selector); + await page.waitForFunction( + (sel, value) => { + return document.querySelector(sel)!.textContent === value; + }, + { timeout: 2000 }, + selector, + String(text), + ); +} + async function spawnServer( options: Deno.CommandOptions, expectErrors = false,