@luna_ui/luna 0.3.4 → 0.3.5
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/dist/cli.mjs +1264 -27
- package/dist/css/index.d.ts +194 -0
- package/dist/css/index.js +721 -0
- package/dist/css/runtime.d.ts +92 -0
- package/dist/css/runtime.js +179 -0
- package/dist/index.js +1 -1
- package/dist/jsx-dev-runtime.js +1 -1
- package/dist/jsx-runtime.js +1 -1
- package/dist/{src-BDdxGwvq.js → src-CHiGeWfy.js} +1 -1
- package/dist/vite-plugin.d.ts +122 -0
- package/dist/vite-plugin.js +1518 -0
- package/package.json +16 -2
- package/src/css/extract.ts +798 -0
- package/src/css/index.ts +10 -0
- package/src/css/inject.ts +205 -0
- package/src/css/inline.ts +182 -0
- package/src/css/minify.ts +70 -0
- package/src/css/optimizer.ts +6 -0
- package/src/css/runtime.ts +344 -0
- package/src/css-optimizer/README.md +353 -0
- package/src/css-optimizer/cooccurrence.ts +100 -0
- package/src/css-optimizer/core.ts +263 -0
- package/src/css-optimizer/extractors.ts +243 -0
- package/src/css-optimizer/hash.ts +54 -0
- package/src/css-optimizer/index.ts +129 -0
- package/src/css-optimizer/merge.ts +109 -0
- package/src/css-optimizer/moonbit-analyzer.ts +210 -0
- package/src/css-optimizer/parser.ts +120 -0
- package/src/css-optimizer/pattern.ts +171 -0
- package/src/css-optimizer/transformers.ts +301 -0
- package/src/css-optimizer/types.ts +128 -0
- package/src/event-utils.ts +227 -0
- package/src/index.ts +890 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +398 -0
- package/src/vite-plugin.ts +718 -0
- package/tests/__screenshots__/context.test.ts/Context-API-context-with-reactive-effects-context-value-accessible-in-effect-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-For-component--SolidJS-style--For-updates-when-signal-changes-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-Show-component--SolidJS-style--Show-accepts-children-as-function-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-Show-component--SolidJS-style--Show-toggles-visibility-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-createElement-createElement-with-dynamic-attribute-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-createElement-createElement-with-dynamic-style-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-createElementNs--SVG-support--createElementNs-with-dynamic-attribute-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-effect-with-DOM-effect-tracks-signal-changes-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach--list-rendering--forEach-handles-clear-to-empty-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach--list-rendering--forEach-handles-empty-array-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach--list-rendering--forEach-removes-items-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach--list-rendering--forEach-renders-initial-list-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach--list-rendering--forEach-updates-when-items-change-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach-with-SVG-elements-forEach-handles-empty-to-non-empty-transition-in-SVG-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach-with-SVG-elements-forEach-handles-reordering-in-SVG-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach-with-SVG-elements-forEach-updates-SVG-elements-when-signal-changes-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-forEach-with-SVG-elements-forEach-with-nested-SVG-groups-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-ref-callback--JSX-style--ref-callback-with-nested-elements-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-show--conditional-rendering--show-creates-a-node-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-show--conditional-rendering--show-with-false-condition-creates-placeholder-1.png +0 -0
- package/tests/__screenshots__/dom.test.ts/DOM-API-text-nodes-textDyn-creates-reactive-text-node-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Complex-nested-scenario-forEach-renders-correctly-without-show--initial-items--1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Complex-nested-scenario-forEach-with-context-renders-correctly-without-show-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Complex-nested-scenario-nested-components-with-context--forEach--and-show-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Complex-nested-scenario-show-and-forEach-inherit-context-from-Owner--fixed--1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Complex-nested-scenario-show-and-forEach-work-together--context-uses-default--1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Context---ForEach-integration-forEach-items-can-access-context-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-ForEach-with-reactive-updates-forEach-renders-initial-list-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-ForEach-with-reactive-updates-forEach-updates-when-signal-changes-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-ForEach-with-reactive-updates-forEach-with-object-items-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Show--conditional-rendering--show-hides-when-condition-is-false-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Show--conditional-rendering--show-renders-when-condition-is-true-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Show--conditional-rendering--show-toggles-from-false-to-true-1.png +0 -0
- package/tests/__screenshots__/integration.test.ts/Integration--Nested-Components-with-Context-Show--conditional-rendering--show-toggles-reactively-1.png +0 -0
- package/tests/__screenshots__/lifecycle.test.ts/onCleanup-in-Component-Body--Solid-js-style--event-listener-pattern--Solid-js-docs-example--1.png +0 -0
- package/tests/__screenshots__/lifecycle.test.ts/onCleanup-in-Component-Body--Solid-js-style--multiple-cleanups-in-component-body--LIFO-order--1.png +0 -0
- package/tests/__screenshots__/lifecycle.test.ts/onCleanup-in-Component-Body--Solid-js-style--onCleanup-in-component-body-runs-on-unmount-1.png +0 -0
- package/tests/__screenshots__/lifecycle.test.ts/onCleanup-in-Component-Body--Solid-js-style--onCleanup-works-with-For-loop-items--component-body-style--1.png +0 -0
- package/tests/__screenshots__/lifecycle.test.ts/onCleanup-in-Component-Body--Solid-js-style--timer-cleanup-pattern--Solid-js-style--1.png +0 -0
- package/tests/__screenshots__/lifecycle.test.ts/onCleanup-in-Effects-effect-cleanup-runs-before-re-run-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Bulk-Updates-large-list-update-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Bulk-Updates-nested-batch-operations-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Bulk-Updates-rapid-sequential-updates-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Conditional-Show-component---visible-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Conditional-show-hide-element---visible-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Fragments-fragment-with-list-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Fragments-nested-fragments-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Fragments-simple-fragment-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Reactive-Updates-conditional-toggle-updates-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Reactive-Updates-list-addition-updates-match-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Reactive-Updates-list-removal-updates-match-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/DOM-Rendering-Comparison---Reactive-Updates-text-updates-match-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Dynamic-Attributes-Comparison-dynamic-className-updates-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Dynamic-Attributes-Comparison-dynamic-style-updates-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Dynamic-Attributes-Comparison-multiple-dynamic-attributes-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Edge-Cases-deeply-nested-conditionals-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Edge-Cases-list-transitions-from-empty-to-populated-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Edge-Cases-list-transitions-from-populated-to-empty-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Effect-Cleanup-Comparison-nested-effects-cleanup-order-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Effect-Cleanup-Comparison-nested-effects-with-inner-signal-change-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Effect-Cleanup-Comparison-onCleanup-is-called-when-effect-re-runs-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Effect-Cleanup-Comparison-onCleanup-with-resource-simulation-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Fragment-Comparison-with-Preact-Fragment-with-multiple-children--no-wrapper--1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Fragment-Comparison-with-Preact-Fragment-with-no-children-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Fragment-Comparison-with-Preact-fragment-with-list-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Fragment-Comparison-with-Preact-nested-Fragments-work-correctly-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/List-Reordering-complex-reordering-with-additions-and-removals-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/List-Reordering-insert-in-middle-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/List-Reordering-remove-from-middle-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/List-Reordering-reverse-list-order-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/List-Reordering-shuffle-list-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Luna-Conditional-Rendering-Show-component-renders-when-condition-is-true-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Luna-Conditional-Rendering-show-renders-content-when-initially-true-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Luna-Conditional-Rendering-show-toggles-visibility-dynamically-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Memo-Dependency-Chain-conditional-memo-dependencies-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Signal-Behavior-Comparison-basic-signal-get-set-produces-same-values-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Signal-Behavior-Comparison-batch-updates-produce-same-final-values-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Untrack-and-Peek-Behavior-peek-reads-value-without-tracking-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Untrack-and-Peek-Behavior-selective-tracking-with-untrack-1.png +0 -0
- package/tests/__screenshots__/preact-signals-comparison.test.ts/Untrack-and-Peek-Behavior-untrack-prevents-dependency-tracking-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API--SolidJS-style--reactivity-accessor-is-reactive-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-AsyncState-helpers-stateError-returns-empty-string-for-non-failure-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-AsyncState-helpers-stateError-returns-undefined-for-non-failure-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-AsyncState-helpers-stateIsFailure-and-stateError-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-AsyncState-helpers-stateIsPending-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-AsyncState-helpers-stateIsSuccess-and-stateValue-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-AsyncState-helpers-stateValue-returns-undefined-for-non-success-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createDeferred-reject-transitions-to-failure-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createDeferred-resolve-transitions-to-success-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createDeferred-returns-resource--resolve--and-reject-functions-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createDeferred-starts-in-pending-state-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createResource-async-resolve-works-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createResource-starts-in-pending-state-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createResource-transitions-to-failure-on-reject-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-createResource-transitions-to-success-on-resolve-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-integration-with-Promise-can-wrap-fetch-like-async-operations-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-integration-with-Promise-works-with-setTimeout-simulation-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-resourceGet-vs-resourcePeek-resourceGet-tracks-dependencies-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-resourceGet-vs-resourcePeek-resourcePeek-does-not-track-dependencies-1.png +0 -0
- package/tests/__screenshots__/resource.test.ts/Resource-API-resourceRefetch-refetch-resets-to-pending-and-re-runs-fetcher-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/Portal-component-Portal-renders-children-to-body-by-default-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/Portal-component-Portal-renders-to-selector-mount-target-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/SolidJS-API-compatibility-createEffect-tracks-dependencies-automatically-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/Switch-Match-component-Switch-with-accessor-condition-in-Match-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/Switch-Match-component-Switch-with-multiple-Match-components-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/Switch-Match-component-Switch-with-single-Match-and-fallback-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/Switch-Match-components-Switch-updates-DOM-when-signal-changes-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/on---utility-on-tracks-multiple-dependencies-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/on---utility-on-tracks-single-dependency-1.png +0 -0
- package/tests/__screenshots__/solidjs-api.test.ts/on---utility-on-with-defer-option-skips-initial-run-1.png +0 -0
- package/tests/__screenshots__/store.test.ts/createStore-Arrays-array-updates-work-1.png +0 -0
- package/tests/__screenshots__/store.test.ts/createStore-Reactivity-only-triggers-when-accessed-property-changes-1.png +0 -0
- package/tests/__screenshots__/store.test.ts/createStore-Reactivity-parent-path-change-notifies-child-accessors-1.png +0 -0
- package/tests/__screenshots__/store.test.ts/createStore-Reactivity-tracks-nested-property-access-1.png +0 -0
- package/tests/__screenshots__/store.test.ts/createStore-Reactivity-tracks-property-access-in-effects-1.png +0 -0
- package/tests/context.test.ts +118 -0
- package/tests/css-optimizer-extractors.test.ts +264 -0
- package/tests/css-optimizer-integration.test.ts +566 -0
- package/tests/css-optimizer-transformers.test.ts +301 -0
- package/tests/css-optimizer.test.ts +646 -0
- package/tests/css-runtime.bench.ts +442 -0
- package/tests/css-runtime.test.ts +342 -0
- package/tests/dom.test.ts +872 -0
- package/tests/integration.test.ts +405 -0
- package/tests/issue-5-for-infinite-loop.test.ts +516 -0
- package/tests/jsx-runtime.test.tsx +393 -0
- package/tests/lifecycle.test.ts +833 -0
- package/tests/move-before.bench.ts +304 -0
- package/tests/preact-signals-comparison.test.ts +1608 -0
- package/tests/resource.test.ts +160 -0
- package/tests/router.test.ts +117 -0
- package/tests/show-initial-mount-leak.test.tsx +182 -0
- package/tests/solidjs-api.test.ts +659 -0
- package/tests/static-perf.bench.ts +64 -0
- package/tests/store.test.ts +263 -0
- package/tests/tsx-syntax.test.tsx +404 -0
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
text,
|
|
4
|
+
textDyn,
|
|
5
|
+
createElement,
|
|
6
|
+
createElementNs,
|
|
7
|
+
svgNs,
|
|
8
|
+
mathmlNs,
|
|
9
|
+
render,
|
|
10
|
+
mount,
|
|
11
|
+
show,
|
|
12
|
+
jsx,
|
|
13
|
+
jsxs,
|
|
14
|
+
Fragment,
|
|
15
|
+
events,
|
|
16
|
+
forEach,
|
|
17
|
+
For,
|
|
18
|
+
Show,
|
|
19
|
+
createSignal,
|
|
20
|
+
createRenderEffect,
|
|
21
|
+
} from "../src/index";
|
|
22
|
+
import { jsx as jsxRuntime } from "../src/jsx-runtime";
|
|
23
|
+
|
|
24
|
+
// MoonBit tuple representation for attrs: [name, value] -> { _0: name, _1: value }
|
|
25
|
+
// AttrValue constructors: $tag: 0 = Static, 1 = Dynamic, 2 = Handler
|
|
26
|
+
function attr(name: string, value: unknown) {
|
|
27
|
+
return { _0: name, _1: value };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const AttrValue = {
|
|
31
|
+
Static: (value: string) => ({ $tag: 0, _0: value }),
|
|
32
|
+
Dynamic: (getter: () => string) => ({ $tag: 1, _0: getter }),
|
|
33
|
+
Handler: (handler: (e: unknown) => void) => ({ $tag: 2, _0: handler }),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe("DOM API", () => {
|
|
37
|
+
let container: HTMLElement;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
container = document.createElement("div");
|
|
41
|
+
document.body.appendChild(container);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("text nodes", () => {
|
|
45
|
+
test("text creates a text node", () => {
|
|
46
|
+
const node = text("hello");
|
|
47
|
+
expect(node).toBeDefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("text node renders to DOM", () => {
|
|
51
|
+
const node = text("hello world");
|
|
52
|
+
render(container, node);
|
|
53
|
+
expect(container.textContent).toBe("hello world");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("textDyn creates reactive text node", () => {
|
|
57
|
+
const [value, setValue] = createSignal("initial");
|
|
58
|
+
const node = textDyn(value);
|
|
59
|
+
render(container, node);
|
|
60
|
+
expect(container.textContent).toBe("initial");
|
|
61
|
+
|
|
62
|
+
setValue("updated");
|
|
63
|
+
expect(container.textContent).toBe("updated");
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("createElement", () => {
|
|
68
|
+
test("createElement creates element with no attrs", () => {
|
|
69
|
+
const node = createElement("div", [], []);
|
|
70
|
+
expect(node).toBeDefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("createElement renders to DOM", () => {
|
|
74
|
+
const node = createElement("div", [], [text("content")]);
|
|
75
|
+
render(container, node);
|
|
76
|
+
expect(container.innerHTML).toBe("<div>content</div>");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("createElement with static attributes", () => {
|
|
80
|
+
const node = createElement(
|
|
81
|
+
"div",
|
|
82
|
+
[
|
|
83
|
+
attr("id", AttrValue.Static("my-id")),
|
|
84
|
+
attr("className", AttrValue.Static("my-class")),
|
|
85
|
+
],
|
|
86
|
+
[]
|
|
87
|
+
);
|
|
88
|
+
render(container, node);
|
|
89
|
+
const div = container.querySelector("div");
|
|
90
|
+
expect(div?.id).toBe("my-id");
|
|
91
|
+
expect(div?.className).toBe("my-class");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("createElement with style attribute", () => {
|
|
95
|
+
const node = createElement(
|
|
96
|
+
"div",
|
|
97
|
+
[attr("style", AttrValue.Static("color: red; margin: 10px"))],
|
|
98
|
+
[]
|
|
99
|
+
);
|
|
100
|
+
render(container, node);
|
|
101
|
+
const div = container.querySelector("div");
|
|
102
|
+
expect(div?.getAttribute("style")).toBe("color: red; margin: 10px");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("createElement with nested children", () => {
|
|
106
|
+
const node = createElement("div", [], [
|
|
107
|
+
createElement("span", [], [text("child1")]),
|
|
108
|
+
createElement("span", [], [text("child2")]),
|
|
109
|
+
]);
|
|
110
|
+
render(container, node);
|
|
111
|
+
expect(container.querySelectorAll("span").length).toBe(2);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("createElement with dynamic attribute", () => {
|
|
115
|
+
const [className, setClassName] = createSignal("initial-class");
|
|
116
|
+
const node = createElement(
|
|
117
|
+
"div",
|
|
118
|
+
[attr("className", AttrValue.Dynamic(className))],
|
|
119
|
+
[]
|
|
120
|
+
);
|
|
121
|
+
render(container, node);
|
|
122
|
+
const div = container.querySelector("div");
|
|
123
|
+
expect(div?.className).toBe("initial-class");
|
|
124
|
+
|
|
125
|
+
setClassName("updated-class");
|
|
126
|
+
expect(div?.className).toBe("updated-class");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("createElement with event handler", () => {
|
|
130
|
+
let clicked = false;
|
|
131
|
+
const node = createElement(
|
|
132
|
+
"button",
|
|
133
|
+
[attr("click", AttrValue.Handler(() => { clicked = true; }))],
|
|
134
|
+
[text("Click me")]
|
|
135
|
+
);
|
|
136
|
+
render(container, node);
|
|
137
|
+
const button = container.querySelector("button");
|
|
138
|
+
button?.click();
|
|
139
|
+
expect(clicked).toBe(true);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("createElement with value attribute (input)", () => {
|
|
143
|
+
const node = createElement(
|
|
144
|
+
"input",
|
|
145
|
+
[attr("value", AttrValue.Static("test-value"))],
|
|
146
|
+
[]
|
|
147
|
+
);
|
|
148
|
+
render(container, node);
|
|
149
|
+
const input = container.querySelector("input") as HTMLInputElement;
|
|
150
|
+
expect(input?.value).toBe("test-value");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("createElement with checked attribute", () => {
|
|
154
|
+
const node = createElement(
|
|
155
|
+
"input",
|
|
156
|
+
[
|
|
157
|
+
attr("type", AttrValue.Static("checkbox")),
|
|
158
|
+
attr("checked", AttrValue.Static("true")),
|
|
159
|
+
],
|
|
160
|
+
[]
|
|
161
|
+
);
|
|
162
|
+
render(container, node);
|
|
163
|
+
const input = container.querySelector("input") as HTMLInputElement;
|
|
164
|
+
expect(input?.checked).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("createElement with disabled attribute true", () => {
|
|
168
|
+
const nodeDisabled = createElement(
|
|
169
|
+
"button",
|
|
170
|
+
[attr("disabled", AttrValue.Static("true"))],
|
|
171
|
+
[]
|
|
172
|
+
);
|
|
173
|
+
render(container, nodeDisabled);
|
|
174
|
+
const btn = container.querySelector("button");
|
|
175
|
+
expect(btn?.hasAttribute("disabled")).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("createElement with disabled attribute false", () => {
|
|
179
|
+
const nodeEnabled = createElement(
|
|
180
|
+
"button",
|
|
181
|
+
[attr("disabled", AttrValue.Static("false"))],
|
|
182
|
+
[]
|
|
183
|
+
);
|
|
184
|
+
render(container, nodeEnabled);
|
|
185
|
+
const btn = container.querySelector("button");
|
|
186
|
+
expect(btn?.hasAttribute("disabled")).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("createElement with dynamic style", () => {
|
|
190
|
+
const [style, setStyle] = createSignal("color: blue");
|
|
191
|
+
const node = createElement(
|
|
192
|
+
"div",
|
|
193
|
+
[attr("style", AttrValue.Dynamic(style))],
|
|
194
|
+
[]
|
|
195
|
+
);
|
|
196
|
+
render(container, node);
|
|
197
|
+
const div = container.querySelector("div");
|
|
198
|
+
expect(div?.getAttribute("style")).toBe("color: blue");
|
|
199
|
+
|
|
200
|
+
setStyle("color: green");
|
|
201
|
+
expect(div?.getAttribute("style")).toBe("color: green");
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("jsx/jsxs", () => {
|
|
206
|
+
test("jsx creates element", () => {
|
|
207
|
+
const node = jsx("div", [], [text("jsx content")]);
|
|
208
|
+
render(container, node);
|
|
209
|
+
expect(container.innerHTML).toBe("<div>jsx content</div>");
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("jsxs creates element with multiple children", () => {
|
|
213
|
+
const node = jsxs("div", [], [text("child1"), text("child2")]);
|
|
214
|
+
render(container, node);
|
|
215
|
+
expect(container.textContent).toBe("child1child2");
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe("Fragment", () => {
|
|
220
|
+
test("Fragment wraps multiple children", () => {
|
|
221
|
+
const node = Fragment([text("a"), text("b"), text("c")]);
|
|
222
|
+
render(container, node);
|
|
223
|
+
expect(container.textContent).toBe("abc");
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe("render and mount", () => {
|
|
228
|
+
test("render clears container first", () => {
|
|
229
|
+
container.innerHTML = "<p>existing</p>";
|
|
230
|
+
const node = text("new content");
|
|
231
|
+
render(container, node);
|
|
232
|
+
expect(container.textContent).toBe("new content");
|
|
233
|
+
expect(container.querySelector("p")).toBeNull();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("mount appends without clearing", () => {
|
|
237
|
+
container.innerHTML = "<p>existing</p>";
|
|
238
|
+
const node = text("appended");
|
|
239
|
+
mount(container, node);
|
|
240
|
+
expect(container.textContent).toBe("existingappended");
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("show (conditional rendering)", () => {
|
|
245
|
+
test("show creates a node", () => {
|
|
246
|
+
const [visible] = createSignal(true);
|
|
247
|
+
const node = show(visible, () =>
|
|
248
|
+
createElement("div", [attr("id", AttrValue.Static("shown"))], [text("visible")])
|
|
249
|
+
);
|
|
250
|
+
expect(node).toBeDefined();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("show with false condition creates placeholder", () => {
|
|
254
|
+
const [visible] = createSignal(false);
|
|
255
|
+
const node = show(visible, () =>
|
|
256
|
+
createElement("div", [], [text("hidden")])
|
|
257
|
+
);
|
|
258
|
+
mount(container, node);
|
|
259
|
+
// When false, only a comment placeholder is rendered
|
|
260
|
+
expect(container.childNodes.length).toBeGreaterThanOrEqual(1);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe("forEach (list rendering)", () => {
|
|
265
|
+
test("forEach renders initial list", () => {
|
|
266
|
+
const [items] = createSignal(["a", "b", "c"]);
|
|
267
|
+
const node = forEach(items, (item: string, _index: number) =>
|
|
268
|
+
createElement("span", [], [text(item)])
|
|
269
|
+
);
|
|
270
|
+
mount(container, node);
|
|
271
|
+
expect(container.querySelectorAll("span").length).toBe(3);
|
|
272
|
+
expect(container.textContent).toBe("abc");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("forEach updates when items change", () => {
|
|
276
|
+
const [items, setItems] = createSignal(["x", "y"]);
|
|
277
|
+
const node = forEach(items, (item: string, _index: number) =>
|
|
278
|
+
createElement("span", [], [text(item)])
|
|
279
|
+
);
|
|
280
|
+
mount(container, node);
|
|
281
|
+
expect(container.textContent).toBe("xy");
|
|
282
|
+
|
|
283
|
+
setItems(["x", "y", "z"]);
|
|
284
|
+
expect(container.querySelectorAll("span").length).toBe(3);
|
|
285
|
+
expect(container.textContent).toBe("xyz");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test("forEach removes items", () => {
|
|
289
|
+
const [items, setItems] = createSignal(["1", "2", "3"]);
|
|
290
|
+
const node = forEach(items, (item: string, _index: number) =>
|
|
291
|
+
createElement("span", [], [text(item)])
|
|
292
|
+
);
|
|
293
|
+
mount(container, node);
|
|
294
|
+
expect(container.querySelectorAll("span").length).toBe(3);
|
|
295
|
+
|
|
296
|
+
setItems(["1"]);
|
|
297
|
+
expect(container.querySelectorAll("span").length).toBe(1);
|
|
298
|
+
expect(container.textContent).toBe("1");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("forEach handles empty array", () => {
|
|
302
|
+
const [items] = createSignal<string[]>([]);
|
|
303
|
+
const node = forEach(items, (item: string, _index: number) =>
|
|
304
|
+
createElement("span", [], [text(item)])
|
|
305
|
+
);
|
|
306
|
+
mount(container, node);
|
|
307
|
+
expect(container.querySelectorAll("span").length).toBe(0);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("forEach handles clear to empty", () => {
|
|
311
|
+
const [items, setItems] = createSignal(["a", "b"]);
|
|
312
|
+
const node = forEach(items, (item: string, _index: number) =>
|
|
313
|
+
createElement("span", [], [text(item)])
|
|
314
|
+
);
|
|
315
|
+
mount(container, node);
|
|
316
|
+
expect(container.querySelectorAll("span").length).toBe(2);
|
|
317
|
+
|
|
318
|
+
setItems([]);
|
|
319
|
+
expect(container.querySelectorAll("span").length).toBe(0);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
describe("events helper", () => {
|
|
324
|
+
test("events returns handler map", () => {
|
|
325
|
+
const handlers = events();
|
|
326
|
+
expect(handlers).toBeDefined();
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe("effect with DOM", () => {
|
|
331
|
+
test("effect tracks signal changes", () => {
|
|
332
|
+
const [count, setCount] = createSignal(0);
|
|
333
|
+
const log: number[] = [];
|
|
334
|
+
|
|
335
|
+
createRenderEffect(() => {
|
|
336
|
+
log.push(count());
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
expect(log).toEqual([0]);
|
|
340
|
+
setCount(1);
|
|
341
|
+
expect(log).toEqual([0, 1]);
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
describe("For component (SolidJS-style)", () => {
|
|
346
|
+
test("For renders list with getter", () => {
|
|
347
|
+
const [items] = createSignal(["a", "b", "c"]);
|
|
348
|
+
|
|
349
|
+
const node = For({
|
|
350
|
+
each: items,
|
|
351
|
+
children: (item: string, index: () => number) =>
|
|
352
|
+
createElement("li", [], [text(`${index()}: ${item}`)]),
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
mount(container, node);
|
|
356
|
+
expect(container.querySelectorAll("li").length).toBe(3);
|
|
357
|
+
expect(container.textContent).toBe("0: a1: b2: c");
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test("For updates when signal changes", () => {
|
|
361
|
+
const [items, setItems] = createSignal(["x", "y"]);
|
|
362
|
+
|
|
363
|
+
const node = For({
|
|
364
|
+
each: items,
|
|
365
|
+
children: (item: string) => createElement("span", [], [text(item)]),
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
mount(container, node);
|
|
369
|
+
expect(container.textContent).toBe("xy");
|
|
370
|
+
|
|
371
|
+
setItems(["x", "y", "z"]);
|
|
372
|
+
expect(container.querySelectorAll("span").length).toBe(3);
|
|
373
|
+
expect(container.textContent).toBe("xyz");
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
test("For provides index as getter function", () => {
|
|
377
|
+
const [items] = createSignal(["one", "two"]);
|
|
378
|
+
const indices: number[] = [];
|
|
379
|
+
|
|
380
|
+
const node = For({
|
|
381
|
+
each: items,
|
|
382
|
+
children: (_item: string, index: () => number) => {
|
|
383
|
+
indices.push(index());
|
|
384
|
+
return createElement("div", [], []);
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
mount(container, node);
|
|
389
|
+
expect(indices).toEqual([0, 1]);
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
describe("Show component (SolidJS-style)", () => {
|
|
394
|
+
test("Show hides content when condition is false", () => {
|
|
395
|
+
const [visible] = createSignal(false);
|
|
396
|
+
|
|
397
|
+
const node = Show({
|
|
398
|
+
when: visible,
|
|
399
|
+
children: createElement("div", [], [text("visible")]),
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
mount(container, node);
|
|
403
|
+
expect(container.querySelector("div")).toBeNull();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test("Show toggles visibility", () => {
|
|
407
|
+
const [visible, setVisible] = createSignal(false);
|
|
408
|
+
|
|
409
|
+
const node = Show({
|
|
410
|
+
when: visible,
|
|
411
|
+
children: createElement("span", [], [text("content")]),
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
mount(container, node);
|
|
415
|
+
expect(container.querySelector("span")).toBeNull();
|
|
416
|
+
|
|
417
|
+
setVisible(true);
|
|
418
|
+
expect(container.querySelector("span")).not.toBeNull();
|
|
419
|
+
|
|
420
|
+
setVisible(false);
|
|
421
|
+
expect(container.querySelector("span")).toBeNull();
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
test("Show accepts children as function", () => {
|
|
425
|
+
const [value, setValue] = createSignal<string | null>(null);
|
|
426
|
+
|
|
427
|
+
const node = Show({
|
|
428
|
+
when: value,
|
|
429
|
+
children: (v: string) => createElement("span", [], [text(v)]),
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
mount(container, node);
|
|
433
|
+
expect(container.querySelector("span")).toBeNull();
|
|
434
|
+
|
|
435
|
+
setValue("hello");
|
|
436
|
+
expect(container.querySelector("span")?.textContent).toBe("hello");
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
describe("ref callback (JSX style)", () => {
|
|
441
|
+
test("ref callback is called with element (low-level __ref)", () => {
|
|
442
|
+
let capturedElement: HTMLElement | null = null;
|
|
443
|
+
|
|
444
|
+
const node = createElement(
|
|
445
|
+
"div",
|
|
446
|
+
[attr("__ref", AttrValue.Handler((el: unknown) => {
|
|
447
|
+
capturedElement = el as HTMLElement;
|
|
448
|
+
}))],
|
|
449
|
+
[text("content")]
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
render(container, node);
|
|
453
|
+
|
|
454
|
+
expect(capturedElement).not.toBeNull();
|
|
455
|
+
expect(capturedElement?.tagName).toBe("DIV");
|
|
456
|
+
expect(capturedElement?.textContent).toBe("content");
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
test("jsx-runtime converts ref prop to __ref", () => {
|
|
460
|
+
let capturedElement: HTMLElement | null = null;
|
|
461
|
+
|
|
462
|
+
// Simulates: <div ref={(el) => capturedElement = el}>content</div>
|
|
463
|
+
const node = jsxRuntime("div", {
|
|
464
|
+
ref: (el: HTMLElement) => { capturedElement = el; },
|
|
465
|
+
children: "content",
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
render(container, node);
|
|
469
|
+
|
|
470
|
+
expect(capturedElement).not.toBeNull();
|
|
471
|
+
expect(capturedElement?.tagName).toBe("DIV");
|
|
472
|
+
expect(capturedElement?.textContent).toBe("content");
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
test("jsx-runtime ref works with input elements", () => {
|
|
476
|
+
let inputRef: HTMLInputElement | null = null;
|
|
477
|
+
|
|
478
|
+
// Simulates: <input type="text" ref={(el) => inputRef = el} />
|
|
479
|
+
const node = jsxRuntime("input", {
|
|
480
|
+
type: "text",
|
|
481
|
+
ref: (el: HTMLInputElement) => { inputRef = el; },
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
render(container, node);
|
|
485
|
+
|
|
486
|
+
expect(inputRef).not.toBeNull();
|
|
487
|
+
expect(inputRef?.tagName).toBe("INPUT");
|
|
488
|
+
expect(inputRef?.type).toBe("text");
|
|
489
|
+
|
|
490
|
+
// Test that we can use the ref to focus the input
|
|
491
|
+
inputRef?.focus();
|
|
492
|
+
expect(document.activeElement).toBe(inputRef);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
test("ref callback provides access to DOM properties", () => {
|
|
496
|
+
let capturedId: string | null = null;
|
|
497
|
+
|
|
498
|
+
const node = createElement(
|
|
499
|
+
"input",
|
|
500
|
+
[
|
|
501
|
+
attr("id", AttrValue.Static("test-input")),
|
|
502
|
+
attr("type", AttrValue.Static("text")),
|
|
503
|
+
attr("__ref", AttrValue.Handler((el: unknown) => {
|
|
504
|
+
capturedId = (el as HTMLInputElement).id;
|
|
505
|
+
})),
|
|
506
|
+
],
|
|
507
|
+
[]
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
render(container, node);
|
|
511
|
+
|
|
512
|
+
expect(capturedId).toBe("test-input");
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
test("ref callback can call DOM methods", () => {
|
|
516
|
+
let inputElement: HTMLInputElement | null = null;
|
|
517
|
+
|
|
518
|
+
const node = createElement(
|
|
519
|
+
"input",
|
|
520
|
+
[
|
|
521
|
+
attr("type", AttrValue.Static("text")),
|
|
522
|
+
attr("__ref", AttrValue.Handler((el: unknown) => {
|
|
523
|
+
inputElement = el as HTMLInputElement;
|
|
524
|
+
})),
|
|
525
|
+
],
|
|
526
|
+
[]
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
render(container, node);
|
|
530
|
+
|
|
531
|
+
expect(inputElement).not.toBeNull();
|
|
532
|
+
// Call focus() via ref
|
|
533
|
+
inputElement?.focus();
|
|
534
|
+
expect(document.activeElement).toBe(inputElement);
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
test("ref callback with nested elements", () => {
|
|
538
|
+
const refs: HTMLElement[] = [];
|
|
539
|
+
|
|
540
|
+
const node = createElement(
|
|
541
|
+
"div",
|
|
542
|
+
[attr("__ref", AttrValue.Handler((el: unknown) => refs.push(el as HTMLElement)))],
|
|
543
|
+
[
|
|
544
|
+
createElement(
|
|
545
|
+
"span",
|
|
546
|
+
[attr("__ref", AttrValue.Handler((el: unknown) => refs.push(el as HTMLElement)))],
|
|
547
|
+
[text("nested")]
|
|
548
|
+
),
|
|
549
|
+
]
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
render(container, node);
|
|
553
|
+
|
|
554
|
+
// Both refs should be captured
|
|
555
|
+
expect(refs.length).toBe(2);
|
|
556
|
+
// Note: Children are processed before parent attributes, so span comes first
|
|
557
|
+
const tagNames = refs.map(el => el.tagName).sort();
|
|
558
|
+
expect(tagNames).toEqual(["DIV", "SPAN"]);
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
describe("createElementNs (SVG support)", () => {
|
|
563
|
+
test("svgNs returns correct namespace", () => {
|
|
564
|
+
expect(svgNs()).toBe("http://www.w3.org/2000/svg");
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
test("mathmlNs returns correct namespace", () => {
|
|
568
|
+
expect(mathmlNs()).toBe("http://www.w3.org/1998/Math/MathML");
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
test("createElementNs creates SVG element with correct namespace", () => {
|
|
572
|
+
const node = createElementNs(svgNs(), "svg", [], []);
|
|
573
|
+
render(container, node);
|
|
574
|
+
const svg = container.querySelector("svg");
|
|
575
|
+
expect(svg).not.toBeNull();
|
|
576
|
+
expect(svg?.namespaceURI).toBe("http://www.w3.org/2000/svg");
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
test("createElementNs creates SVG child elements", () => {
|
|
580
|
+
const rect = createElementNs(
|
|
581
|
+
svgNs(),
|
|
582
|
+
"rect",
|
|
583
|
+
[
|
|
584
|
+
attr("x", AttrValue.Static("10")),
|
|
585
|
+
attr("y", AttrValue.Static("10")),
|
|
586
|
+
attr("width", AttrValue.Static("100")),
|
|
587
|
+
attr("height", AttrValue.Static("50")),
|
|
588
|
+
attr("fill", AttrValue.Static("blue")),
|
|
589
|
+
],
|
|
590
|
+
[]
|
|
591
|
+
);
|
|
592
|
+
const svg = createElementNs(
|
|
593
|
+
svgNs(),
|
|
594
|
+
"svg",
|
|
595
|
+
[
|
|
596
|
+
attr("width", AttrValue.Static("200")),
|
|
597
|
+
attr("height", AttrValue.Static("200")),
|
|
598
|
+
],
|
|
599
|
+
[rect]
|
|
600
|
+
);
|
|
601
|
+
render(container, svg);
|
|
602
|
+
|
|
603
|
+
const svgEl = container.querySelector("svg");
|
|
604
|
+
const rectEl = container.querySelector("rect");
|
|
605
|
+
expect(svgEl).not.toBeNull();
|
|
606
|
+
expect(rectEl).not.toBeNull();
|
|
607
|
+
expect(rectEl?.namespaceURI).toBe("http://www.w3.org/2000/svg");
|
|
608
|
+
expect(rectEl?.getAttribute("fill")).toBe("blue");
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
test("createElementNs works with circle and path", () => {
|
|
612
|
+
const circle = createElementNs(
|
|
613
|
+
svgNs(),
|
|
614
|
+
"circle",
|
|
615
|
+
[
|
|
616
|
+
attr("cx", AttrValue.Static("50")),
|
|
617
|
+
attr("cy", AttrValue.Static("50")),
|
|
618
|
+
attr("r", AttrValue.Static("25")),
|
|
619
|
+
],
|
|
620
|
+
[]
|
|
621
|
+
);
|
|
622
|
+
const path = createElementNs(
|
|
623
|
+
svgNs(),
|
|
624
|
+
"path",
|
|
625
|
+
[attr("d", AttrValue.Static("M 10 10 L 50 50"))],
|
|
626
|
+
[]
|
|
627
|
+
);
|
|
628
|
+
const g = createElementNs(svgNs(), "g", [], [circle, path]);
|
|
629
|
+
const svg = createElementNs(svgNs(), "svg", [], [g]);
|
|
630
|
+
render(container, svg);
|
|
631
|
+
|
|
632
|
+
expect(container.querySelector("circle")).not.toBeNull();
|
|
633
|
+
expect(container.querySelector("path")).not.toBeNull();
|
|
634
|
+
expect(container.querySelector("g")).not.toBeNull();
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
test("createElementNs with dynamic attribute", () => {
|
|
638
|
+
const [fill, setFill] = createSignal("red");
|
|
639
|
+
const rect = createElementNs(
|
|
640
|
+
svgNs(),
|
|
641
|
+
"rect",
|
|
642
|
+
[
|
|
643
|
+
attr("width", AttrValue.Static("100")),
|
|
644
|
+
attr("height", AttrValue.Static("100")),
|
|
645
|
+
attr("fill", AttrValue.Dynamic(fill)),
|
|
646
|
+
],
|
|
647
|
+
[]
|
|
648
|
+
);
|
|
649
|
+
const svg = createElementNs(svgNs(), "svg", [], [rect]);
|
|
650
|
+
render(container, svg);
|
|
651
|
+
|
|
652
|
+
const rectEl = container.querySelector("rect");
|
|
653
|
+
expect(rectEl?.getAttribute("fill")).toBe("red");
|
|
654
|
+
|
|
655
|
+
setFill("green");
|
|
656
|
+
expect(rectEl?.getAttribute("fill")).toBe("green");
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
test("SVG can be nested inside regular HTML elements", () => {
|
|
660
|
+
const circle = createElementNs(
|
|
661
|
+
svgNs(),
|
|
662
|
+
"circle",
|
|
663
|
+
[
|
|
664
|
+
attr("cx", AttrValue.Static("50")),
|
|
665
|
+
attr("cy", AttrValue.Static("50")),
|
|
666
|
+
attr("r", AttrValue.Static("25")),
|
|
667
|
+
],
|
|
668
|
+
[]
|
|
669
|
+
);
|
|
670
|
+
const svg = createElementNs(
|
|
671
|
+
svgNs(),
|
|
672
|
+
"svg",
|
|
673
|
+
[
|
|
674
|
+
attr("width", AttrValue.Static("100")),
|
|
675
|
+
attr("height", AttrValue.Static("100")),
|
|
676
|
+
],
|
|
677
|
+
[circle]
|
|
678
|
+
);
|
|
679
|
+
const div = createElement("div", [], [svg]);
|
|
680
|
+
render(container, div);
|
|
681
|
+
|
|
682
|
+
const divEl = container.querySelector("div");
|
|
683
|
+
const svgEl = container.querySelector("svg");
|
|
684
|
+
expect(divEl).not.toBeNull();
|
|
685
|
+
expect(svgEl?.parentElement).toBe(divEl);
|
|
686
|
+
expect(svgEl?.namespaceURI).toBe("http://www.w3.org/2000/svg");
|
|
687
|
+
});
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
describe("forEach with SVG elements", () => {
|
|
691
|
+
test("forEach renders SVG elements", () => {
|
|
692
|
+
const [items, setItems] = createSignal([
|
|
693
|
+
{ id: "1", x: 10, y: 10 },
|
|
694
|
+
{ id: "2", x: 50, y: 50 },
|
|
695
|
+
]);
|
|
696
|
+
|
|
697
|
+
const forEachNode = forEach(
|
|
698
|
+
items,
|
|
699
|
+
(item) =>
|
|
700
|
+
createElementNs(
|
|
701
|
+
svgNs(),
|
|
702
|
+
"rect",
|
|
703
|
+
[
|
|
704
|
+
attr("data-id", AttrValue.Static(item.id)),
|
|
705
|
+
attr("x", AttrValue.Static(String(item.x))),
|
|
706
|
+
attr("y", AttrValue.Static(String(item.y))),
|
|
707
|
+
attr("width", AttrValue.Static("20")),
|
|
708
|
+
attr("height", AttrValue.Static("20")),
|
|
709
|
+
],
|
|
710
|
+
[]
|
|
711
|
+
)
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
const svg = createElementNs(
|
|
715
|
+
svgNs(),
|
|
716
|
+
"svg",
|
|
717
|
+
[
|
|
718
|
+
attr("width", AttrValue.Static("200")),
|
|
719
|
+
attr("height", AttrValue.Static("200")),
|
|
720
|
+
],
|
|
721
|
+
[forEachNode]
|
|
722
|
+
);
|
|
723
|
+
|
|
724
|
+
render(container, svg);
|
|
725
|
+
|
|
726
|
+
const rects = container.querySelectorAll("rect");
|
|
727
|
+
expect(rects.length).toBe(2);
|
|
728
|
+
expect(rects[0].getAttribute("data-id")).toBe("1");
|
|
729
|
+
expect(rects[1].getAttribute("data-id")).toBe("2");
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
test("forEach updates SVG elements when signal changes", () => {
|
|
733
|
+
const [items, setItems] = createSignal([{ id: "1", x: 10 }]);
|
|
734
|
+
|
|
735
|
+
const forEachNode = forEach(
|
|
736
|
+
items,
|
|
737
|
+
(item) =>
|
|
738
|
+
createElementNs(
|
|
739
|
+
svgNs(),
|
|
740
|
+
"circle",
|
|
741
|
+
[
|
|
742
|
+
attr("data-id", AttrValue.Static(item.id)),
|
|
743
|
+
attr("cx", AttrValue.Static(String(item.x))),
|
|
744
|
+
attr("cy", AttrValue.Static("50")),
|
|
745
|
+
attr("r", AttrValue.Static("10")),
|
|
746
|
+
],
|
|
747
|
+
[]
|
|
748
|
+
)
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
const svg = createElementNs(svgNs(), "svg", [], [forEachNode]);
|
|
752
|
+
render(container, svg);
|
|
753
|
+
|
|
754
|
+
expect(container.querySelectorAll("circle").length).toBe(1);
|
|
755
|
+
|
|
756
|
+
// Add item
|
|
757
|
+
setItems([
|
|
758
|
+
{ id: "1", x: 10 },
|
|
759
|
+
{ id: "2", x: 50 },
|
|
760
|
+
]);
|
|
761
|
+
expect(container.querySelectorAll("circle").length).toBe(2);
|
|
762
|
+
|
|
763
|
+
// Remove item
|
|
764
|
+
setItems([{ id: "2", x: 50 }]);
|
|
765
|
+
expect(container.querySelectorAll("circle").length).toBe(1);
|
|
766
|
+
expect(container.querySelector("circle")?.getAttribute("data-id")).toBe("2");
|
|
767
|
+
});
|
|
768
|
+
|
|
769
|
+
test("forEach handles reordering in SVG", () => {
|
|
770
|
+
const item1 = { id: "a", x: 10 };
|
|
771
|
+
const item2 = { id: "b", x: 20 };
|
|
772
|
+
const item3 = { id: "c", x: 30 };
|
|
773
|
+
|
|
774
|
+
const [items, setItems] = createSignal([item1, item2, item3]);
|
|
775
|
+
|
|
776
|
+
const forEachNode = forEach(
|
|
777
|
+
items,
|
|
778
|
+
(item) =>
|
|
779
|
+
createElementNs(
|
|
780
|
+
svgNs(),
|
|
781
|
+
"rect",
|
|
782
|
+
[attr("data-id", AttrValue.Static(item.id))],
|
|
783
|
+
[]
|
|
784
|
+
)
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
const svg = createElementNs(svgNs(), "svg", [], [forEachNode]);
|
|
788
|
+
render(container, svg);
|
|
789
|
+
|
|
790
|
+
const getIds = () =>
|
|
791
|
+
Array.from(container.querySelectorAll("rect")).map((r) =>
|
|
792
|
+
r.getAttribute("data-id")
|
|
793
|
+
);
|
|
794
|
+
|
|
795
|
+
expect(getIds()).toEqual(["a", "b", "c"]);
|
|
796
|
+
|
|
797
|
+
// Reorder
|
|
798
|
+
setItems([item3, item1, item2]);
|
|
799
|
+
expect(getIds()).toEqual(["c", "a", "b"]);
|
|
800
|
+
|
|
801
|
+
// Reverse
|
|
802
|
+
setItems([item2, item1, item3]);
|
|
803
|
+
expect(getIds()).toEqual(["b", "a", "c"]);
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
test("forEach with nested SVG groups", () => {
|
|
807
|
+
const [groups, setGroups] = createSignal([
|
|
808
|
+
{ id: "g1", items: ["a", "b"] },
|
|
809
|
+
{ id: "g2", items: ["c", "d"] },
|
|
810
|
+
]);
|
|
811
|
+
|
|
812
|
+
const forEachNode = forEach(
|
|
813
|
+
groups,
|
|
814
|
+
(group) =>
|
|
815
|
+
createElementNs(
|
|
816
|
+
svgNs(),
|
|
817
|
+
"g",
|
|
818
|
+
[attr("data-group", AttrValue.Static(group.id))],
|
|
819
|
+
group.items.map((item) =>
|
|
820
|
+
createElementNs(
|
|
821
|
+
svgNs(),
|
|
822
|
+
"rect",
|
|
823
|
+
[attr("data-item", AttrValue.Static(item))],
|
|
824
|
+
[]
|
|
825
|
+
)
|
|
826
|
+
)
|
|
827
|
+
)
|
|
828
|
+
);
|
|
829
|
+
|
|
830
|
+
const svg = createElementNs(svgNs(), "svg", [], [forEachNode]);
|
|
831
|
+
render(container, svg);
|
|
832
|
+
|
|
833
|
+
const g1 = container.querySelector('[data-group="g1"]');
|
|
834
|
+
const g2 = container.querySelector('[data-group="g2"]');
|
|
835
|
+
expect(g1?.querySelectorAll("rect").length).toBe(2);
|
|
836
|
+
expect(g2?.querySelectorAll("rect").length).toBe(2);
|
|
837
|
+
|
|
838
|
+
// Update groups
|
|
839
|
+
setGroups([{ id: "g1", items: ["a", "b", "c"] }]);
|
|
840
|
+
expect(container.querySelectorAll("g").length).toBe(1);
|
|
841
|
+
expect(container.querySelectorAll("rect").length).toBe(3);
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
test("forEach handles empty to non-empty transition in SVG", () => {
|
|
845
|
+
const [items, setItems] = createSignal<{ id: string }[]>([]);
|
|
846
|
+
|
|
847
|
+
const forEachNode = forEach(
|
|
848
|
+
items,
|
|
849
|
+
(item) =>
|
|
850
|
+
createElementNs(
|
|
851
|
+
svgNs(),
|
|
852
|
+
"circle",
|
|
853
|
+
[attr("data-id", AttrValue.Static(item.id))],
|
|
854
|
+
[]
|
|
855
|
+
)
|
|
856
|
+
);
|
|
857
|
+
|
|
858
|
+
const svg = createElementNs(svgNs(), "svg", [], [forEachNode]);
|
|
859
|
+
render(container, svg);
|
|
860
|
+
|
|
861
|
+
expect(container.querySelectorAll("circle").length).toBe(0);
|
|
862
|
+
|
|
863
|
+
// Add items
|
|
864
|
+
setItems([{ id: "1" }, { id: "2" }]);
|
|
865
|
+
expect(container.querySelectorAll("circle").length).toBe(2);
|
|
866
|
+
|
|
867
|
+
// Clear again
|
|
868
|
+
setItems([]);
|
|
869
|
+
expect(container.querySelectorAll("circle").length).toBe(0);
|
|
870
|
+
});
|
|
871
|
+
});
|
|
872
|
+
});
|