]> Lady’s Gitweb - Lemon/blob - mod.test.js
[window] Polyfill top‐level D·O·M interfaces
[Lemon] / mod.test.js
1 // 🍋🏷 Lemon ∷ mod.test.js
2 // ====================================================================
3 //
4 // Copyright © 2022 Lady [@ Lady’s Computer]..
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 assertEquals,
12 assertStrictEquals,
13 DOMImplementation,
14 } from "./dev-deps.js";
15 import Lemon from "./mod.js";
16
17 Deno.test({
18 name: "Lemon can be used to make a new element.",
19 fn: () => {
20 const elt = Lemon("xxx")({})``;
21 assertStrictEquals(typeof elt, "object");
22 assertStrictEquals(elt.localName, "xxx");
23 },
24 });
25
26 Deno.test({
27 name:
28 "Lemon can be used to make a new element in a specified document.",
29 fn: () => {
30 const doc = new DOMImplementation().createDocument(
31 null,
32 "福",
33 null,
34 );
35 const elt = Lemon.call({ document: doc }, "xxx")({})``;
36 assertStrictEquals(elt.ownerDocument, doc);
37 },
38 });
39
40 Deno.test({
41 name: "Lemon uses X·H·T·M·L as the default namespace.",
42 fn: () => {
43 const elt = Lemon("xxx")({})``;
44 assertStrictEquals(
45 elt.namespaceURI,
46 "http://www.w3.org/1999/xhtml",
47 );
48 },
49 });
50
51 Deno.test({
52 name: "Lemon uses the provided namespace if present.",
53 fn: () => {
54 const elt = Lemon("xxx", "example:gay")({})``;
55 assertStrictEquals(elt.namespaceURI, "example:gay");
56 },
57 });
58
59 Deno.test({
60 name: '`Lemon.◊` can be used in place of `Lemon("◊")`.',
61 fn: () => {
62 const elt = Lemon.xxx({})``;
63 assertStrictEquals(typeof elt, "object");
64 assertStrictEquals(elt.localName, "xxx");
65 assertStrictEquals(
66 elt.namespaceURI,
67 "http://www.w3.org/1999/xhtml",
68 );
69 const customElt = Lemon.creamPie({})``;
70 assertStrictEquals(typeof customElt, "object");
71 assertStrictEquals(customElt.localName, "cream-pie");
72 assertStrictEquals(
73 customElt.namespaceURI,
74 "http://www.w3.org/1999/xhtml",
75 );
76 },
77 });
78
79 Deno.test({
80 name: "Bound Lemons are also proxied.",
81 fn: () => {
82 const doc = new DOMImplementation().createDocument(
83 null,
84 "福",
85 null,
86 );
87 const myLemon = Lemon.bind({ document: doc });
88 const elt = myLemon.creamPie({})``;
89 assertStrictEquals(typeof elt, "object");
90 assertStrictEquals(elt.localName, "cream-pie");
91 assertStrictEquals(elt.ownerDocument, doc);
92 },
93 });
94
95 Deno.test({
96 name: "Lemon tags can be destructured.",
97 fn: () => {
98 const { creamPie, xxx } = Lemon;
99 const elt = xxx({})``;
100 assertStrictEquals(typeof elt, "object");
101 assertStrictEquals(elt.localName, "xxx");
102 assertStrictEquals(
103 elt.namespaceURI,
104 "http://www.w3.org/1999/xhtml",
105 );
106 const customElt = creamPie({})``;
107 assertStrictEquals(typeof customElt, "object");
108 assertStrictEquals(customElt.localName, "cream-pie");
109 assertStrictEquals(
110 customElt.namespaceURI,
111 "http://www.w3.org/1999/xhtml",
112 );
113 },
114 });
115
116 Deno.test({
117 name: "Lemon tags assign attributes.",
118 fn: () => {
119 const elt = Lemon.xxx({ "data-🍆": "💦" })``;
120 assertStrictEquals(elt.getAttribute("data-🍆"), "💦");
121 },
122 });
123
124 Deno.test({
125 name: "Lemon tags can assign attributes with property chaining.",
126 fn: () => {
127 const elt = (
128 Lemon.xxx
129 .class("abc")
130 .class("def")
131 .dataWet("💦")
132 .dataFalse("failure")
133 .dataTrue()
134 .hidden
135 .hidden
136 .dataName("data-name")
137 .dataName("data-name")
138 .dataFalse(false)
139 )``;
140 assertStrictEquals(elt.getAttribute("class"), "abc def");
141 assertStrictEquals(elt.getAttribute("data-wet"), "💦");
142 assertStrictEquals(elt.getAttribute("data-true"), "");
143 assertStrictEquals(elt.getAttribute("data-hidden"), "");
144 assertStrictEquals(elt.getAttribute("data-name"), "data-name");
145 assertStrictEquals(elt.hasAttribute("data-false"), false);
146 },
147 });
148
149 Deno.test({
150 name: "Lemon tags expose their internal state.",
151 fn: () => {
152 const eltTag = (
153 Lemon("xxx:yyy", "example:xxx")
154 .class("abc")
155 .class("def")
156 .dataWet("💦")
157 .dataFalse("failure")
158 .dataTrue()
159 .hidden
160 .hidden
161 .dataName("data-name")
162 .dataName("data-name")
163 .dataFalse(false)
164 .dataExtra
165 );
166 assertEquals(eltTag.attributes, {
167 class: "abc def",
168 "data-wet": "💦",
169 "data-true": "",
170 hidden: "",
171 "data-name": "data-name",
172 "data-extra": "",
173 });
174 assertStrictEquals(eltTag.localName, "yyy");
175 assertStrictEquals(eltTag.namespaceURI, "example:xxx");
176 assertStrictEquals(eltTag.qualifiedName, "xxx:yyy");
177 assertStrictEquals(eltTag.ownerDocument, document);
178 assertStrictEquals(
179 eltTag.name,
180 '<xxx:yyy xmlns:xxx="example:xxx" class="abc def" data-wet="💦" data-true="" hidden="" data-name="data-name" data-extra=""> /* pending data-extra */',
181 );
182 assertStrictEquals(
183 eltTag().name,
184 '<xxx:yyy xmlns:xxx="example:xxx" class="abc def" data-wet="💦" data-true="" hidden="" data-name="data-name" data-extra="">',
185 );
186 },
187 });
188
189 Deno.test({
190 name: "Lemon tags support all kinds of expression.",
191 fn: () => {
192 const elt = Lemon.xxx({})` a ${null} b ${Lemon.creamPie(
193 {},
194 )`\t`}${" c "}`;
195 assertStrictEquals(elt.childNodes.length, 3);
196 assertStrictEquals(elt.childNodes[0].nodeType, 3);
197 assertStrictEquals(elt.childNodes[0].textContent, " a b ");
198 assertStrictEquals(elt.childNodes[1].nodeType, 1);
199 assertStrictEquals(elt.childNodes[1].localName, "cream-pie");
200 assertStrictEquals(elt.childNodes[1].textContent, "\\t");
201 assertStrictEquals(elt.childNodes[2].nodeType, 3);
202 assertStrictEquals(elt.childNodes[2].textContent, " c ");
203 },
204 });
205
206 Deno.test({
207 name: "Lemon tags normalize even nested nodes.",
208 fn: () => {
209 const notNormal = document.createElement("i");
210 notNormal.appendChild(document.createTextNode(" a "));
211 notNormal.appendChild(document.createTextNode(" b "));
212 assertStrictEquals(notNormal.childNodes.length, 2);
213 const elt = Lemon.xxx({})`${notNormal}`;
214 assertStrictEquals(elt.childNodes[0].nodeType, 1);
215 assertStrictEquals(elt.childNodes[0].childNodes.length, 1);
216 assertStrictEquals(
217 elt.childNodes[0].childNodes[0].textContent,
218 " a b ",
219 );
220 },
221 });
This page took 0.102609 seconds and 5 git commands to generate.