import { App, staticFiles } from "fresh"; 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 { ALL_ISLAND_DIR, buildProd, Doc, ISLAND_GROUP_DIR, withBrowserApp, } from "./test_utils.tsx"; import { parseHtml, waitForText } from "./test_utils.tsx"; import { expect } from "@std/expect"; import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx"; import { FnIsland } from "./fixtures_islands/FnIsland.tsx"; import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx"; import type { FreshConfig } from "../src/config.ts"; import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx"; import { FakeServer } from "../src/test_utils.ts"; import { PARTIAL_SEARCH_PARAM } from "../src/constants.ts"; import { ComputedSignal } from "./fixtures_islands/Computed.tsx"; import { EnvIsland } from "./fixtures_islands/EnvIsland.tsx"; Deno.env.set("FRESH_PUBLIC_TEST_FOO", "test-env-value"); Deno.env.set("FRESH_PRIVATE_TEST_FOO", "i-should-not-be-visible"); const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR }); const islandGroupCache = await buildProd({ root: ISLAND_GROUP_DIR }); function testApp(config?: FreshConfig): App { const app = new App(config) .use(staticFiles()) .fsRoutes(); allIslandCache(app); return app; } function testGroupApp(config?: FreshConfig): App { const app = new App(config) .use(staticFiles()) .fsRoutes(); islandGroupCache(app); return app; } Deno.test({ name: "islands - should make signals interactive", fn: async () => { const app = testApp() .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 app = testApp() .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 app = testApp() .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({ // Note that computed signals are detached because we cannot // serialize the whole signal tree at the moment. name: "islands - should serialize computed", fn: async () => { const app = testApp() .get("/", (ctx) => { return ctx.render( , ); }); await withBrowserApp(app, async (page, address) => { await page.goto(address, { waitUntil: "load" }); await page.locator("#comp.ready").wait(); await page.locator("#comp .trigger").click(); await waitForText(page, "#comp .output", "it works"); }); }, }); Deno.test({ name: "islands - import json", fn: async () => { const app = testApp() .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 app = testApp() .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 app = testApp() .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 app = testApp() .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 app = testApp() .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 app = testApp() .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 app = testApp() .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 app = testApp() .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 - nested children slots", fn: async () => { const app = testApp() .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("#a .increment").click(); await page.locator("#b .increment").click(); await waitForText(page, "#a .output", "1"); await waitForText(page, "#b .output", "1"); }); }, }); Deno.test({ name: "islands - conditional jsx children", fn: async () => { const app = testApp() .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 app = testApp() .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 app = testApp() .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 app = testApp() .get("/", (ctx) => { return ctx.render( , ); }) .get("/foo", (ctx) => { return ctx.render( alert('hey')`} /> , ); }) .get("/bar", (ctx) => { return ctx.render( alert('hey')`} /> , ); }); 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 p") .evaluate((el) => el.textContent!); expect(text).toEqual("it works"); // Page would error here const text2 = await page .locator(".ready div") .evaluate((el) => el.textContent!); expect(text2).toEqual(`"foo"asdf`); }); // Check escaping of ` { await page.goto(`${address}/foo`, { waitUntil: "load" }); await page.locator(".ready").wait(); const text = await page .locator(".ready p") .evaluate((el) => el.textContent!); expect(text).toEqual("it works"); const text2 = await page .locator(".ready div") .evaluate((el) => el.textContent!); expect(text2).toEqual(``); }); // Check escaping of `