@sigil-dev/compiler 0.9.5 → 0.9.7

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/jsx.test.ts CHANGED
@@ -16,17 +16,22 @@ describe("JSX", () => {
16
16
 
17
17
  test("static string attribute", () => {
18
18
  const result = transform(`const el = <div class="app" />;`);
19
- expect(result).toContain('_el0.className = "app"');
19
+ expect(result).toContain('setProp(_el0, "className", "app")');
20
20
  });
21
21
 
22
22
  test("attribute without mapping", () => {
23
23
  const result = transform(`const el = <input type="text" />;`);
24
- expect(result).toContain('_el0.type = "text"');
24
+ expect(result).toContain('setProp(_el0, "type", "text")');
25
+ });
26
+
27
+ test("bare boolean attribute goes through setProp", () => {
28
+ const result = transform(`const el = <input disabled />;`);
29
+ expect(result).toContain('setProp(_el0, "disabled", true)');
25
30
  });
26
31
 
27
32
  test("expression attribute", () => {
28
33
  const result = transform(`const el = <div id={myId} />;`);
29
- expect(result).toContain("_el0.id = myId");
34
+ expect(result).toContain('setProp(_el0, "id", myId)');
30
35
  });
31
36
 
32
37
  describe("Reactivity", () => {
@@ -36,7 +41,7 @@ describe("JSX", () => {
36
41
  const el = <div id={count} />;
37
42
  `);
38
43
  expect(result).toContain("createEffect(");
39
- expect(result).toContain("_el0.id = count()");
44
+ expect(result).toContain('setProp(_el0, "id", count())');
40
45
  });
41
46
 
42
47
  test("static expression attribute does not wrap in createEffect", () => {
@@ -45,7 +50,7 @@ describe("JSX", () => {
45
50
  const el = <div id={myId} />;
46
51
  `);
47
52
  expect(result).not.toContain("createEffect(");
48
- expect(result).toContain("_el0.id = myId");
53
+ expect(result).toContain('setProp(_el0, "id", myId)');
49
54
  });
50
55
 
51
56
  test("style object compiles to per-property assignment", () => {
@@ -103,8 +103,8 @@ describe("Scoped CSS", () => {
103
103
 
104
104
  test("classList.add after createElement and attributes", () => {
105
105
  const result = transform(`<div class="foo" />`, "abc12345");
106
- // className = "foo" should come before classList.add
107
- const classNameIdx = result.indexOf('.className = "foo"');
106
+ // setProp(el, "className", "foo") should come before classList.add
107
+ const classNameIdx = result.indexOf('setProp(');
108
108
  const classListIdx = result.indexOf('classList.add("abc12345")');
109
109
  expect(classNameIdx).toBeGreaterThan(0);
110
110
  expect(classListIdx).toBeGreaterThan(classNameIdx);
@@ -0,0 +1,56 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { transformSSR } from "./helpers/transform.ts";
6
+
7
+ // Render through the real SSR output: write the compiled module to disk and
8
+ // import it, so the assertions are on the HTML a browser would receive.
9
+ const dir = mkdtempSync(join(tmpdir(), "sigil-ssr-bool-"));
10
+ let n = 0;
11
+ async function render(src: string, ...args: unknown[]): Promise<string> {
12
+ const file = join(dir, `m${n++}.js`);
13
+ writeFileSync(file, transformSSR(src));
14
+ const mod = await import(file);
15
+ return String(mod.default(...args));
16
+ }
17
+
18
+ describe("SSR boolean attributes", () => {
19
+ const box = `export default (on) => <input type="checkbox" checked={on} />;`;
20
+
21
+ test("true renders the bare attribute", async () => {
22
+ expect(await render(box, true)).toBe(`<input type="checkbox" checked>`);
23
+ });
24
+
25
+ test("false, null and undefined omit it", async () => {
26
+ // checked="" would mean checked: every box ticked, every option selected
27
+ for (const v of [false, null, undefined]) {
28
+ expect(await render(box, v)).toBe(`<input type="checkbox">`);
29
+ }
30
+ });
31
+
32
+ test("selected, disabled and friends follow the same rule", async () => {
33
+ const src = `export default (a, b) => <select disabled={a}><option selected={b}>x</option></select>;`;
34
+ expect(await render(src, false, true)).toBe(
35
+ `<select><option selected>x</option></select>`,
36
+ );
37
+ expect(await render(src, true, false)).toBe(
38
+ `<select disabled><option>x</option></select>`,
39
+ );
40
+ });
41
+
42
+ test("a string value is kept, escaped", async () => {
43
+ expect(
44
+ await render(`export default (v) => <div hidden={v} />;`, "until-found"),
45
+ ).toBe(`<div hidden="until-found"></div>`);
46
+ expect(
47
+ await render(`export default (v) => <div hidden={v} />;`, `"><x`),
48
+ ).toBe(`<div hidden="&quot;&gt;&lt;x"></div>`);
49
+ });
50
+
51
+ test("literal attributes are untouched", async () => {
52
+ expect(
53
+ await render(`export default () => <input checked="checked" />;`),
54
+ ).toBe(`<input checked="checked">`);
55
+ });
56
+ });
@@ -1,13 +1,13 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { transformSSR } from "./helpers/transform.ts";
3
-
4
- describe("SSR style objects", () => {
5
- test("style={{...}} serializes to CSS string, not [object Object]", () => {
6
- const out = transformSSR(
7
- `export function Card() { return <div style={{ display: "grid", marginTop: 12 }}>x</div>; }`,
8
- );
9
- // the __e helper must carry the object branch
10
- expect(out).toContain("constructor === Object");
11
- expect(out).toContain("UNITLESS");
12
- });
13
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformSSR } from "./helpers/transform.ts";
3
+
4
+ describe("SSR style objects", () => {
5
+ test("style={{...}} serializes to CSS string, not [object Object]", () => {
6
+ const out = transformSSR(
7
+ `export function Card() { return <div style={{ display: "grid", marginTop: 12 }}>x</div>; }`,
8
+ );
9
+ // the __e helper must carry the object branch
10
+ expect(out).toContain("constructor === Object");
11
+ expect(out).toContain("UNITLESS");
12
+ });
13
+ });