@sigil-dev/compiler 0.9.21 → 0.9.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/ssr.test.ts CHANGED
@@ -1,130 +1,130 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { transformSSR } from "./helpers/transform.ts";
3
-
4
- describe("SSR", () => {
5
- describe("$state", () => {
6
- test("becomes plain let", () => {
7
- const result = transformSSR(`let count = $state(0);`);
8
- expect(result).toContain("let count = 0");
9
- expect(result).not.toContain("createSignal");
10
- });
11
-
12
- test("mutations stay as mutations", () => {
13
- const result = transformSSR(`let count = $state(0); count++;`);
14
- expect(result).toContain("let count = 0");
15
- expect(result).toContain("count++");
16
- });
17
- });
18
-
19
- describe("$derived", () => {
20
- test("becomes arrow function", () => {
21
- const result = transformSSR(
22
- `let count = $state(0); let doubled = $derived(count * 2);`,
23
- );
24
- expect(result).toContain("const doubled = () => count * 2");
25
- });
26
- });
27
-
28
- describe("$effect", () => {
29
- test("is dropped entirely", () => {
30
- const result = transformSSR(`$effect(() => { console.log("hi"); });`);
31
- expect(result).not.toContain("createEffect");
32
- expect(result).not.toContain("console.log");
33
- });
34
- });
35
-
36
- describe("JSX", () => {
37
- test("static element", () => {
38
- const result = transformSSR(`const el = <div />;`);
39
- expect(result).toContain("<div>");
40
- expect(result).toContain("</div>");
41
- });
42
-
43
- test("static element with class", () => {
44
- const result = transformSSR(`const el = <div class="app" />;`);
45
- expect(result).toContain('class="app"');
46
- expect(result).not.toContain('className="');
47
- });
48
-
49
- test("void element self closes", () => {
50
- const result = transformSSR(`const el = <input type="text" />;`);
51
- expect(result).toContain("<input");
52
- expect(result).not.toContain("</input>");
53
- });
54
-
55
- test("static text child", () => {
56
- const result = transformSSR(`const el = <div>hello</div>;`);
57
- expect(result).toContain("<div>");
58
- expect(result).toContain("hello");
59
- expect(result).toContain("</div>");
60
- });
61
-
62
- test("dynamic child is escaped", () => {
63
- const result = transformSSR(`
64
- let count = $state(0);
65
- const el = <div>{count}</div>;
66
- `);
67
- expect(result).toContain("__e(");
68
- expect(result).toContain("</div>");
69
- });
70
-
71
- test("event handlers dropped", () => {
72
- const result = transformSSR(
73
- `const el = <button onClick={() => {}}>click</button>;`,
74
- );
75
- expect(result).not.toContain("addEventListener");
76
- expect(result).not.toContain("onClick");
77
- });
78
-
79
- test("nested elements", () => {
80
- const result = transformSSR(`const el = <div><span>hi</span></div>;`);
81
- expect(result).toContain("<div>");
82
- expect(result).toContain("<span>");
83
- expect(result).toContain("hi");
84
- expect(result).toContain("</span>");
85
- expect(result).toContain("</div>");
86
- });
87
-
88
- test("component called as function returning string", () => {
89
- const result = transformSSR(`const el = <Card title="hi" />;`);
90
- expect(result).toContain("Card(");
91
- expect(result).toContain('title: "hi"');
92
- expect(result).not.toContain("createElement");
93
- });
94
-
95
- test("component children passed as string", () => {
96
- const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
97
- expect(result).toContain("Card(");
98
- expect(result).toContain("children:");
99
- expect(result).toContain("<p>");
100
- });
101
-
102
- test("runtime import is escape only", () => {
103
- const result = transformSSR(
104
- `const name = $state("John"); const el = <div>{name}</div>;`,
105
- );
106
- expect(result).toContain("__e(");
107
- expect(result).not.toContain("createSignal");
108
- expect(result).not.toContain("createEffect");
109
- });
110
-
111
- test("innerHTML passes through unsanitized", () => {
112
- const result = transformSSR(
113
- `const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
114
- );
115
- expect(result).toContain("html");
116
- // innerHTML wraps in __h() for SafeHtml, not __e() for escaping
117
- expect(result).toContain("__h(");
118
- expect(result).not.toMatch(/__e\([^)]*html/);
119
- });
120
-
121
- test("keyed list converts key to data-key", () => {
122
- const result = transformSSR(
123
- `let items = $state([{id: 1, text: "A"}]);
124
- const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
125
- );
126
- expect(result).toContain("data-key");
127
- expect(result).not.toContain('"key"');
128
- });
129
- });
130
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformSSR } from "./helpers/transform.ts";
3
+
4
+ describe("SSR", () => {
5
+ describe("$state", () => {
6
+ test("becomes plain let", () => {
7
+ const result = transformSSR(`let count = $state(0);`);
8
+ expect(result).toContain("let count = 0");
9
+ expect(result).not.toContain("createSignal");
10
+ });
11
+
12
+ test("mutations stay as mutations", () => {
13
+ const result = transformSSR(`let count = $state(0); count++;`);
14
+ expect(result).toContain("let count = 0");
15
+ expect(result).toContain("count++");
16
+ });
17
+ });
18
+
19
+ describe("$derived", () => {
20
+ test("becomes arrow function", () => {
21
+ const result = transformSSR(
22
+ `let count = $state(0); let doubled = $derived(count * 2);`,
23
+ );
24
+ expect(result).toContain("const doubled = () => count * 2");
25
+ });
26
+ });
27
+
28
+ describe("$effect", () => {
29
+ test("is dropped entirely", () => {
30
+ const result = transformSSR(`$effect(() => { console.log("hi"); });`);
31
+ expect(result).not.toContain("createEffect");
32
+ expect(result).not.toContain("console.log");
33
+ });
34
+ });
35
+
36
+ describe("JSX", () => {
37
+ test("static element", () => {
38
+ const result = transformSSR(`const el = <div />;`);
39
+ expect(result).toContain("<div>");
40
+ expect(result).toContain("</div>");
41
+ });
42
+
43
+ test("static element with class", () => {
44
+ const result = transformSSR(`const el = <div class="app" />;`);
45
+ expect(result).toContain('class="app"');
46
+ expect(result).not.toContain('className="');
47
+ });
48
+
49
+ test("void element self closes", () => {
50
+ const result = transformSSR(`const el = <input type="text" />;`);
51
+ expect(result).toContain("<input");
52
+ expect(result).not.toContain("</input>");
53
+ });
54
+
55
+ test("static text child", () => {
56
+ const result = transformSSR(`const el = <div>hello</div>;`);
57
+ expect(result).toContain("<div>");
58
+ expect(result).toContain("hello");
59
+ expect(result).toContain("</div>");
60
+ });
61
+
62
+ test("dynamic child is escaped", () => {
63
+ const result = transformSSR(`
64
+ let count = $state(0);
65
+ const el = <div>{count}</div>;
66
+ `);
67
+ expect(result).toContain("__e(");
68
+ expect(result).toContain("</div>");
69
+ });
70
+
71
+ test("event handlers dropped", () => {
72
+ const result = transformSSR(
73
+ `const el = <button onClick={() => {}}>click</button>;`,
74
+ );
75
+ expect(result).not.toContain("addEventListener");
76
+ expect(result).not.toContain("onClick");
77
+ });
78
+
79
+ test("nested elements", () => {
80
+ const result = transformSSR(`const el = <div><span>hi</span></div>;`);
81
+ expect(result).toContain("<div>");
82
+ expect(result).toContain("<span>");
83
+ expect(result).toContain("hi");
84
+ expect(result).toContain("</span>");
85
+ expect(result).toContain("</div>");
86
+ });
87
+
88
+ test("component called as function returning string", () => {
89
+ const result = transformSSR(`const el = <Card title="hi" />;`);
90
+ expect(result).toContain("Card(");
91
+ expect(result).toContain('title: "hi"');
92
+ expect(result).not.toContain("createElement");
93
+ });
94
+
95
+ test("component children passed as string", () => {
96
+ const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
97
+ expect(result).toContain("Card(");
98
+ expect(result).toContain("children:");
99
+ expect(result).toContain("<p>");
100
+ });
101
+
102
+ test("runtime import is escape only", () => {
103
+ const result = transformSSR(
104
+ `const name = $state("John"); const el = <div>{name}</div>;`,
105
+ );
106
+ expect(result).toContain("__e(");
107
+ expect(result).not.toContain("createSignal");
108
+ expect(result).not.toContain("createEffect");
109
+ });
110
+
111
+ test("innerHTML passes through unsanitized", () => {
112
+ const result = transformSSR(
113
+ `const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
114
+ );
115
+ expect(result).toContain("html");
116
+ // innerHTML wraps in __h() for SafeHtml, not __e() for escaping
117
+ expect(result).toContain("__h(");
118
+ expect(result).not.toMatch(/__e\([^)]*html/);
119
+ });
120
+
121
+ test("keyed list converts key to data-key", () => {
122
+ const result = transformSSR(
123
+ `let items = $state([{id: 1, text: "A"}]);
124
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
125
+ );
126
+ expect(result).toContain("data-key");
127
+ expect(result).not.toContain('"key"');
128
+ });
129
+ });
130
+ });
@@ -0,0 +1,123 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdirSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { transformSync } from "@babel/core";
6
+ import { clearAnalysisCache } from "../src/analysis/hydration.ts";
7
+ import sigilPlugin from "../src/babel/index.ts";
8
+
9
+ const dir = join(tmpdir(), `sigil-static-${Date.now()}`);
10
+
11
+ beforeAll(() => {
12
+ mkdirSync(dir, { recursive: true });
13
+ writeFileSync(
14
+ join(dir, "Counter.tsx"),
15
+ `export const Counter = () => { let n = $state(0); return <button onClick={() => n++}>{n}</button>; };`,
16
+ );
17
+ writeFileSync(
18
+ join(dir, "Pure.tsx"),
19
+ `export const Pure = (props) => <i>{props.label}</i>;`,
20
+ );
21
+ writeFileSync(
22
+ join(dir, "Wrap.tsx"),
23
+ `export const Wrap = (props) => <div>{props.children}</div>;`,
24
+ );
25
+ clearAnalysisCache();
26
+ });
27
+ afterAll(() => rmSync(dir, { recursive: true, force: true }));
28
+
29
+ function compile(body: string, mode?: "ssr" | "dom") {
30
+ const header = `
31
+ import { Counter } from "./Counter.tsx";
32
+ import { Pure } from "./Pure.tsx";
33
+ import { Wrap } from "./Wrap.tsx";
34
+ const Footer = () => <footer>© now</footer>;
35
+ `;
36
+ return (
37
+ transformSync(header + body, {
38
+ filename: join(dir, "Page.tsx"),
39
+ babelrc: false,
40
+ configFile: false,
41
+ parserOpts: {
42
+ // @ts-expect-error babel's option type does not list the tuple form
43
+ plugins: [["typescript", { isTSX: true }], "jsx"],
44
+ },
45
+ plugins: [[sigilPlugin, { mode }]],
46
+ })?.code ?? ""
47
+ );
48
+ }
49
+
50
+ describe("<X static />", () => {
51
+ test("holds for a static component, and the attribute never reaches it", () => {
52
+ const out = compile(
53
+ `export const A = () => <main><Footer static /></main>;`,
54
+ );
55
+ expect(out).not.toContain("static");
56
+ });
57
+
58
+ test("holds for an imported static component given a constant prop", () => {
59
+ expect(() =>
60
+ compile(`export const A = () => <Pure label="hi" static />;`),
61
+ ).not.toThrow();
62
+ });
63
+
64
+ test("fails for a component that hydrates, naming the reason and the line", () => {
65
+ const run = () => compile(`export const A = () => <Counter static />;`);
66
+ expect(run).toThrow(/<Counter static> is not static/);
67
+ expect(run).toThrow(/\$state/);
68
+ expect(run).toThrow(/line \d+/);
69
+ });
70
+
71
+ test("fails when this use passes state, even to a static component", () => {
72
+ expect(() =>
73
+ compile(
74
+ `let n = $state(0); export const A = () => <Pure label={n} static />;`,
75
+ ),
76
+ ).toThrow(/passes `label` from state `n`/);
77
+ });
78
+
79
+ test("fails when the enclosing component's own props flow in", () => {
80
+ expect(() =>
81
+ compile(
82
+ `export const A = (props) => <Pure label={props.title} static />;`,
83
+ ),
84
+ ).toThrow(/prop object `props`/);
85
+ });
86
+
87
+ test("a static wrapper cannot hide a dynamic child", () => {
88
+ expect(() =>
89
+ compile(`export const A = () => <Wrap static><Counter /></Wrap>;`),
90
+ ).toThrow(/<Counter> hydrates/);
91
+ expect(() =>
92
+ compile(`export const A = () => <Wrap static><p>text</p></Wrap>;`),
93
+ ).not.toThrow();
94
+ });
95
+
96
+ test("a handler written inside the use makes it dynamic", () => {
97
+ expect(() =>
98
+ compile(
99
+ `export const A = () => <Wrap static><button onClick={() => {}}>x</button></Wrap>;`,
100
+ ),
101
+ ).toThrow(/onClick handler/);
102
+ });
103
+
104
+ test("is for components, not HTML elements", () => {
105
+ expect(() => compile(`export const A = () => <div static />;`)).toThrow(
106
+ /applies to components/,
107
+ );
108
+ });
109
+
110
+ test("static={false} switches it off", () => {
111
+ const out = compile(`export const A = () => <Counter static={false} />;`);
112
+ expect(out).not.toContain("static");
113
+ });
114
+
115
+ test("the server build reaches the same verdict", () => {
116
+ expect(() =>
117
+ compile(`export const A = () => <Counter static />;`, "ssr"),
118
+ ).toThrow(/not static/);
119
+ expect(() =>
120
+ compile(`export const A = () => <Footer static />;`, "ssr"),
121
+ ).not.toThrow();
122
+ });
123
+ });
@@ -0,0 +1,292 @@
1
+ /**
2
+ * Skipping hydration of components the analysis proved static.
3
+ *
4
+ * Same three renders as render-parity: server HTML, that HTML after hydration,
5
+ * and a fresh client render with an empty pool (what navigation runs). The
6
+ * difference is what each one calls.
7
+ */
8
+ import { afterEach, describe, expect, test } from "bun:test";
9
+ import { transformSync } from "@babel/core";
10
+ import * as runtime from "../../runtime/index.ts";
11
+ import sigilPlugin from "../src/babel/index.ts";
12
+
13
+ type Mode = "ssr" | "hydrate" | "dom";
14
+
15
+ function compile(source: string, mode: Mode, dev = false): () => unknown {
16
+ const code = transformSync(source, {
17
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode, dev }]],
18
+ filename: "t.tsx",
19
+ })!.code!;
20
+ const body = code
21
+ .replace(/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g, "")
22
+ .replace(/^export\s+/gm, "");
23
+ // grimoire's Head renders nothing on the server; the analysis only needs the import
24
+ const grimoire = body.replace(
25
+ /import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/grimoire["'];?/g,
26
+ "",
27
+ );
28
+ return new Function(
29
+ "document",
30
+ "Head",
31
+ ...Object.keys(runtime),
32
+ `${grimoire};\nreturn App;`,
33
+ )(globalThis.document, () => "", ...Object.values(runtime));
34
+ }
35
+
36
+ const g = globalThis as { __footerCalls?: number };
37
+ const calls = () => g.__footerCalls ?? 0;
38
+ afterEach(() => {
39
+ g.__footerCalls = 0;
40
+ });
41
+
42
+ const noMarkers = (html: string) => html.replace(/<!--[^>]*-->/g, "");
43
+
44
+ const FOOTER = `
45
+ const Footer = () => {
46
+ globalThis.__footerCalls = (globalThis.__footerCalls ?? 0) + 1;
47
+ return <footer><p>© now</p></footer>;
48
+ };
49
+ const Counter = () => {
50
+ let n = $state(0);
51
+ return <button onClick={() => n++}>{n}</button>;
52
+ };
53
+ `;
54
+
55
+ function renderAll(source: string, dev = false) {
56
+ g.__footerCalls = 0;
57
+ const ssr = String(compile(source, "ssr", dev)());
58
+ const afterSsr = calls();
59
+
60
+ const host = document.createElement("div");
61
+ host.innerHTML = ssr;
62
+ const before = Array.from(host.querySelectorAll("footer"));
63
+ runtime.pushHydrationNodes(Array.from(host.childNodes));
64
+ compile(source, "hydrate", dev)();
65
+ runtime.popHydrationNodes();
66
+ const afterHydrate = calls();
67
+
68
+ const spa = document.createElement("div");
69
+ runtime.pushHydrationNodes([]);
70
+ runtime.insert(spa, compile(source, "hydrate", dev)());
71
+ runtime.popHydrationNodes();
72
+
73
+ return { ssr, host, spa, before, afterSsr, afterHydrate, afterSpa: calls() };
74
+ }
75
+
76
+ describe("a static use is fenced by the server and skipped by hydration", () => {
77
+ const source = `${FOOTER}
78
+ export const App = () => (
79
+ <main>
80
+ <Footer />
81
+ <Counter />
82
+ <p>after</p>
83
+ </main>
84
+ );`;
85
+
86
+ test("the server fences only the static use", () => {
87
+ const html = String(compile(source, "ssr")());
88
+ expect(html).toContain("<!--s--><footer>");
89
+ expect(html).toContain("</footer><!--/s-->");
90
+ expect(html.match(/<!--s-->/g)).toHaveLength(1);
91
+ // the dynamic sibling is not fenced
92
+ expect(html).not.toMatch(/<!--s--><button/);
93
+ });
94
+
95
+ test("hydration never calls the component; a client render still does", () => {
96
+ const r = renderAll(source);
97
+ expect(r.afterSsr).toBe(1);
98
+ expect(r.afterHydrate).toBe(1); // hydration added nothing
99
+ expect(r.afterSpa).toBe(2); // the empty-pool render ran it
100
+ });
101
+
102
+ test("the server's own nodes are left exactly as they were", () => {
103
+ const r = renderAll(source);
104
+ const after = r.host.querySelector("footer");
105
+ expect(after).toBe(r.before[0]!);
106
+ });
107
+
108
+ test("the dynamic sibling after it still hydrates, and the page equals the server HTML", () => {
109
+ const r = renderAll(source);
110
+ expect(noMarkers(r.host.innerHTML)).toBe(noMarkers(r.ssr));
111
+ expect(noMarkers(r.spa.innerHTML)).toBe(noMarkers(r.ssr));
112
+ const button = r.host.querySelector("button")!;
113
+ button.click();
114
+ return Promise.resolve(runtime.tick()).then(() => {
115
+ expect(button.textContent).toBe("1");
116
+ });
117
+ });
118
+ });
119
+
120
+ describe("several static uses among dynamic ones and text", () => {
121
+ const source = `${FOOTER}
122
+ export const App = () => (
123
+ <main>
124
+ hello <Footer /> middle <Counter /><Footer /> end
125
+ </main>
126
+ );`;
127
+
128
+ test("each is skipped, in order, and the text around them is claimed correctly", () => {
129
+ const r = renderAll(source);
130
+ expect(r.afterHydrate).toBe(r.afterSsr);
131
+ expect(noMarkers(r.host.innerHTML)).toBe(noMarkers(r.ssr));
132
+ expect(r.host.querySelectorAll("footer")).toHaveLength(2);
133
+ for (let i = 0; i < 2; i++) {
134
+ expect(r.host.querySelectorAll("footer")[i]).toBe(r.before[i]!);
135
+ }
136
+ });
137
+ });
138
+
139
+ describe("uses that are not skipped", () => {
140
+ test("a use inside a condition, a component's children or a fragment is left alone", () => {
141
+ const source = `${FOOTER}
142
+ const Box = (props) => <div>{props.children}</div>;
143
+ export const App = (props) => (
144
+ <main>
145
+ {props.show ? <Footer /> : null}
146
+ <Box><Footer /></Box>
147
+ </main>
148
+ );`;
149
+ const html = String(
150
+ (compile(source, "ssr") as (p: unknown) => unknown)({ show: true }),
151
+ );
152
+ expect(html).toContain("<footer>");
153
+ // <Box> with only static children is one static run; the Footer inside it
154
+ // and the one in the condition are not fenced on their own
155
+ expect(html.match(/<!--s-->/g)).toHaveLength(1);
156
+ expect(html).not.toContain("<!--s--><footer>");
157
+ const hydrate = transformSync(source, {
158
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode: "hydrate" }]],
159
+ filename: "t.tsx",
160
+ })!.code!;
161
+ expect(hydrate.match(/skipStatic\(/g)).toHaveLength(1);
162
+ });
163
+
164
+ test("a use given reactive props, a spread or a handler child is not static", () => {
165
+ const source = `${FOOTER}
166
+ const Tag = (props) => <i>{props.label}</i>;
167
+ export const App = () => {
168
+ let n = $state(0);
169
+ return (
170
+ <main>
171
+ <Tag label={n} />
172
+ <Tag {...{ label: "x" }} />
173
+ </main>
174
+ );
175
+ };`;
176
+ expect(String(compile(source, "ssr")())).not.toContain("<!--s-->");
177
+ });
178
+
179
+ test("static={false} forces a use to hydrate", () => {
180
+ const source = `${FOOTER}
181
+ export const App = () => <main><Footer static={false} /></main>;`;
182
+ expect(String(compile(source, "ssr")())).not.toContain("<!--s-->");
183
+ });
184
+
185
+ test("the dom build has no markers and no skipping", () => {
186
+ const source = `${FOOTER}
187
+ export const App = () => <main><Footer /></main>;`;
188
+ const out = transformSync(source, {
189
+ plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode: "dom" }]],
190
+ filename: "t.tsx",
191
+ })!.code!;
192
+ expect(out).not.toContain("skipStatic(");
193
+ });
194
+ });
195
+
196
+ describe("grimoire's Head does not make a component dynamic", () => {
197
+ test("a component that only sets the head and renders text is static", () => {
198
+ const source = `
199
+ import { Head } from "@sigil-dev/grimoire";
200
+ const Title = () => <section><Head><title>x</title></Head><p>hi</p></section>;
201
+ export const App = () => <main><Title /></main>;`;
202
+ expect(String(compile(source, "ssr")())).toContain("<!--s-->");
203
+ });
204
+ });
205
+
206
+ describe("probeStatic", () => {
207
+ test("passes a component that renders only HTML", () => {
208
+ expect(() =>
209
+ runtime.probeStatic("Plain", () => document.createElement("p")),
210
+ ).not.toThrow();
211
+ });
212
+
213
+ test("throws for an effect that tracks a signal", () => {
214
+ const count = runtime.createSignal(0);
215
+ expect(() =>
216
+ runtime.probeStatic("Live", () => {
217
+ runtime.createEffect(() => {
218
+ count();
219
+ });
220
+ }),
221
+ ).toThrow(/<Live> was compiled as static/);
222
+ });
223
+
224
+ test("does not throw for an effect that reads nothing reactive", () => {
225
+ expect(() =>
226
+ runtime.probeStatic("Prop", () => {
227
+ runtime.createEffect(() => {
228
+ void "constant";
229
+ });
230
+ }),
231
+ ).not.toThrow();
232
+ });
233
+
234
+ test("throws for an event listener", () => {
235
+ expect(() =>
236
+ runtime.probeStatic("Clicky", () => {
237
+ document.createElement("button").addEventListener("click", () => {});
238
+ }),
239
+ ).toThrow(/1 event listener/);
240
+ });
241
+
242
+ test("restores what it patched, even after throwing", () => {
243
+ const add = EventTarget.prototype.addEventListener;
244
+ try {
245
+ runtime.probeStatic("Clicky", () => {
246
+ document.createElement("button").addEventListener("click", () => {});
247
+ });
248
+ } catch {}
249
+ expect(EventTarget.prototype.addEventListener).toBe(add);
250
+ // effects created afterwards are no longer counted
251
+ const count = runtime.createSignal(0);
252
+ expect(() =>
253
+ runtime.probeStatic("After", () => document.createElement("i")),
254
+ ).not.toThrow();
255
+ runtime.createEffect(() => {
256
+ count();
257
+ });
258
+ });
259
+ });
260
+
261
+ describe("skipStatic", () => {
262
+ function pool(html: string) {
263
+ const host = document.createElement("div");
264
+ host.innerHTML = html;
265
+ return Array.from(host.childNodes);
266
+ }
267
+
268
+ test("cuts the fenced run out of the pool and returns its first node", () => {
269
+ const nodes = pool("<p>a</p><!--s--><i>x</i>text<!--/s--><p>b</p>");
270
+ const first = runtime.skipStatic(nodes);
271
+ expect((first as Element).tagName).toBe("I");
272
+ expect(nodes.map((n) => (n as Element).tagName ?? "#")).toEqual(["P", "P"]);
273
+ });
274
+
275
+ test("matches nested runs by depth", () => {
276
+ const nodes = pool(
277
+ "<!--s--><b><!--s--><i></i><!--/s--></b><!--/s--><u></u>",
278
+ );
279
+ runtime.skipStatic(nodes);
280
+ expect(nodes.map((n) => (n as Element).tagName)).toEqual(["U"]);
281
+ });
282
+
283
+ test("returns null, and leaves the pool alone, when there is nothing well formed to skip", () => {
284
+ const none = pool("<p>a</p>");
285
+ expect(runtime.skipStatic(none)).toBeNull();
286
+ expect(none).toHaveLength(1);
287
+ const open = pool("<!--s--><p>a</p>");
288
+ expect(runtime.skipStatic(open)).toBeNull();
289
+ expect(open).toHaveLength(2);
290
+ expect(runtime.skipStatic([])).toBeNull();
291
+ });
292
+ });