import { App, fsRoutes } from "@fresh/core"; import { Counter } from "./fixtures_islands/Counter.tsx"; import { IslandInIsland } from "./fixtures_islands/IslandInIsland.tsx"; import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx"; import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx"; import { CounterWithSlots } from "./fixtures_islands/CounterWithSlots.tsx"; import { PassThrough } from "./fixtures_islands/PassThrough.tsx"; import { NullIsland } from "./fixtures_islands/NullIsland.tsx"; import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx"; import { JsxIsland } from "./fixtures_islands/JsxIsland.tsx"; import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx"; import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx"; import { signal } from "@preact/signals"; import { allIslandApp, buildProd, Doc, getIsland, withBrowserApp, } from "./test_utils.tsx"; import { parseHtml, waitForText } from "./test_utils.tsx"; import { staticFiles } from "../src/middlewares/static_files.ts"; import { expect } from "@std/expect"; import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx"; import { FnIsland } from "./fixtures_islands/FnIsland.tsx"; import { FragmentIsland } from "./fixtures_islands/FragmentIsland.tsx"; import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx"; import * as path from "@std/path"; import { setBuildCache } from "../src/app.ts"; import { getBuildCache } from "../src/app.ts"; await buildProd(allIslandApp); function testApp() { const app = new App(); setBuildCache(app, getBuildCache(allIslandApp)); return app; } Deno.test({ name: "islands - should make signals interactive", fn: async () => { const counterIsland = getIsland("Counter.tsx"); const app = testApp() .use(staticFiles()) .island(counterIsland, "Counter", Counter) .get("/", (ctx) => { const sig = signal(3); return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); await page.locator(".increment").click(); await waitForText(page, ".output", "4"); }); }, }); Deno.test({ name: "islands - revive multiple islands from one island file", fn: async () => { const multipleIslands = getIsland("Multiple.tsx"); const app = testApp() .use(staticFiles()) .island(multipleIslands, "Multiple1", Multiple1) .island(multipleIslands, "Multiple2", Multiple2) .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator("#multiple-1.ready").wait(); await page.locator("#multiple-2.ready").wait(); await page.locator("#multiple-1 .increment").click(); await page.locator("#multiple-2 .increment").click(); await waitForText(page, "#multiple-1 .output", "1"); await waitForText(page, "#multiple-2 .output", "1"); }); }, }); Deno.test({ name: "islands - revive multiple islands with shared signal", fn: async () => { const counterIsland = getIsland("Counter.tsx"); const app = testApp() .use(staticFiles()) .island(counterIsland, "Counter", Counter) .get("/", (ctx) => { const sig = signal(0); return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator("#counter-1.ready").wait(); await page.locator("#counter-2.ready").wait(); await page.locator("#counter-1 .increment").click(); await waitForText(page, "#counter-1 .output", "1"); await waitForText(page, "#counter-2 .output", "1"); }); }, }); Deno.test({ name: "islands - import json", fn: async () => { const jsonIsland = getIsland("JsonIsland.tsx"); const app = testApp() .use(staticFiles()) .island(jsonIsland, "JsonIsland", Counter) .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator("pre").wait(); const text = await page .locator("pre") .evaluate((el) => el.textContent!); const json = JSON.parse(text); expect(json).toEqual({ foo: 123 }); }); }, }); Deno.test({ name: "islands - returns null", fn: async () => { const nullIsland = getIsland("NullIsland.tsx"); const app = testApp() .use(staticFiles()) .island(nullIsland, "NullIsland", NullIsland) .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); }); }, }); Deno.test({ name: "islands - only instantiate top level island", fn: async () => { const counter = getIsland("Counter.tsx"); const islandInIsland = getIsland("IslandInIsland.tsx"); const app = testApp() .use(staticFiles()) .island(counter, "Counter", Counter) .island(islandInIsland, "IslandInIsland", IslandInIsland) .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); await page.locator(".trigger").click(); await waitForText(page, ".output", "1"); const html = await page.content(); expect(html).not.toContain("import { Counter }"); }); }, }); Deno.test({ name: "islands - pass null JSX props to islands", fn: async () => { const jsxIsland = getIsland("JsxIsland.tsx"); const app = testApp() .use(staticFiles()) .island(jsxIsland, "JsxIsland", JsxIsland) .get("/", (ctx) => { return ctx.render( {null} , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); const html = await page.content(); const doc = parseHtml(html); expect(doc.querySelector(".jsx")!.childNodes.length).toEqual(0); expect(doc.querySelector(".children")!.childNodes.length).toEqual(0); }); }, }); Deno.test({ name: "islands - pass JSX props to islands", fn: async () => { const jsxIsland = getIsland("JsxIsland.tsx"); const app = testApp() .use(staticFiles()) .island(jsxIsland, "JsxIsland", JsxIsland) .get("/", (ctx) => { return ctx.render( foo

}>

bar

, ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); const text = await page .locator("pre") .evaluate((el) => el.textContent!); expect(JSON.parse(text)).toEqual({ jsx: true, children: true }); }); }, }); Deno.test({ name: "islands - never serialize children prop", fn: async () => { const jsxChildrenIsland = getIsland("JsxChildrenIsland.tsx"); const app = testApp() .use(staticFiles()) .island(jsxChildrenIsland, "JsxChildrenIsland", JsxChildrenIsland) .get("/", (ctx) => { return ctx.render( foobar , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); const text = await page .locator("script") .evaluate((el) => el.textContent!); expect(text).not.toContain("foobar"); const childText = await page .locator(".after") .evaluate((el) => el.textContent!); expect(childText).toEqual("foobar"); }); }, }); Deno.test({ name: "islands - instantiate islands in jsx children", fn: async () => { const passThrough = getIsland("PassThrough.tsx"); const selfCounter = getIsland("SelfCounter.tsx"); const app = testApp() .use(staticFiles()) .island(passThrough, "PassThrough", PassThrough) .island(selfCounter, "SelfCounter", SelfCounter) .get("/", (ctx) => { return ctx.render(
, ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); await page.locator(".increment").click(); await waitForText(page, ".output", "1"); }); }, }); Deno.test({ name: "islands - instantiate islands in jsx children with slots", fn: async () => { const counterWithSlots = getIsland("CounterWithSlots.tsx"); const selfCounter = getIsland("SelfCounter.tsx"); const app = testApp() .use(staticFiles()) .island(counterWithSlots, "CounterWithSlots", CounterWithSlots) .island(selfCounter, "SelfCounter", SelfCounter) .get("/", (ctx) => { return ctx.render( } >
, ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); await page.locator(".jsx .increment").click(); await page.locator(".children .increment").click(); await page.locator(".counter-with-children button").click(); await waitForText(page, ".counter-with-children .output", "1"); await waitForText(page, ".jsx .output", "1"); await waitForText(page, ".children .output", "1"); }); }, }); Deno.test({ name: "islands - conditional jsx children", fn: async () => { const jsxConditional = getIsland("JsxConditional.tsx"); const selfCounter = getIsland("SelfCounter.tsx"); const app = testApp() .use(staticFiles()) .island(jsxConditional, "JsxConditional", JsxConditional) .island(selfCounter, "SelfCounter", SelfCounter) .get("/", (ctx) => { return ctx.render( } >
, ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); await page.locator(".jsx .increment").click(); await page.locator(".children .increment").click(); await page.locator(".cond-update").click(); await waitForText(page, ".cond-output", "1"); await waitForText(page, ".jsx .output", "1"); await waitForText(page, ".children .output", "1"); }); }, }); Deno.test({ name: "islands - revive DOM attributes", fn: async () => { const jsxConditional = getIsland("JsxConditional.tsx"); const app = testApp() .use(staticFiles()) .island(jsxConditional, "JsxConditional", JsxConditional) .get("/", (ctx) => { return ctx.render(
, ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); await page.locator(".children > .foo").wait(); const checkboxChecked = await page .locator("input[name='check']") .evaluate((el) => el.checked); expect(checkboxChecked).toEqual(true); const required = await page .locator("input[name='text']") .evaluate((el) => el.required); expect(required).toEqual(true); const radio1 = await page .locator("input[type='radio'][value='1']") .evaluate((el) => el.checked); expect(radio1).toEqual(false); const radio2 = await page .locator("input[type='radio'][value='2']") .evaluate((el) => el.checked); expect(radio2).toEqual(true); }); }, }); Deno.test({ name: "islands - revive island with fn inside", fn: async () => { const fragmentIsland = getIsland("FragmentIsland.tsx"); const fnIsland = getIsland("FnIsland.tsx"); const app = testApp() .use(staticFiles()) .island(fragmentIsland, "FragmentIsland", FragmentIsland) .island(fnIsland, "FnIsland", FnIsland) .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); const text = await page .locator(".ready") .evaluate((el) => el.textContent!); expect(text).toEqual("it works"); }); }, }); Deno.test({ name: "islands - escape props", fn: async () => { const escapeIsland = getIsland("EscapeIsland.tsx"); const app = testApp() .use(staticFiles()) .island(escapeIsland, "EscapeIsland", EscapeIsland) .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator(".ready").wait(); // Page would error here const text = await page .locator(".ready") .evaluate((el) => el.textContent!); expect(text).toEqual("it works"); }); }, }); Deno.test({ name: "islands - stub Node 'process.env'", fn: async () => { const nodeProcess = getIsland("NodeProcess.tsx"); const app = testApp() .use(staticFiles()) .island(nodeProcess, "NodeProcess", NodeProcess) .get("/", (ctx) => ctx.render( , )); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/`, { waitUntil: "load" }); await page.locator(".ready").wait(); // Page would error here const text = await page .locator(".ready") .evaluate((el) => el.textContent!); expect(text).toEqual("value: production"); }); }, }); Deno.test({ name: "fsRoutes - load islands from group folder", fn: async () => { const app = testApp() .use(staticFiles()); await fsRoutes(app, { dir: path.join( import.meta.dirname!, "fixture_island_groups", ), loadIsland: (path) => import("./fixture_island_groups/islands/" + path), loadRoute: (path) => import("./fixture_island_groups/routes/" + path), }); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/foo`, { waitUntil: "load" }); await page.locator(".ready").wait(); // Page would error here const text = await page .locator(".ready") .evaluate((el) => el.textContent!); expect(text).toEqual("it works"); }); }, });