forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
98 lines (80 loc) · 2.35 KB
/
test_utils.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
import pytest
import idom
from idom.utils import html_to_vdom
def test_basic_ref_behavior():
r = idom.Ref(1)
assert r.current == 1
r.current = 2
assert r.current == 2
assert r.set_current(3) == 2
assert r.current == 3
r = idom.Ref()
with pytest.raises(AttributeError):
r.current
r.current = 4
assert r.current == 4
def test_ref_equivalence():
assert idom.Ref([1, 2, 3]) == idom.Ref([1, 2, 3])
assert idom.Ref([1, 2, 3]) != idom.Ref([1, 2])
assert idom.Ref([1, 2, 3]) != [1, 2, 3]
assert idom.Ref() != idom.Ref()
assert idom.Ref() != idom.Ref(1)
def test_ref_repr():
assert repr(idom.Ref([1, 2, 3])) == "Ref([1, 2, 3])"
assert repr(idom.Ref()) == "Ref(<undefined>)"
@pytest.mark.parametrize(
"case",
[
{"source": "<div/>", "model": {"tagName": "div"}},
{
"source": "<div style='background-color:blue'/>",
"model": {
"tagName": "div",
"attributes": {"style": {"backgroundColor": "blue"}},
},
},
{
"source": "<div>Hello!</div>",
"model": {"tagName": "div", "children": ["Hello!"]},
},
{
"source": "<div>Hello!<p>World!</p></div>",
"model": {
"tagName": "div",
"children": ["Hello!", {"tagName": "p", "children": ["World!"]}],
},
},
],
)
def test_html_to_vdom(case):
assert html_to_vdom(case["source"]) == {
"tagName": "div",
"children": [case["model"]],
}
def test_html_to_vdom_transform():
source = "<p>hello <a>world</a> and <a>universe</a></p>"
def make_links_blue(node):
if node["tagName"] == "a":
node["attributes"]["style"] = {"color": "blue"}
return node
expected = {
"tagName": "p",
"children": [
"hello ",
{
"tagName": "a",
"children": ["world"],
"attributes": {"style": {"color": "blue"}},
},
" and ",
{
"tagName": "a",
"children": ["universe"],
"attributes": {"style": {"color": "blue"}},
},
],
}
assert html_to_vdom(source, make_links_blue) == {
"tagName": "div",
"children": [expected],
}