generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
170 lines (156 loc) · 3.96 KB
/
test.ts
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
import { assertEquals, assertThrows } from "@std/assert";
import { generateTag, sanitize, tag, tagNoVoid, tagVoid } from "./mod.ts";
Deno.test("render tag", () => {
assertEquals(
tag("div", { id: "foo", class: "bar" }, "Hello world!"),
`<div id="foo" class="bar">Hello world!</div>`,
);
assertEquals(
tag("my-tag", { class: "bar" }, "My own tag!"),
`<my-tag class="bar">My own tag!</my-tag>`,
);
});
Deno.test("render void tag", () => {
assertEquals(
tag("meta", { charset: "utf-8" }),
`<meta charset="utf-8">`,
);
assertEquals(
tag("input", {
type: "text",
name: "name",
placeholder: "input your name",
}),
`<input type="text" name="name" placeholder="input your name">`,
);
});
Deno.test("render tag without attributes", () => {
assertEquals(
tag("div", "No attributes!"),
`<div>No attributes!</div>`,
);
assertEquals(
tag("br"),
`<br>`,
);
});
Deno.test("render nested tag", () => {
assertEquals(
tag(
"a",
{ href: "https://deno.land" },
tag("img", { src: "https://deno.land/logo.svg" }),
),
`<a href="https://deno.land"><img src="https://deno.land/logo.svg"></a>`,
);
assertEquals(
tag(
"ul",
{ class: "nav" },
tag("li", "first element"),
tag("li", "second element"),
),
`<ul class="nav"><li>first element</li><li>second element</li></ul>`,
);
assertEquals(
tag(
"div",
{ class: "container" },
tag(
"article",
{ class: "post" },
tag("p", "awesome text"),
),
),
`<div class="container"><article class="post"><p>awesome text</p></article></div>`,
);
});
Deno.test("render svg tag", () => {
assertEquals(
tag(
"svg",
{ width: 100, height: 100 },
tag("rect", { x: 20, y: 20, fill: "#a33" }),
),
`<svg width="100" height="100"><rect x="20" y="20" fill="#a33"></rect></svg>`,
);
});
Deno.test("render boolean attributes", () => {
assertEquals(
tag("button", { type: "button", disabled: false }, "enabled"),
`<button type="button">enabled</button>`,
);
assertEquals(
tag("button", { type: "button", disabled: true }, "disabled"),
`<button type="button" disabled>disabled</button>`,
);
});
Deno.test("throw error when tag is empty", () => {
assertThrows(
() => {
tag("");
},
Error,
"tagName is empty.",
);
});
Deno.test("throw error when tag has whitespace characters", () => {
assertThrows(
() => {
tag("separate tag", { id: "foo" }, "Hello world!");
},
Error,
"tagName has whitespace characters.",
);
});
Deno.test("always add closing tag", () => {
assertEquals(
tagNoVoid("link", "http://example.com"),
`<link>http://example.com</link>`,
);
});
Deno.test("always add closing tag", () => {
assertEquals(
tagVoid("div", { class: "red" }),
`<div class="red">`,
);
});
Deno.test("generate tag", () => {
const div = generateTag("div");
assertEquals(
div({ id: "foo", class: "bar" }, "Hello world!"),
`<div id="foo" class="bar">Hello world!</div>`,
);
});
Deno.test("generate tagNoVoid", () => {
const link = generateTag("link", "noVoid");
assertEquals(
link("http://example.com"),
`<link>http://example.com</link>`,
);
});
Deno.test("generate tagVoid", () => {
const span = generateTag("span", "void");
assertEquals(
span({ class: "red" }),
`<span class="red">`,
);
});
Deno.test("sanitize string", () => {
assertEquals(
sanitize(`<img src="https://www.example.com?width=10&height=10">`),
"<img src="https://www.example.com?width=10&height=10">",
);
// empty inputs
assertEquals(sanitize(), "");
});
Deno.test("ignore to sanitize", () => {
assertEquals(
sanitize(`<span>"ok" & ng</span>`, { lt: false, gt: false }),
`<span>"ok" & ng</span>`,
);
assertEquals(
sanitize(`<span>"ok" & ng</span>`, { amp: false, quot: false }),
`<span>"ok" & ng</span>`,
);
});