-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.jsx
354 lines (327 loc) ยท 8.94 KB
/
test.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h, renderJSX } from "./mod.js";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
function dedent(strings, ...params) {
const str = strings.reduce((prev, string, i) =>
prev + string + (params[i] ?? "")
);
const len = str.match(/^\s+/gm)?.reduce(
(res, str) => Math.min(str.length, res),
999,
) || 0;
return str
.replace(/^\n/, "")
.replace(/\n\s*$/, "")
.replace(new RegExp(`^([ |\\t]{${len}})`, "gm"), "");
}
Deno.test("pretty print", async () => {
const jsx = (
<>
<aside>
<a href="#">Link</a>
<dfn>
<abbr title="Cascading Style Sheets">
CSS
</abbr>
</dfn>
</aside>
</>
);
assertEquals(
await renderJSX(jsx, { pretty: false }),
'<aside><a href="#">Link</a><dfn><abbr title="Cascading Style Sheets">CSS</abbr></dfn></aside>',
);
assertEquals(
await renderJSX(jsx, { pretty: true, tab: " " }),
dedent`<aside>
<a href="#">Link</a>
<dfn>
<abbr title="Cascading Style Sheets">CSS</abbr>
</dfn>
</aside>`,
);
});
Deno.test("textarea has value prop, no children ", async () => {
assertEquals(
await renderJSX(<textarea value="foo" />),
"<textarea>foo</textarea>",
);
});
Deno.test("textarea has value prop & children ", async () => {
assertEquals(
await renderJSX(<textarea value="foo">bar</textarea>),
"<textarea>foo</textarea>",
);
});
Deno.test("textarea with value prop as undefined", async () => {
assertEquals(
await renderJSX(<textarea value={undefined}>bar</textarea>),
"<textarea>bar</textarea>",
);
});
Deno.test("textarea with value prop as null ", async () => {
assertEquals(
await renderJSX(<textarea value={null}>bar</textarea>),
"<textarea>bar</textarea>",
);
});
Deno.test("textarea without value prop, ", async () => {
assertEquals(
await renderJSX(<textarea>bar</textarea>),
"<textarea>bar</textarea>",
);
});
Deno.test("self-closing textarea without value prop", async () => {
assertEquals(
await renderJSX(<textarea />),
"<textarea></textarea>",
);
});
Deno.test("preformatted text", async () => {
assertEquals(
await renderJSX(
<pre>
{dedent`
foo
bar
`}
</pre>,
),
`<pre> foo\n bar</pre>`,
);
});
Deno.test("pretty printing doesn't format content of textarea & pre tags", async () => {
assertEquals(
await renderJSX(
<>
<p>
We can all fight against loneliness by engaging in random acts of
kindness.
</p>
<pre>
We can all fight against loneliness by engaging in random acts of
kindness.
</pre>
<textarea>
We can all fight against loneliness by engaging in random acts of
kindness.
</textarea>
</>,
),
dedent`<p>
We can all fight against loneliness by engaging in random acts of kindness.
</p>
<pre>We can all fight against loneliness by engaging in random acts of kindness.</pre>
<textarea>We can all fight against loneliness by engaging in random acts of kindness.</textarea>`,
);
});
Deno.test("href values", async () => {
assertEquals(
await renderJSX(<a id={'"'} href="http://example.com?foo=bar&baz">bar</a>),
`<a id=""" href="http://example.com?foo=bar&baz">bar</a>`,
);
});
Deno.test("html entities", async () => {
assertEquals(
await renderJSX(<>{'<&">'}</>),
`<&">`,
);
});
Deno.test("pretty options", async () => {
assertEquals(
await renderJSX(<aside>Nobody got a guided tour</aside>, {
maxInlineContentWidth: 10,
tab: "\t",
newline: "\n\n",
}),
`<aside>\n\n\tNobody got a guided tour\n\n</aside>`,
);
});
Deno.test("big example", async () => {
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
const AsyncElement = async ({ ms, children }) => {
await delay(ms);
return <x-async data-ms={ms}>{children}</x-async>;
};
const jsx = (
<>
<header style={{ marginBottom: "0px", color: "#333" }}>
<h1>Hello World ๐</h1>
</header>
<hr />
<main class="main-section">
<AsyncElement ms={50}>
<AsyncElement ms={30} />
<AsyncElement ms={50} />
<AsyncElement ms={50}>
Sometimes in life, random things can blind-side you. ๐ธ๐ธ๐ธ
</AsyncElement>
</AsyncElement>
<form>
<input type="text" value="foo" />
<input type="checkbox" checked />
<br />
<textarea value="bar" />
</form>
<p>
{123}
<span>
If you wait for tomorrow, tomorrow comes. If you wait for tomorrow,
tomorrow comes.
</span>
If you don't wait for tomorrow, <b>tomorrow comes.</b>
</p>
<div
dangerouslySetInnerHTML={{
__html: "<q><b> html content </b><b> html content </b></q>",
}}
/>
</main>
<hr />
<footer>
We're all in this alone. ๐ฑ
<small>We're all in this alone.</small>
</footer>
</>
);
const expected = (
dedent`<header style="margin-bottom: 0px; color: #333">
<h1>Hello World ๐</h1>
</header>
<hr />
<main class="main-section">
<x-async data-ms="50">
<x-async data-ms="30"></x-async>
<x-async data-ms="50"></x-async>
<x-async data-ms="50">
Sometimes in life, random things can blind-side you. ๐ธ๐ธ๐ธ
</x-async>
</x-async>
<form>
<input type="text" value="foo" />
<input type="checkbox" checked />
<br />
<textarea>bar</textarea>
</form>
<p>
123
<span>
If you wait for tomorrow, tomorrow comes. If you wait for tomorrow, tomorrow comes.
</span>
If you don't wait for tomorrow, ${""}
<b>tomorrow comes.</b>
</p>
<div>
<q><b> html content </b><b> html content </b></q>
</div>
</main>
<hr />
<footer>
We're all in this alone. ๐ฑ
<small>We're all in this alone.</small>
</footer>`
);
const html = await renderJSX(jsx);
assertEquals(html, expected);
});
Deno.test("jsx component with children", async () => {
const Form = ({ name }) => (
<form>
<button type="submit">
Click me, {name}!
</button>
</form>
);
assertEquals(
await renderJSX(<Form name="Le Foo" />),
dedent`<form>
<button type="submit">Click me, Le Foo!</button>
</form>`,
);
});
Deno.test("nested jsx children", async () => {
const Foo = ({ children }) => <>{children}</>;
const Bar = ({ children }) => <Foo>{children}</Foo>;
const Baz = ({ children }) => <Bar>{children}</Bar>;
assertEquals(
await renderJSX(<Baz>Le Foo</Baz>),
`Le Foo`,
);
});
Deno.test("handles style dimensions", async () => {
assertEquals(
await renderJSX(
<h1 style={{ margin: 10, padding: "2rem", zIndex: 2 }}>Le Foo</h1>,
),
`<h1 style="margin: 10px; padding: 2rem; z-index: 2">Le Foo</h1>`,
);
});
Deno.test("returns target element", async () => {
const SearchForm = ({ id }) => (
<form id={id}>
<label for="site-search">Search the site:</label>
<input
type="search"
id="site-search"
name="q"
aria-label="Search through site content"
/>
<button>Search</button>
</form>
);
const MainNav = () => (
<nav>
<SearchForm id="search-form" />
</nav>
);
const Page = () => (
<main>
<MainNav />
<article id="article-1" class="tech-news">
<h2>Example</h2>
<p>Test test test</p>
</article>
<article id="article-2" class="tech-news">
<h2>Example</h2>
<p>Test test test</p>
</article>
</main>
);
const expected = dedent`
<form id="search-form">
<label for="site-search">Search the site:</label>
<input type="search" id="site-search" name="q" aria-label="Search through site content" />
<button>Search</button>
</form>`;
const result = await renderJSX(<Page />, { targetElementId: "search-form" });
assertEquals(expected, result);
});
Deno.test("boolean attributes", async () => {
assertEquals(
await renderJSX(<input hidden />),
`<input hidden />`,
);
assertEquals(
await renderJSX(<input disabled="disabled" />),
`<input disabled />`,
);
assertEquals(
await renderJSX(<input checked={false} disabled={true} />),
`<input disabled />`,
);
assertEquals(
await renderJSX(<input checked disabled={false} />),
`<input checked />`,
);
});
Deno.test("undefined attributes", async () => {
assertEquals(
await renderJSX(<p class={undefined} />),
`<p></p>`,
);
assertEquals(
await renderJSX(<img alt={undefined} />),
`<img />`,
);
});