Add Partials (#1824)

This commit is contained in:
Marvin Hagemeister
2023-09-27 13:34:30 +02:00
committed by GitHub
parent d9829e25e6
commit 665d35bbc2
92 changed files with 3271 additions and 116 deletions
-4
View File
@@ -69,10 +69,6 @@ async function testBuild(
Array.isArray(snapshot.files["signals.js"]),
"signals.js output file not found in snapshot",
);
assert(
Array.isArray(snapshot.files["deserializer.js"]),
"deserializer.js output file not found in snapshot",
);
// Should not include `preact/debug`
const mainJs = await Deno.readTextFile(path.join(outDir, "main.js"));
@@ -0,0 +1,5 @@
import { ComponentChildren } from "preact";
export function Keyed(props: { children?: ComponentChildren }) {
return <>{props.children}</>;
}
+20
View File
@@ -0,0 +1,20 @@
{
"lock": false,
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts"
},
"imports": {
"$fresh/": "../../",
"twind": "https://esm.sh/twind@0.16.19",
"twind/": "https://esm.sh/twind@0.16.19/",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+160
View File
@@ -0,0 +1,160 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/_app.tsx";
import * as $1 from "./routes/client_nav/_layout.tsx";
import * as $2 from "./routes/client_nav/index.tsx";
import * as $3 from "./routes/client_nav/injected.tsx";
import * as $4 from "./routes/client_nav/page-a.tsx";
import * as $5 from "./routes/client_nav/page-b.tsx";
import * as $6 from "./routes/client_nav/page-c.tsx";
import * as $7 from "./routes/client_nav_opt_out/_layout.tsx";
import * as $8 from "./routes/client_nav_opt_out/index.tsx";
import * as $9 from "./routes/client_nav_opt_out/injected.tsx";
import * as $10 from "./routes/client_nav_opt_out/page-a.tsx";
import * as $11 from "./routes/client_nav_opt_out/page-b.tsx";
import * as $12 from "./routes/client_nav_opt_out/page-c.tsx";
import * as $13 from "./routes/form/index.tsx";
import * as $14 from "./routes/form/injected.tsx";
import * as $15 from "./routes/form/update.tsx";
import * as $16 from "./routes/index.tsx";
import * as $17 from "./routes/island_instance/index.tsx";
import * as $18 from "./routes/island_instance/injected.tsx";
import * as $19 from "./routes/island_instance/partial.tsx";
import * as $20 from "./routes/island_instance/partial_remove.tsx";
import * as $21 from "./routes/island_instance/partial_replace.tsx";
import * as $22 from "./routes/island_instance_multiple/index.tsx";
import * as $23 from "./routes/island_instance_multiple/injected.tsx";
import * as $24 from "./routes/island_instance_multiple/partial.tsx";
import * as $25 from "./routes/island_instance_multiple/partial_both.tsx";
import * as $26 from "./routes/island_instance_nested/index.tsx";
import * as $27 from "./routes/island_instance_nested/injected.tsx";
import * as $28 from "./routes/island_instance_nested/partial.tsx";
import * as $29 from "./routes/island_instance_nested/replace.tsx";
import * as $30 from "./routes/island_props/index.tsx";
import * as $31 from "./routes/island_props/injected.tsx";
import * as $32 from "./routes/island_props/partial.tsx";
import * as $33 from "./routes/island_props_signals/index.tsx";
import * as $34 from "./routes/island_props_signals/injected.tsx";
import * as $35 from "./routes/island_props_signals/partial.tsx";
import * as $36 from "./routes/keys/index.tsx";
import * as $37 from "./routes/keys/injected.tsx";
import * as $38 from "./routes/keys/swap.tsx";
import * as $39 from "./routes/keys_components/index.tsx";
import * as $40 from "./routes/keys_components/injected.tsx";
import * as $41 from "./routes/keys_components/swap.tsx";
import * as $42 from "./routes/keys_dom/index.tsx";
import * as $43 from "./routes/keys_dom/injected.tsx";
import * as $44 from "./routes/keys_dom/swap.tsx";
import * as $45 from "./routes/loading/index.tsx";
import * as $46 from "./routes/loading/injected.tsx";
import * as $47 from "./routes/loading/update.tsx";
import * as $48 from "./routes/missing_partial/index.tsx";
import * as $49 from "./routes/missing_partial/injected.tsx";
import * as $50 from "./routes/missing_partial/update.tsx";
import * as $51 from "./routes/mode/append.tsx";
import * as $52 from "./routes/mode/index.tsx";
import * as $53 from "./routes/mode/injected.tsx";
import * as $54 from "./routes/mode/prepend.tsx";
import * as $55 from "./routes/mode/replace.tsx";
import * as $56 from "./routes/no_islands/index.tsx";
import * as $57 from "./routes/no_islands/injected.tsx";
import * as $58 from "./routes/no_islands/update.tsx";
import * as $59 from "./routes/partial_slot_inside_island.tsx";
import * as $$0 from "./islands/Counter.tsx";
import * as $$1 from "./islands/CounterA.tsx";
import * as $$2 from "./islands/CounterB.tsx";
import * as $$3 from "./islands/Fader.tsx";
import * as $$4 from "./islands/InvalidSlot.tsx";
import * as $$5 from "./islands/Logger.tsx";
import * as $$6 from "./islands/Other.tsx";
import * as $$7 from "./islands/PartialTrigger.tsx";
import * as $$8 from "./islands/PassThrough.tsx";
import * as $$9 from "./islands/PropIsland.tsx";
import * as $$10 from "./islands/SignalProp.tsx";
import * as $$11 from "./islands/Spinner.tsx";
import * as $$12 from "./islands/Stateful.tsx";
const manifest = {
routes: {
"./routes/_app.tsx": $0,
"./routes/client_nav/_layout.tsx": $1,
"./routes/client_nav/index.tsx": $2,
"./routes/client_nav/injected.tsx": $3,
"./routes/client_nav/page-a.tsx": $4,
"./routes/client_nav/page-b.tsx": $5,
"./routes/client_nav/page-c.tsx": $6,
"./routes/client_nav_opt_out/_layout.tsx": $7,
"./routes/client_nav_opt_out/index.tsx": $8,
"./routes/client_nav_opt_out/injected.tsx": $9,
"./routes/client_nav_opt_out/page-a.tsx": $10,
"./routes/client_nav_opt_out/page-b.tsx": $11,
"./routes/client_nav_opt_out/page-c.tsx": $12,
"./routes/form/index.tsx": $13,
"./routes/form/injected.tsx": $14,
"./routes/form/update.tsx": $15,
"./routes/index.tsx": $16,
"./routes/island_instance/index.tsx": $17,
"./routes/island_instance/injected.tsx": $18,
"./routes/island_instance/partial.tsx": $19,
"./routes/island_instance/partial_remove.tsx": $20,
"./routes/island_instance/partial_replace.tsx": $21,
"./routes/island_instance_multiple/index.tsx": $22,
"./routes/island_instance_multiple/injected.tsx": $23,
"./routes/island_instance_multiple/partial.tsx": $24,
"./routes/island_instance_multiple/partial_both.tsx": $25,
"./routes/island_instance_nested/index.tsx": $26,
"./routes/island_instance_nested/injected.tsx": $27,
"./routes/island_instance_nested/partial.tsx": $28,
"./routes/island_instance_nested/replace.tsx": $29,
"./routes/island_props/index.tsx": $30,
"./routes/island_props/injected.tsx": $31,
"./routes/island_props/partial.tsx": $32,
"./routes/island_props_signals/index.tsx": $33,
"./routes/island_props_signals/injected.tsx": $34,
"./routes/island_props_signals/partial.tsx": $35,
"./routes/keys/index.tsx": $36,
"./routes/keys/injected.tsx": $37,
"./routes/keys/swap.tsx": $38,
"./routes/keys_components/index.tsx": $39,
"./routes/keys_components/injected.tsx": $40,
"./routes/keys_components/swap.tsx": $41,
"./routes/keys_dom/index.tsx": $42,
"./routes/keys_dom/injected.tsx": $43,
"./routes/keys_dom/swap.tsx": $44,
"./routes/loading/index.tsx": $45,
"./routes/loading/injected.tsx": $46,
"./routes/loading/update.tsx": $47,
"./routes/missing_partial/index.tsx": $48,
"./routes/missing_partial/injected.tsx": $49,
"./routes/missing_partial/update.tsx": $50,
"./routes/mode/append.tsx": $51,
"./routes/mode/index.tsx": $52,
"./routes/mode/injected.tsx": $53,
"./routes/mode/prepend.tsx": $54,
"./routes/mode/replace.tsx": $55,
"./routes/no_islands/index.tsx": $56,
"./routes/no_islands/injected.tsx": $57,
"./routes/no_islands/update.tsx": $58,
"./routes/partial_slot_inside_island.tsx": $59,
},
islands: {
"./islands/Counter.tsx": $$0,
"./islands/CounterA.tsx": $$1,
"./islands/CounterB.tsx": $$2,
"./islands/Fader.tsx": $$3,
"./islands/InvalidSlot.tsx": $$4,
"./islands/Logger.tsx": $$5,
"./islands/Other.tsx": $$6,
"./islands/PartialTrigger.tsx": $$7,
"./islands/PassThrough.tsx": $$8,
"./islands/PropIsland.tsx": $$9,
"./islands/SignalProp.tsx": $$10,
"./islands/Spinner.tsx": $$11,
"./islands/Stateful.tsx": $$12,
},
baseUrl: import.meta.url,
};
export default manifest;
@@ -0,0 +1,16 @@
import { useSignal } from "@preact/signals";
import { Logger } from "./Logger.tsx";
export default function Counter(props: { id: string }) {
const sig = useSignal(0);
return (
<Logger name={`Counter ${props.id}`}>
<div class="island">
<p class={`output-${props.id}`}>{sig.value}</p>
<button onClick={() => sig.value += 1}>
update
</button>
</div>
</Logger>
);
}
@@ -0,0 +1,16 @@
import { useSignal } from "@preact/signals";
import { Logger } from "./Logger.tsx";
export default function CounterA() {
const sig = useSignal(0);
return (
<Logger name="Counter A">
<div class="island island-a">
<p class="output-a">{sig.value}</p>
<button onClick={() => sig.value += 1}>
update
</button>
</div>
</Logger>
);
}
@@ -0,0 +1,16 @@
import { useSignal } from "@preact/signals";
import { Logger } from "./Logger.tsx";
export default function CounterB() {
const sig = useSignal(0);
return (
<Logger name="Counter B">
<div class="island island-b">
<p class="output-b">{sig.value}</p>
<button onClick={() => sig.value += 1}>
update
</button>
</div>
</Logger>
);
}
+26
View File
@@ -0,0 +1,26 @@
import { useEffect, useRef } from "preact/hooks";
import { ComponentChildren } from "preact";
export function Fader(props: { children?: ComponentChildren }) {
const ref = useRef<HTMLDivElement>(null);
const mounted = useRef(false);
useEffect(() => {
if (!mounted.current) {
mounted.current = true;
return;
}
if (ref.current) {
ref.current.animate([
{ backgroundColor: "white" },
{ backgroundColor: "peachpuff" },
], {
fill: "backwards",
easing: "ease-out",
duration: 600,
});
}
});
return <div ref={ref}>{props.children}</div>;
}
@@ -0,0 +1,11 @@
import { Partial } from "$fresh/runtime.ts";
export default function InvalidSlot() {
return (
<div class="island">
<Partial name="invalid">
<h1>it doesn't work</h1>
</Partial>
</div>
);
}
+30
View File
@@ -0,0 +1,30 @@
import { ComponentChildren } from "preact";
import { useEffect, useRef } from "preact/hooks";
export function Logger(props: { children?: ComponentChildren; name?: string }) {
const mounted = useRef(false);
useEffect(() => {
mounted.current = true;
const logs = document.querySelector("#logs");
if (logs) {
logs.textContent += `mount ${props.name}\n`;
}
return () => {
if (logs) {
logs.textContent += `unmount ${props.name}\n`;
}
};
}, []);
if (mounted.current && typeof document !== "undefined") {
const logs = document.querySelector("#logs");
if (logs) {
logs.textContent += `update ${props.name}\n`;
}
}
// deno-lint-ignore no-explicit-any
return props.children as any;
}
+16
View File
@@ -0,0 +1,16 @@
import { useSignal } from "@preact/signals";
import { Logger } from "./Logger.tsx";
export default function Other() {
const sig = useSignal(0);
return (
<Logger name="Other">
<div class="island island-other">
<p class="output-other">{sig.value}</p>
<button onClick={() => sig.value += 1}>
update
</button>
</div>
</Logger>
);
}
@@ -0,0 +1,23 @@
import { Signal } from "@preact/signals";
import { ComponentChildren } from "preact";
export default function PartialTrigger(
props: {
class: string;
href: string;
partial?: string;
loading?: Signal<boolean>;
children?: ComponentChildren;
},
) {
return (
<a
class={props.class}
href={props.href}
fh-partial={props.partial}
fh-loading={props.loading}
>
{props.children}
</a>
);
}
@@ -0,0 +1,15 @@
import { Fader } from "$fresh/tests/fixture_partials/islands/Fader.tsx";
import { Logger } from "./Logger.tsx";
import { ComponentChildren } from "preact";
export default function PassThrough(props: { children?: ComponentChildren }) {
return (
<Fader>
<Logger name="PassThrough">
<div class="island">
{props.children}
</div>
</Logger>
</Fader>
);
}
@@ -0,0 +1,22 @@
export default function PropIsland(
props: {
number: number;
string: string;
boolean: boolean;
strArr: string[];
obj: { foo: number };
},
) {
return (
<pre class="pre-props">
{JSON.stringify({
number: props.number,
string: props.string,
null: null,
boolean: props.boolean,
object: props.obj,
strArr: props.strArr,
},null,2)}
</pre>
);
}
@@ -0,0 +1,12 @@
import { Signal } from "@preact/signals";
export default function SignalProp(props: { sig: Signal<number> }) {
return (
<div class="island">
<p class="output">{props.sig.value}</p>
<button onClick={() => props.sig.value += 1}>
update
</button>
</div>
);
}
@@ -0,0 +1,7 @@
import { Signal } from "@preact/signals";
export default function Spinner(props: { id: string; show: Signal<boolean> }) {
return props.show.value
? <p class={`spinner spinner-${props.id}`}>loading...</p>
: null;
}
@@ -0,0 +1,13 @@
import { useState } from "preact/hooks";
export default function Stateful(props: { id: string }) {
const [v, set] = useState(0);
return (
<div class="island">
<p class={`output-${props.id}`}>{v}</p>
<button class={`btn-${props.id}`} onClick={() => set((v) => v + 1)}>
update
</button>
</div>
);
}
+9
View File
@@ -0,0 +1,9 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
+16
View File
@@ -0,0 +1,16 @@
import { defineApp } from "$fresh/src/server/defines.ts";
export default defineApp((res, ctx) => {
return (
<html>
<head>
<meta charSet="utf-8" />
<title>test</title>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
<ctx.Component />
</body>
</html>
);
});
@@ -0,0 +1,48 @@
import { AppProps } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
export default function AppLayout({ Component }: AppProps) {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>fresh title</title>
</head>
<body fresh-client-nav="true">
<Partial name="body">
<Fader>
<Component />
</Fader>
</Partial>
<p>
<a
class="page-a-link"
href="/client_nav/page-a"
>
Page A
</a>
</p>
<p>
<a
class="page-b-link"
href="/client_nav/page-b"
>
Page B
</a>
</p>
<p>
<a
class="page-c-link"
href="/client_nav/page-c"
>
Page C
</a>
</p>
<pre id="logs" />
</body>
</html>
);
}
@@ -0,0 +1,21 @@
import { Partial } from "$fresh/runtime.ts";
import { RouteConfig } from "$fresh/server.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function ModeDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p class="status-initial">Initial content</p>
<CounterA />
</Fader>
</Partial>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/client_nav",
},
});
});
@@ -0,0 +1,17 @@
import { RouteConfig } from "$fresh/server.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function PageA() {
return (
<Fader>
<h1>Page A</h1>
<p>asdfasdf asdf asdf</p>
<CounterA />
</Fader>
);
}
@@ -0,0 +1,20 @@
import { RouteConfig } from "$fresh/server.ts";
import { Fader } from "../../islands/Fader.tsx";
import CounterB from "../../islands/CounterB.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function PageB() {
return (
<Fader>
<i>something before</i>
<div>
<h1>Page B</h1>
</div>
<CounterB />
<p>asdfasdf asdf asdf</p>
</Fader>
);
}
@@ -0,0 +1,17 @@
import { RouteConfig } from "$fresh/server.ts";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function PageB() {
return (
<Fader>
<h1>Page C</h1>
<span class="page-c-text">
<p>asdfasdf asdf asdf</p>
</span>
</Fader>
);
}
@@ -0,0 +1,48 @@
import { AppProps } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
export default function AppLayout({ Component }: AppProps) {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>fresh title</title>
</head>
<body fresh-client-nav="false">
<Partial name="body">
<Fader>
<Component />
</Fader>
</Partial>
<p>
<a
class="page-a-link"
href="/client_nav_opt_out/page-a"
>
Page A
</a>
</p>
<p>
<a
class="page-b-link"
href="/client_nav_opt_out/page-b"
>
Page B
</a>
</p>
<p>
<a
class="page-c-link"
href="/client_nav_opt_out/page-c"
>
Page C
</a>
</p>
<pre id="logs" />
</body>
</html>
);
}
@@ -0,0 +1,21 @@
import { Partial } from "$fresh/runtime.ts";
import { RouteConfig } from "$fresh/server.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function ModeDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p class="status-initial">Initial content</p>
<CounterA />
</Fader>
</Partial>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/client_nav_opt_out",
},
});
});
@@ -0,0 +1,17 @@
import { RouteConfig } from "$fresh/server.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function PageA() {
return (
<Fader>
<h1>Page A</h1>
<p>asdfasdf asdf asdf</p>
<CounterA />
</Fader>
);
}
@@ -0,0 +1,20 @@
import { RouteConfig } from "$fresh/server.ts";
import { Fader } from "../../islands/Fader.tsx";
import CounterB from "../../islands/CounterB.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function PageB() {
return (
<Fader>
<i>something before</i>
<div>
<h1>Page B</h1>
</div>
<CounterB />
<p>asdfasdf asdf asdf</p>
</Fader>
);
}
@@ -0,0 +1,17 @@
import { RouteConfig } from "$fresh/server.ts";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
};
export default function PageB() {
return (
<Fader>
<h1>Page C</h1>
<span class="page-c-text">
<p>asdfasdf asdf asdf</p>
</span>
</Fader>
);
}
@@ -0,0 +1,19 @@
import { Partial } from "$fresh/runtime.ts";
export default function SlotDemo() {
return (
<div>
<form fh-partial="/form/update">
<Partial name="slot-1">
<p class="status">Default content</p>
<p>
<input type="text" value="foo" />
</p>
</Partial>
<button type="submit" class="submit">
submit
</button>
</form>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/form",
},
});
});
@@ -0,0 +1,19 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<p class="status-updated">it works</p>
<p>
<input type="text" />
</p>
</Partial>
);
});
+27
View File
@@ -0,0 +1,27 @@
import * as path from "https://deno.land/std@0.192.0/path/mod.ts";
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
const links: string[] = [];
for (const file of Deno.readDirSync(__dirname)) {
if (file.name.startsWith("index")) continue;
const name = path.basename(file.name, path.extname(file.name));
links.push(name);
}
export default function Home() {
return (
<div>
<h1>Tests</h1>
<ul>
{links.sort().map((link) => {
return (
<li key={link}>
<a href={`/${link}`}>{link}</a>
</li>
);
})}
</ul>
</div>
);
}
@@ -0,0 +1,44 @@
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export default function SlotDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p>Initial content</p>
<CounterA />
</Fader>
</Partial>
<p>
<a
class="update-link"
href="/island_instance/injected"
fh-partial="/island_instance/partial"
>
Update
</a>
</p>
<p>
<a
class="remove-link"
href="/island_instance/injected"
fh-partial="/island_instance/partial_remove"
>
Remove
</a>
</p>
<p>
<a
class="replace-link"
href="/island_instance/injected"
fh-partial="/island_instance/partial_replace"
>
Replace
</a>
</p>
<pre id="logs" />
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/island_instance",
},
});
});
@@ -0,0 +1,22 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<h1>Another page</h1>
<p class="status">updated content</p>
<CounterA />
</Fader>
</Partial>
);
});
@@ -0,0 +1,19 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status">no islands</p>
</Fader>
</Partial>
);
});
@@ -0,0 +1,21 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Other from "../../islands/Other.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status-replaced">replaced content</p>
<Other />
</Fader>
</Partial>
);
});
@@ -0,0 +1,46 @@
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import CounterB from "../../islands/CounterB.tsx";
import { Fader } from "../../islands/Fader.tsx";
export default function SlotDemo() {
return (
<div>
<div id="output">
<Partial name="slot-1">
<Fader>
<p>Initial content slot 1</p>
<CounterA />
</Fader>
</Partial>
<hr />
<Partial name="slot-2">
<Fader>
<p>Initial content slot 2</p>
<CounterB />
</Fader>
</Partial>
<hr />
</div>
<p>
<a
class="update-second-link"
href="/island_instance_multiple/injected"
fh-partial="/island_instance_multiple/partial"
>
update second
</a>
</p>
<p>
<a
class="update-both-link"
href="/island_instance_multiple/injected"
fh-partial="/island_instance_multiple/partial_both"
>
update both
</a>
</p>
<pre id="logs" />
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/island_instance_multiple",
},
});
});
@@ -0,0 +1,22 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import CounterB from "../../islands/CounterB.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-2">
<Fader>
<h1>Another page</h1>
<p class="status-2">updated content {Math.random()}</p>
<CounterB />
</Fader>
</Partial>
);
});
@@ -0,0 +1,31 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import CounterB from "../../islands/CounterB.tsx";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<>
<Partial name="slot-1">
<Fader>
<p class="status-1">updated content {Math.random()}</p>
<CounterA />
</Fader>
</Partial>
<Partial name="slot-2">
<Fader>
<h1>Another page</h1>
<p class="status-2">updated content {Math.random()}</p>
<CounterB />
</Fader>
</Partial>
</>
);
});
@@ -0,0 +1,44 @@
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import CounterB from "../../islands/CounterB.tsx";
import PassThrough from "../../islands/PassThrough.tsx";
export default function SlotDemo() {
return (
<div>
<Partial name="slot-1">
<PassThrough>
<div class="inner">
<p>server content</p>
<CounterA />
</div>
<hr />
<PassThrough>
<p>another pass through</p>
<CounterB />
</PassThrough>
</PassThrough>
</Partial>
<hr />
<p>
<a
class="update-link"
href="/island_instance_nested/injected"
fh-partial="/island_instance_nested/partial"
>
update
</a>
</p>
<p>
<a
class="replace-link"
href="/island_instance_nested/injected"
fh-partial="/island_instance_nested/replace"
>
replace
</a>
</p>
<pre id="logs" />
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/island_instance_nested",
},
});
});
@@ -0,0 +1,29 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import CounterB from "../../islands/CounterB.tsx";
import PassThrough from "../../islands/PassThrough.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<PassThrough>
<div class="inner">
<p class="status-a">updated server content</p>
<CounterA />
</div>
<hr />
<PassThrough>
<p class="status-b">another pass through</p>
<CounterB />
</PassThrough>
</PassThrough>
</Partial>
);
});
@@ -0,0 +1,17 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<CounterA />
</Partial>
);
});
@@ -0,0 +1,32 @@
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import PropIsland from "../../islands/PropIsland.tsx";
export default function PropsDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p class="status-initial">initial</p>
<PropIsland
boolean={true}
number={1}
obj={{ foo: 123 }}
strArr={["foo"]}
string="foo"
/>
</Fader>
</Partial>
<p>
<a
class="update-link"
href="/island_props/injected"
fh-partial="/island_props/partial"
>
Update
</a>
</p>
<pre id="logs" />
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/island_props",
},
});
});
@@ -0,0 +1,27 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import PropIsland from "../../islands/PropIsland.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status-updated">updated</p>
<PropIsland
boolean={false}
number={42}
obj={{ foo: 123456 }}
strArr={["foo", "bar"]}
string="foobar"
/>
</Fader>
</Partial>
);
});
@@ -0,0 +1,30 @@
import { Partial } from "$fresh/runtime.ts";
import { useSignal } from "@preact/signals";
import { Fader } from "../../islands/Fader.tsx";
import SignalProp from "../../islands/SignalProp.tsx";
export default function PropsDemo() {
const sig = useSignal(0);
return (
<div>
<Partial name="slot-1">
<Fader>
<p class="status-initial">initial</p>
<SignalProp
sig={sig}
/>
</Fader>
</Partial>
<p>
<a
class="update-link"
href="/island_props_signals/injected"
fh-partial="/island_props_signals/partial"
>
Update
</a>
</p>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/island_props_signals",
},
});
});
@@ -0,0 +1,23 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import SignalProp from "../../islands/SignalProp.tsx";
import { signal } from "@preact/signals";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
const sig = signal(0);
return (
<Partial name="slot-1">
<Fader>
<p class="status-update">update</p>
<SignalProp sig={sig} />
</Fader>
</Partial>
);
});
@@ -0,0 +1,29 @@
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Stateful from "../../islands/Stateful.tsx";
export default function SlotDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p>Initial content</p>
{[
<Stateful key="A" id="A" />,
<Stateful key="B" id="B" />,
<Stateful key="C" id="C" />,
]}
</Fader>
</Partial>
<p>
<a
class="swap-link"
href="/keys/injected"
fh-partial="/keys/swap"
>
swap
</a>
</p>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/keys",
},
});
});
@@ -0,0 +1,25 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Stateful from "../../islands/Stateful.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status-swap">swapped content</p>
{[
<Stateful key="C" id="C" />,
<Stateful key="B" id="B" />,
<Stateful key="A" id="A" />,
]}
</Fader>
</Partial>
);
});
@@ -0,0 +1,36 @@
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Stateful from "../../islands/Stateful.tsx";
import { Keyed } from "../../components/Keyed.tsx";
export default function SlotDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p>Initial content</p>
{[
<Keyed key="A">
<Stateful id="A" />
</Keyed>,
<Keyed key="B">
<Stateful id="B" />
</Keyed>,
<Keyed key="C">
<Stateful id="C" />
</Keyed>,
]}
</Fader>
</Partial>
<p>
<a
class="swap-link"
href="/keys_components/injected"
fh-partial="/keys_components/swap"
>
swap
</a>
</p>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/keys_components",
},
});
});
@@ -0,0 +1,32 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Stateful from "../../islands/Stateful.tsx";
import { Keyed } from "../../components/Keyed.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status-swap">swapped content</p>
{[
<Keyed key="C">
<Stateful id="C" />
</Keyed>,
<Keyed key="B">
<Stateful id="B" />
</Keyed>,
<Keyed key="A">
<Stateful id="A" />
</Keyed>,
]}
</Fader>
</Partial>
);
});
@@ -0,0 +1,37 @@
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Stateful from "../../islands/Stateful.tsx";
export default function SlotDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p>Initial content</p>
<ul>
{[
<li key="A" class="list-A">
<Stateful id="A" />
</li>,
<li key="B" class="list-B">
<Stateful id="B" />
</li>,
<li key="C" class="list-C">
<Stateful id="C" />
</li>,
]}
</ul>
</Fader>
</Partial>
<p>
<a
class="swap-link"
href="/keys_dom/injected"
fh-partial="/keys_dom/swap"
>
swap
</a>
</p>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/keys_dom",
},
});
});
@@ -0,0 +1,33 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Stateful from "../../islands/Stateful.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status-swap">swapped content</p>
<ul>
{[
<li key="C" class="list-C">
<Stateful id="C" />
</li>,
<li key="B" class="list-B">
<Stateful id="B" />
</li>,
<li key="A" class="list-A">
<Stateful id="A" />
</li>,
]}
</ul>
</Fader>
</Partial>
);
});
@@ -0,0 +1,36 @@
import { Partial } from "$fresh/runtime.ts";
import { useSignal } from "@preact/signals";
import Spinner from "../../islands/Spinner.tsx";
import PartialTrigger from "../../islands/PartialTrigger.tsx";
export default function SlotDemo() {
const sig = useSignal(false);
return (
<div>
<div class="output">
<Partial name="slot-1">
<p class="status">Default content</p>
<Spinner id="inner" show={sig} />
</Partial>
</div>
<Spinner id="outer" show={sig} />
<a
class="update-link"
href="/loading/injected"
fh-partial="/loading/update"
fh-loading={sig}
>
update
</a>
<br />
<PartialTrigger
class="trigger"
href="/loading/injected"
partial="/loading/update"
loading={sig}
>
partial trigger
</PartialTrigger>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/loading",
},
});
});
@@ -0,0 +1,20 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { delay } from "../../../deps.ts";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute(async (req, ctx) => {
// A bit of artificial delay to show the loader
await delay(200);
return (
<Partial name="slot-1">
<p class="status-updated">it works</p>
</Partial>
);
});
@@ -0,0 +1,25 @@
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export default function WarnDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p class="status-initial">Initial content</p>
<CounterA />
</Fader>
</Partial>
<p>
<a
class="update-link"
href="/missing_partial/injected"
fh-partial="/missing_partial/update"
>
update
</a>
</p>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/missing_partial",
},
});
});
@@ -0,0 +1,16 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="unknown-name">
<p class="status-append">append content</p>
</Partial>
);
});
@@ -0,0 +1,21 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
import Other from "../../islands/Other.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1" mode="append">
<Fader>
<p class="status-append">append content</p>
<Other />
</Fader>
</Partial>
);
});
@@ -0,0 +1,45 @@
import { Partial } from "$fresh/runtime.ts";
import CounterA from "../../islands/CounterA.tsx";
import { Fader } from "../../islands/Fader.tsx";
export default function ModeDemo() {
return (
<div>
<Partial name="slot-1">
<Fader>
<p class="status-initial">Initial content</p>
<CounterA />
</Fader>
</Partial>
<p>
<a
class="replace-link"
href="/mode/injected"
fh-partial="/mode/replace"
>
replace
</a>
</p>
<p>
<a
class="append-link"
href="/mode/injected"
fh-partial="/mode/append"
>
append
</a>
</p>
<p>
<a
class="prepend-link"
href="/mode/injected"
fh-partial="/mode/prepend"
>
prepend
</a>
</p>
<pre id="logs" />
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/mode",
},
});
});
@@ -0,0 +1,20 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1" mode="prepend">
<Fader>
<h1>prepend</h1>
<p class="status-prepend">prepend content</p>
</Fader>
</Partial>
);
});
@@ -0,0 +1,19 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Fader } from "../../islands/Fader.tsx";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<Fader>
<p class="status-replace">replace</p>
</Fader>
</Partial>
);
});
@@ -0,0 +1,20 @@
import { Partial } from "$fresh/runtime.ts";
export default function SlotDemo() {
return (
<div>
<div class="output">
<Partial name="slot-1">
<p>Default content</p>
</Partial>
</div>
<a
class="update-link"
href="/no_islands/injected"
fh-partial="/no_islands/update"
>
update
</a>
</div>
);
}
@@ -0,0 +1,10 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
export default defineRoute(() => {
return new Response("", {
status: 302,
headers: {
Location: "/no_islands",
},
});
});
@@ -0,0 +1,16 @@
import { defineRoute } from "$fresh/src/server/defines.ts";
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default defineRoute((req, ctx) => {
return (
<Partial name="slot-1">
<p class="status">it works</p>
</Partial>
);
});
@@ -0,0 +1,5 @@
import InvalidSlot from "../islands/InvalidSlot.tsx";
export default function SlotDemo() {
return <InvalidSlot />;
}
+8
View File
@@ -0,0 +1,8 @@
@keyframes fade {
0% {
background: white;
}
100% {
background: peachpuff;
}
}
+1 -1
View File
@@ -858,7 +858,7 @@ Deno.test("rendering custom _500.tsx page for default handlers", async (t) => {
Deno.test("renders error boundary", async () => {
await withPageName("./tests/fixture/main.ts", async (page, address) => {
await page.goto(`${address}/error_boundary`);
const text = await page.$eval("body", (el) => el.textContent);
const text = await page.$eval("p", (el) => el.textContent);
assertEquals(text, "it works");
});
});
+876
View File
@@ -0,0 +1,876 @@
import { assertEquals, assertMatch } from "$std/testing/asserts.ts";
import { Page } from "./deps.ts";
import {
assertNoPageComments,
assertNotSelector,
assertSelector,
assertTextMany,
parseHtml,
waitFor,
waitForText,
withFakeServe,
withPageName,
} from "./test_utils.ts";
async function assertLogs(page: Page, expected: string[]) {
await waitForText(page, "#logs", expected.join("\n") + "\n");
}
Deno.test("injects server content with no islands present", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/no_islands`);
await page.waitForSelector(".output");
await assertNoPageComments(page);
const href = await page.$eval(".update-link", (el) => el.href);
await page.click(".update-link");
await waitForText(page, "p", "it works");
assertEquals(href, await page.url());
await assertNoPageComments(page);
},
);
});
Deno.test(
"throws when Partial is instantiated inside an island",
async () => {
await withFakeServe(
"./tests/fixture_partials/main.ts",
async (server) => {
const html = await server.getHtml("/partial_slot_inside_island");
assertMatch(
html.querySelector("h1")!.textContent!,
/<Partial> components cannot be used inside islands/,
);
},
);
},
);
Deno.test("warns on missing partial", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const logs: string[] = [];
page.on("console", (msg) => logs.push(msg.text()));
const initialUrl = `${address}/missing_partial`;
await page.goto(initialUrl);
await page.waitForSelector(".status-initial");
await page.click(".update-link");
await waitFor(() =>
logs.find((line) => /^Partial.*not found/.test(line))
);
},
);
});
Deno.test("injects content with island and keeps island instance alive", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance`);
await page.waitForSelector(".output-a");
// Update island state
await page.click(".island button");
await waitForText(page, ".output-a", "1");
await assertLogs(page, ["mount Counter A", "update Counter A"]);
await assertNoPageComments(page);
const href = await page.$eval(".update-link", (el) => el.href);
await page.click(".update-link");
await waitForText(page, ".status", "updated content");
await assertLogs(page, ["mount Counter A", "update Counter A"]);
assertEquals(href, await page.url());
// Check that island value didn't change
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
},
);
});
Deno.test("unmounts island", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance`);
await page.waitForSelector(".output-a");
const href = await page.$eval(".remove-link", (el) => el.href);
await page.click(".remove-link");
await waitForText(page, ".status", "no islands");
await assertNoPageComments(page);
assertEquals(href, await page.url());
await assertLogs(page, ["mount Counter A", "unmount Counter A"]);
await page.click(".update-link");
await waitForText(page, ".status", "updated content");
await assertNoPageComments(page);
await assertLogs(page, [
"mount Counter A",
"unmount Counter A",
"mount Counter A",
]);
},
);
});
Deno.test("unmounts island on replace", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance`);
await page.waitForSelector(".output-a");
// Update island state
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
const href = await page.$eval(".replace-link", (el) => el.href);
await page.click(".replace-link");
await waitForText(page, ".status-replaced", "replaced content");
await assertNoPageComments(page);
assertEquals(href, await page.url());
await assertLogs(page, [
"mount Counter A",
"update Counter A",
"unmount Counter A",
"mount Other",
]);
await page.click(".island-other button");
await waitForText(page, ".output-other", "1");
await assertNoPageComments(page);
await assertLogs(page, [
"mount Counter A",
"update Counter A",
"unmount Counter A",
"mount Other",
"update Other",
]);
},
);
});
Deno.test("updates only one partial of many", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance_multiple`);
await page.waitForSelector(".output-a");
// Update island state
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
await assertLogs(page, [
"mount Counter A",
"mount Counter B",
"update Counter A",
"update Counter B",
]);
const href = await page.$eval(".update-second-link", (el) => el.href);
await page.click(".update-second-link");
await page.waitForSelector(".status-2");
await assertNoPageComments(page);
assertEquals(href, await page.url());
// Check that island value didn't change
await waitForText(page, ".output-a", "1");
await waitForText(page, ".output-b", "1");
},
);
});
Deno.test("updates many partials at once", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance_multiple`);
await page.waitForSelector(".output-a");
// Update island state
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
await assertLogs(page, [
"mount Counter A",
"mount Counter B",
"update Counter A",
"update Counter B",
]);
const href = await page.$eval(".update-both-link", (el) => el.href);
await page.click(".update-both-link");
await page.waitForSelector(".status-1");
await page.waitForSelector(".status-2");
await assertNoPageComments(page);
assertEquals(href, await page.url());
// Check that island value didn't change
await waitForText(page, ".output-a", "1");
await waitForText(page, ".output-b", "1");
},
);
});
Deno.test("keeps nested island state", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance_nested`);
await page.waitForSelector(".output-a");
// Update island state
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
await assertLogs(page, [
"mount Counter A",
"mount Counter B",
"mount PassThrough",
"mount PassThrough",
"update Counter A",
"update Counter B",
]);
const href = await page.$eval(".update-link", (el) => el.href);
await page.click(".update-link");
await page.waitForSelector(".status-a");
await page.waitForSelector(".status-b");
await assertNoPageComments(page);
assertEquals(href, await page.url());
// Check that island value didn't change
await waitForText(page, ".output-a", "1");
await waitForText(page, ".output-b", "1");
},
);
});
Deno.test("replace island if parent type changes", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_instance_nested`);
await page.waitForSelector(".output-a");
// Update island state
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
await assertLogs(page, [
"mount Counter A",
"mount Counter B",
"mount PassThrough",
"mount PassThrough",
"update Counter A",
"update Counter B",
]);
const href = await page.$eval(".replace-link", (el) => el.href);
await page.click(".replace-link");
await page.waitForSelector(".output-a");
await assertNoPageComments(page);
assertEquals(href, await page.url());
// Check that island value was destroyed since we replaced it
await waitForText(page, ".output-a", "0");
await assertLogs(page, [
"mount Counter A",
"mount Counter B",
"mount PassThrough",
"mount PassThrough",
"update Counter A",
"update Counter B",
"unmount PassThrough",
"unmount Counter A",
"unmount PassThrough",
"unmount Counter B",
"mount Counter A",
]);
},
);
});
Deno.test("serializes props", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_props`);
await page.waitForSelector(".pre-props");
let text = JSON.parse(
await page.$eval(".pre-props", (el) => el.textContent!),
);
assertEquals(
text,
{
number: 1,
string: "foo",
boolean: true,
null: null,
object: { foo: 123 },
strArr: ["foo"],
},
);
await page.click(".update-link");
await page.waitForSelector(".status-updated");
text = JSON.parse(
await page.$eval(".pre-props", (el) => el.textContent!),
);
assertEquals(
text,
{
number: 42,
string: "foobar",
boolean: false,
null: null,
object: { foo: 123456 },
strArr: ["foo", "bar"],
},
);
await assertNoPageComments(page);
},
);
});
Deno.test("serializes signals", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/island_props_signals`);
await page.waitForSelector(".island");
await page.click("button");
await waitForText(page, ".output", "1");
await assertNoPageComments(page);
await page.click(".update-link");
await page.waitForSelector(".status-update");
await assertNoPageComments(page);
// Currently, signal props are reset. This may change in
// the future
await waitForText(page, ".output", "0");
},
);
});
Deno.test("reconciles keyed islands", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/keys`);
await page.waitForSelector(".island");
await page.click(".btn-A");
await waitForText(page, ".output-A", "1");
await assertNoPageComments(page);
await page.click(".btn-B");
await waitForText(page, ".output-B", "1");
await page.click(".btn-B");
await waitForText(page, ".output-B", "2");
await assertNoPageComments(page);
await page.click(".btn-C");
await waitForText(page, ".output-C", "1");
await page.click(".btn-C");
await waitForText(page, ".output-C", "2");
await page.click(".btn-C");
await waitForText(page, ".output-C", "3");
await assertNoPageComments(page);
await page.click(".swap-link");
await page.waitForSelector(".status-swap");
await assertNoPageComments(page);
// Check that result is stable
await waitForText(page, ".output-A", "1");
await waitForText(page, ".output-B", "2");
await waitForText(page, ".output-C", "3");
},
);
});
Deno.test("reconciles keyed DOM nodes", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/keys_dom`);
await page.waitForSelector(".island");
await page.click(".btn-A");
await waitForText(page, ".output-A", "1");
await assertNoPageComments(page);
await page.click(".btn-B");
await waitForText(page, ".output-B", "1");
await page.click(".btn-B");
await waitForText(page, ".output-B", "2");
await assertNoPageComments(page);
await page.click(".btn-C");
await waitForText(page, ".output-C", "1");
await page.click(".btn-C");
await waitForText(page, ".output-C", "2");
await page.click(".btn-C");
await waitForText(page, ".output-C", "3");
await assertNoPageComments(page);
await page.click(".swap-link");
await page.waitForSelector(".status-swap");
await assertNoPageComments(page);
// Check that result is stable
await waitForText(page, ".output-A", "1");
await waitForText(page, ".output-B", "2");
await waitForText(page, ".output-C", "3");
// Check that no element has `data-fresh-key` attribute
const doc = parseHtml(await page.content());
assertNotSelector(doc, "[data-fresh-key]");
},
);
});
Deno.test("reconciles keyed non island components", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/keys_components`);
await page.waitForSelector(".island");
await page.click(".btn-A");
await waitForText(page, ".output-A", "1");
await assertNoPageComments(page);
await page.click(".btn-B");
await waitForText(page, ".output-B", "1");
await page.click(".btn-B");
await waitForText(page, ".output-B", "2");
await assertNoPageComments(page);
await page.click(".btn-C");
await waitForText(page, ".output-C", "1");
await page.click(".btn-C");
await waitForText(page, ".output-C", "2");
await page.click(".btn-C");
await waitForText(page, ".output-C", "3");
await assertNoPageComments(page);
await page.click(".swap-link");
await page.waitForSelector(".status-swap");
await assertNoPageComments(page);
// Check that result is stable
await waitForText(page, ".output-A", "1");
await waitForText(page, ".output-B", "2");
await waitForText(page, ".output-C", "3");
// Check that no element has `data-fresh-key` attribute
const doc = parseHtml(await page.content());
assertNotSelector(doc, "[data-fresh-key]");
},
);
});
Deno.test("partial injection mode", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
await page.goto(`${address}/mode`);
await page.waitForSelector(".island");
await page.click("button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
// Append
await page.click(".append-link");
await page.waitForSelector(".status-append");
// Check that old content is still there
await page.waitForSelector(".status-initial");
await assertNoPageComments(page);
// Check that newly inserted island is interactive
await page.click(".island-other button");
await waitForText(page, ".output-other", "1");
await assertNoPageComments(page);
// Prepend
await page.click(".prepend-link");
await page.waitForSelector(".status-prepend");
// Check that old content is still there
await page.waitForSelector(".status-append");
await page.waitForSelector(".status-initial");
await assertNoPageComments(page);
// Replace
await page.click(".replace-link");
await page.waitForSelector(".status-replace");
await assertNoPageComments(page);
const doc = parseHtml(await page.content());
assertNotSelector(doc, ".status-append");
assertNotSelector(doc, ".status-prepend");
},
);
});
Deno.test("partial navigation", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const initialUrl = `${address}/mode`;
await page.goto(initialUrl);
await page.waitForSelector(".island");
await page.click(".append-link");
await page.waitForSelector(".status-append");
await assertNoPageComments(page);
await page.click(".island-other button");
await waitForText(page, ".output-other", "1");
await assertNoPageComments(page);
const url = page.url();
// Click link again
await page.click(".append-link");
await page.waitForFunction(() =>
document.querySelectorAll(".status-append").length > 1
);
assertEquals(page.url(), url);
await assertNoPageComments(page);
// Go back
await page.goBack();
await page.waitForFunction(() =>
document.querySelectorAll(".island").length === 1
);
assertEquals(page.url(), initialUrl);
await waitForText(page, "pre", "mount Counter A\n");
await assertNoPageComments(page);
},
);
});
Deno.test("non-partial client navigation", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const initialUrl = `${address}/client_nav`;
await page.goto(initialUrl);
await page.waitForSelector(".island");
// Add marker to check if the page reloaded or not
await page.evaluate(() => {
const marker = document.createElement("fresh-nav-marker");
document.body.appendChild(marker);
});
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
// Go to page B
await page.click(".page-b-link");
await page.waitForSelector(".island-b");
await assertNoPageComments(page);
let doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertSelector(doc, "fresh-nav-marker");
assertEquals(page.url(), `${address}/client_nav/page-b`);
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
// Go to page C
await page.click(".page-c-link");
await page.waitForSelector(".page-c-text");
await assertNoPageComments(page);
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertNotSelector(doc, ".island-b");
assertSelector(doc, "fresh-nav-marker");
assertEquals(page.url(), `${address}/client_nav/page-c`);
// Go back to B
await page.goBack();
await page.waitForSelector(".island-b");
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertSelector(doc, "fresh-nav-marker");
assertNotSelector(doc, ".page-c-text");
// Non-shared state is reset
assertTextMany(doc, ".output-b", ["0"]);
// Check that island is interactive
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
// Go back to A
await page.goBack();
await page.waitForSelector(".island-a");
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-b");
assertNotSelector(doc, ".page-c-text");
assertSelector(doc, "fresh-nav-marker");
// Non-shared state is reset
assertTextMany(doc, ".output-a", ["0"]);
// Check that island is interactive
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
// Go forward to B
await page.goForward();
await page.waitForSelector(".island-b");
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertNotSelector(doc, ".page-c-text");
assertSelector(doc, "fresh-nav-marker");
// Non-shared state is reset
assertTextMany(doc, ".output-b", ["0"]);
// Check that island is interactive
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
},
);
});
Deno.test("allow opting out of client navigation", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const initialUrl = `${address}/client_nav_opt_out`;
await page.goto(initialUrl);
await page.waitForSelector(".island");
async function addMarker() {
await page.evaluate(() => {
const marker = document.createElement("fresh-nav-marker");
document.body.appendChild(marker);
});
}
// Add marker to check if the page reloaded or not
await addMarker();
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
// Go to page B
await page.click(".page-b-link");
await page.waitForSelector(".island-b");
await assertNoPageComments(page);
let doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertNotSelector(doc, "fresh-nav-marker");
assertEquals(page.url(), `${address}/client_nav_opt_out/page-b`);
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
// Add marker to check if the page reloaded or not
await addMarker();
// Go to page C
await page.click(".page-c-link");
await page.waitForSelector(".page-c-text");
await assertNoPageComments(page);
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertNotSelector(doc, ".island-b");
assertNotSelector(doc, "fresh-nav-marker");
assertEquals(page.url(), `${address}/client_nav_opt_out/page-c`);
// Add marker to check if the page reloaded or not
await addMarker();
// Go back to B
await page.goBack();
await page.waitForSelector(".island-b");
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertNotSelector(doc, "fresh-nav-marker");
assertNotSelector(doc, ".page-c-text");
// Non-shared state is reset
assertTextMany(doc, ".output-b", ["0"]);
// Check that island is interactive
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
await assertNoPageComments(page);
// Go back to A
await page.goBack();
await page.waitForSelector(".island-a");
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-b");
assertNotSelector(doc, ".page-c-text");
assertNotSelector(doc, "fresh-nav-marker");
// Non-shared state is reset
assertTextMany(doc, ".output-a", ["0"]);
// Check that island is interactive
await page.click(".island-a button");
await waitForText(page, ".output-a", "1");
await assertNoPageComments(page);
// Go forward to B
await page.goForward();
await page.waitForSelector(".island-b");
doc = parseHtml(await page.content());
assertNotSelector(doc, ".island-a");
assertNotSelector(doc, ".page-c-text");
assertNotSelector(doc, "fresh-nav-marker");
// Non-shared state is reset
assertTextMany(doc, ".output-b", ["0"]);
// Check that island is interactive
await page.click(".island-b button");
await waitForText(page, ".output-b", "1");
},
);
});
Deno.test("shows loading indicator if trigger outside island", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const initialUrl = `${address}/loading`;
await page.goto(initialUrl);
await page.waitForSelector(".status");
let doc = parseHtml(await page.content());
assertNotSelector(doc, ".spinner");
await Promise.all([
page.waitForSelector(".spinner-inner"),
page.waitForSelector(".spinner-outer"),
page.click(".update-link"),
]);
await page.waitForSelector(".status-updated");
await assertNoPageComments(page);
doc = parseHtml(await page.content());
assertNotSelector(doc, ".spinner");
},
);
});
Deno.test("shows loading indicator if trigger inside island", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const initialUrl = `${address}/loading`;
await page.goto(initialUrl);
await page.waitForSelector(".status");
let doc = parseHtml(await page.content());
assertNotSelector(doc, ".spinner");
await Promise.all([
page.waitForSelector(".spinner-inner"),
page.waitForSelector(".spinner-outer"),
page.click(".trigger"),
]);
await page.waitForSelector(".status-updated");
await assertNoPageComments(page);
doc = parseHtml(await page.content());
assertNotSelector(doc, ".spinner");
},
);
});
Deno.test("submit form", async () => {
await withPageName(
"./tests/fixture_partials/main.ts",
async (page, address) => {
const initialUrl = `${address}/form`;
await page.goto(initialUrl);
await page.waitForSelector(".status");
await page.click(".submit");
await page.waitForSelector(".status-updated");
await assertNoPageComments(page);
},
);
});
// TODO Head merging
// TODO Active links
// TODO Children Keys
// TODO Leftover comments
// TODO HTML validation
+1 -1
View File
@@ -127,7 +127,7 @@ Deno.test({
await page.goto(`${address}/twind`);
await page.waitForSelector("h1");
const text = await page.$eval("body", (el) => el.textContent);
const text = await page.$eval("h1", (el) => el.textContent);
assertEquals(text, "it works");
// Check that CSS was applied accordingly
+41 -8
View File
@@ -363,14 +363,25 @@ export async function waitForText(
text: string,
) {
await page.waitForSelector(selector);
await page.waitForFunction(
(sel, value) => {
return document.querySelector(sel)!.textContent === value;
},
{ timeout: 2000 },
selector,
String(text),
);
try {
await page.waitForFunction(
(sel, value) => {
return document.querySelector(sel)!.textContent === value;
},
{ timeout: 2000 },
selector,
String(text),
);
} catch (err) {
const body = await page.content();
// deno-lint-ignore no-explicit-any
const pretty = prettyDom(parseHtml(body) as any);
console.log(
`Text "${text}" not found on selector "${selector}" in html:\n\n${pretty}`,
);
throw err;
}
}
export async function waitForStyle(
@@ -486,6 +497,28 @@ export function getStdOutput(
return { stdout, stderr };
}
export async function waitFor(
fn: () => Promise<unknown> | unknown,
): Promise<void> {
let now = Date.now();
const limit = now + 2000;
while (now < limit) {
try {
if (await fn()) return;
} catch (err) {
if (now > limit) {
throw err;
}
} finally {
await delay(100);
now = Date.now();
}
}
throw new Error(`Timed out`);
}
function walk(doc: Document, node: HTMLElement): string | null {
for (let i = 0; i < node.childNodes.length; i++) {
const child = node.childNodes[i];