-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_htmx.py
146 lines (102 loc) · 4.44 KB
/
test_htmx.py
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
import unittest
from .htmxido import domx, HTMXError, render
def test_body_escapes_special_characters():
assert str(domx.div("&\"><")) == "<div>&"><</div>"
def test_deferred_content_style():
assert str(domx.div(class_="test", hxTarget="root")(
"test"
)) == '<div class="test" hx-target="root">test</div>'
def test_htmx_none_is_skipped():
assert str(domx.ul(
[
[None, None, None],
domx.li("Test")
]
)) == '<ul><li>Test</li></ul>'
def test_htmx_array_is_flattened():
assert str(domx.ul(
[
[domx.li(f"Name {i}") for i in range(1, 3)]
]
)) == '<ul><li>Name 1</li><li>Name 2</li></ul>'
def test_htmx_generator():
assert str(domx.ul(
domx.li(f"Name {i}") for i in range(1, 3)
)) == '<ul><li>Name 1</li><li>Name 2</li></ul>'
def test_keywords_are_attributed_properly():
assert str(domx.div(
"Test",
class_="large"
)) == '<div class="large">Test</div>'
assert str(domx.div(
"Test",
with_="large"
)) == '<div with="large">Test</div>'
def test_htmx_processor_handles_camel_and_snake_case():
assert str(domx.p("Another text", hx_target="foo")) == '<p hx-target="foo">Another text</p>'
assert str(domx.a("Click", hx_push_url="foo")) == '<a hx-push-url="foo">Click</a>'
assert str(domx.p("Another text", hxTarget="foo")) == '<p hx-target="foo">Another text</p>'
assert str(domx.a("Click", hxPushUrl="foo")) == '<a hx-push-url="foo">Click</a>'
assert str(domx.p(
domx.span("Hello", hxTest="1"),
domx.span("World", hxTest="2"),
hxTest="3"
)) == '<p hx-test="3"><span hx-test="1">Hello</span><span hx-test="2">World</span></p>'
def test_none_attribute():
assert str(domx.button(disabled=None)) == '<button disabled></button>'
def test_empty_tag_close():
assert str(domx.script(src="test.js")) == '<script src="test.js"></script>'
def test_script_content_is_default_raw():
assert str(domx.script("window.alert('Hello')")) == "<script>window.alert('Hello')</script>"
def test_explicit_raw():
assert str(domx.div(domx.raw("<Test>"))) == "<div><Test></div>"
class ExceptionTests(unittest.TestCase):
def test_already_added_content_throws(self):
with self.assertRaises(HTMXError) as ctx:
domx.div("Content", class_="test")("This should cause throw")
self.assertTrue("Content mutation not allowed" in str(ctx.exception))
def test_entity_tags_wont_allow_content(self):
with self.assertRaises(HTMXError) as ctx:
domx.img(class_="test")("This should cause throw")
self.assertTrue("img can not have content body" in str(ctx.exception))
def test_bad_content_type_throws(self):
with self.assertRaises(HTMXError) as ctx:
str(domx.div(9.0))
self.assertTrue("Unknown type for variable" in str(ctx.exception))
class RenderTests(unittest.TestCase):
def test_render_yields_full_html(self):
assert [x for x in render(domx.div(
domx.ul(
domx.li("one"),
domx.li("two"),
)
))] == ["<div><ul><li>one</li><li>two</li></ul></div>"]
def test_render_with_limit_size_yeilds_chunks(self):
buffers = [x for x in render(domx.div(
domx.ul(
domx.li(f"Item {i}") for i in range(1, 9999)
)
), buffer_size=512)]
for b in buffers:
assert len(b) <= 512
def test_invalid_buffer(self):
with self.assertRaises(HTMXError) as ctx:
[x for x in render(domx.div("This should cause throw"), buffer_size='99')]
self.assertTrue("Invalid buffer size 99")
with self.assertRaises(HTMXError) as ctx:
[x for x in render(domx.div("This should cause throw"), buffer_size=-1)]
self.assertTrue("Invalid buffer size")
def test_example():
x = domx
example_dom = x.div(id="root",
class_="bg-white dark:bg-slate-800 rounded-lg px-6 py-8 ring-1 ring-slate-900/5 shadow-xl")(
x.h1(hxIndicator="#spinner")('Hello World'),
x.p('You can have nested elements like this:'),
x.ul(
x.li(f"Item {i}") for i in range(1, 3)
),
x.div('Not only that but also you can have HTMX attributes'),
x.button(hxTarget="rubber-band")('Click me'),
x.img(id="spinner", src="loading.gif", class_="spinner-animation")
)
print(str(example_dom))