import { App, staticFiles } from "fresh"; import { Head } from "fresh/runtime"; import { buildProd, parseHtml, waitFor, waitForText, withBrowserApp, } from "./test_utils.tsx"; import { expect } from "@std/expect"; import { FakeServer } from "../src/test_utils.ts"; import * as path from "@std/path"; const applyHeadCache = await buildProd({ root: path.join(import.meta.dirname!, "fixture_head"), }); Deno.test("Head - ssr - updates title", async () => { const handler = new App() .appWrapper(({ Component }) => { return ( not ok ); }) .get("/", (ctx) => { return ctx.render( <>

heading

ok , ); }).handler(); const server = new FakeServer(handler); const res = await server.get("/"); const doc = parseHtml(await res.text()); expect(doc.querySelector("title")?.textContent).toEqual("ok"); expect(doc.querySelector("h1")?.textContent).toEqual("heading"); }); Deno.test("Head - ssr - updates meta", async () => { const handler = new App() .appWrapper(({ Component }) => { return ( ok ); }) .get("/", (ctx) => { return ctx.render( <>

heading

, ); }).handler(); const server = new FakeServer(handler); const res = await server.get("/"); const doc = parseHtml(await res.text()); const meta = doc.querySelector("meta[name='foo']") as HTMLMetaElement; expect(meta.content).toEqual("ok"); }); Deno.test("Head - ssr - updates style", async () => { const handler = new App() .appWrapper(({ Component }) => { return ( ok ); }) .get("/", (ctx) => { return ctx.render( <>

heading

, ); }).handler(); const server = new FakeServer(handler); const res = await server.get("/"); const doc = parseHtml(await res.text()); const meta = doc.querySelector("style") as HTMLStyleElement; expect(meta.textContent).toEqual("ok"); }); Deno.test("Head - ssr - merge keyed", async () => { const handler = new App() .appWrapper(({ Component }) => { return ( ok ); }) .get("/", (ctx) => { return ctx.render( <>

heading

, ); }).handler(); const server = new FakeServer(handler); const res = await server.get("/"); const doc = parseHtml(await res.text()); const last = doc.head.lastChild; expect(last?.textContent).toEqual("ok"); }); Deno.test("Head - ssr - updates link", async () => { const handler = new App() .appWrapper(({ Component }) => { return ( ); }) .get("/", (ctx) => { return ctx.render( , ); }).handler(); const server = new FakeServer(handler); const res = await server.get("/"); const doc = parseHtml(await res.text()); const link = doc.querySelector("link[rel='canonical']") as HTMLLinkElement; expect(link.href).toEqual("https://example.com/ok"); }); Deno.test({ name: "Head - client - set title", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/title`); await page.locator(".ready").wait(); await page.locator("button").click(); await waitForText(page, "title", "Count: 1"); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - match meta", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/meta`); await page.locator(".ready").wait(); await waitFor(async () => { const metas = await page.evaluate(() => { const metas = Array.from( document.querySelectorAll("meta[name]"), ) as HTMLMetaElement[]; return metas.map(( el: HTMLMetaElement, ) => ({ name: el.name, content: el.content })); }); expect(metas).toEqual([ { name: "foo", content: "ok" }, { name: "bar", content: "not ok" }, ]); return true; }); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - match style by id", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/id`); await page.locator(".ready").wait(); await waitFor(async () => { const styles = await page.evaluate(() => { const els = Array.from( document.querySelectorAll("head style"), ) as HTMLStyleElement[]; return els.map(( el: HTMLStyleElement, ) => ({ id: el.id, text: el.textContent })); }); expect(styles).toEqual([ { id: "", text: "not ok" }, { id: "style-id", text: "ok" }, ]); return true; }); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - match key", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/key`); await page.locator(".ready").wait(); await waitFor(async () => { const tpls = await page.evaluate(() => { const els = Array.from( document.querySelectorAll("head template"), ) as HTMLTemplateElement[]; return els.map(( el: HTMLTemplateElement, ) => ({ key: el.getAttribute("data-key"), text: el.content.textContent, })); }); expect(tpls).toEqual([ { key: "a", text: "ok" }, { key: "b", text: "not ok" }, { key: null, text: "not ok" }, ]); return true; }); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - dynamic meta update", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/dynamic_meta`); await page.locator(".ready").wait(); // Verify initial meta value await waitFor(async () => { const content = await page.evaluate(() => { const el = document.querySelector( "meta[name='foo']", ) as HTMLMetaElement; return el?.content; }); expect(content).toEqual("value-0"); return true; }); // Click to update and verify meta changes reactively await page.locator("button").click(); await waitFor(async () => { const content = await page.evaluate(() => { const el = document.querySelector( "meta[name='foo']", ) as HTMLMetaElement; return el?.content; }); expect(content).toEqual("value-1"); return true; }); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - link element", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/link`); await page.locator(".ready").wait(); await waitFor(async () => { const href = await page.evaluate(() => { const el = document.querySelector( "link[rel='canonical']", ) as HTMLLinkElement; return el?.href; }); expect(href).toEqual("https://example.com/ok"); return true; }); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - multiple islands with Head", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/multi`); await page.locator(".ready-a").wait(); await page.locator(".ready-b").wait(); await waitFor(async () => { const title = await page.evaluate(() => document.title); expect(title).toEqual("from island A"); return true; }); await waitFor(async () => { const metas = await page.evaluate(() => { return { author: (document.querySelector( "meta[name='author']", ) as HTMLMetaElement)?.content, description: (document.querySelector( "meta[name='description']", ) as HTMLMetaElement)?.content, }; }); expect(metas.author).toEqual("island-a"); expect(metas.description).toEqual("from-island-b"); return true; }); }); }, sanitizeOps: false, sanitizeResources: false, }); Deno.test({ name: "Head - client - title updates multiple times", fn: async () => { const app = new App({}) .use(staticFiles()) .fsRoutes(); applyHeadCache(app); await withBrowserApp(app, async (page, address) => { await page.goto(`${address}/title`); await page.locator(".ready").wait(); await page.locator("button").click(); await waitForText(page, "title", "Count: 1"); await page.locator("button").click(); await waitForText(page, "title", "Count: 2"); await page.locator("button").click(); await waitForText(page, "title", "Count: 3"); }); }, sanitizeOps: false, sanitizeResources: false, });