-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_components.py
More file actions
300 lines (218 loc) · 8.04 KB
/
test_components.py
File metadata and controls
300 lines (218 loc) · 8.04 KB
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
"""Unit tests for the built-in element-creating functions."""
from pythonnative.components import (
ActivityIndicator,
Button,
Column,
FlatList,
Image,
Modal,
Pressable,
ProgressBar,
Row,
SafeAreaView,
ScrollView,
Slider,
Spacer,
Switch,
Text,
TextInput,
View,
WebView,
)
# ---------------------------------------------------------------------------
# Text
# ---------------------------------------------------------------------------
def test_text_defaults() -> None:
el = Text()
assert el.type == "Text"
assert el.props.get("text", "") == ""
assert el.children == []
def test_text_with_props() -> None:
el = Text("Hello", font_size=18, color="#FF0000", bold=True, text_align="center")
assert el.props["text"] == "Hello"
assert el.props["font_size"] == 18
assert el.props["color"] == "#FF0000"
assert el.props["bold"] is True
assert el.props["text_align"] == "center"
def test_text_none_props_excluded() -> None:
el = Text("Hi")
assert "font_size" not in el.props
assert "color" not in el.props
def test_text_layout_props() -> None:
el = Text("Hi", width=100, height=50, flex=1, margin=8, align_self="center")
assert el.props["width"] == 100
assert el.props["height"] == 50
assert el.props["flex"] == 1
assert el.props["margin"] == 8
assert el.props["align_self"] == "center"
# ---------------------------------------------------------------------------
# Button
# ---------------------------------------------------------------------------
def test_button_defaults() -> None:
el = Button()
assert el.type == "Button"
assert el.props["title"] == ""
assert "on_click" not in el.props
def test_button_with_callback() -> None:
cb = lambda: None # noqa: E731
el = Button("Tap", on_click=cb, background_color="#123456")
assert el.props["title"] == "Tap"
assert el.props["on_click"] is cb
assert el.props["background_color"] == "#123456"
def test_button_disabled() -> None:
el = Button("Off", enabled=False)
assert el.props["enabled"] is False
# ---------------------------------------------------------------------------
# Column / Row
# ---------------------------------------------------------------------------
def test_column_with_children() -> None:
el = Column(Text("a"), Text("b"), spacing=10, padding=16, alignment="fill")
assert el.type == "Column"
assert len(el.children) == 2
assert el.props["spacing"] == 10
assert el.props["padding"] == 16
assert el.props["alignment"] == "fill"
def test_row_with_children() -> None:
el = Row(Text("x"), Text("y"), spacing=5)
assert el.type == "Row"
assert len(el.children) == 2
assert el.props["spacing"] == 5
def test_column_no_spacing_omitted() -> None:
el = Column()
assert "spacing" not in el.props
def test_column_layout_props() -> None:
el = Column(flex=2, margin={"horizontal": 8})
assert el.props["flex"] == 2
assert el.props["margin"] == {"horizontal": 8}
# ---------------------------------------------------------------------------
# ScrollView
# ---------------------------------------------------------------------------
def test_scrollview_with_child() -> None:
child = Column(Text("a"))
el = ScrollView(child)
assert el.type == "ScrollView"
assert len(el.children) == 1
assert el.children[0] is child
def test_scrollview_empty() -> None:
el = ScrollView()
assert el.children == []
# ---------------------------------------------------------------------------
# TextInput
# ---------------------------------------------------------------------------
def test_textinput_defaults() -> None:
el = TextInput()
assert el.type == "TextInput"
assert el.props["value"] == ""
def test_textinput_with_props() -> None:
cb = lambda s: None # noqa: E731
el = TextInput(value="hi", placeholder="type...", on_change=cb, secure=True)
assert el.props["value"] == "hi"
assert el.props["placeholder"] == "type..."
assert el.props["on_change"] is cb
assert el.props["secure"] is True
# ---------------------------------------------------------------------------
# Other leaf components
# ---------------------------------------------------------------------------
def test_image() -> None:
el = Image("icon.png", width=48, height=48)
assert el.type == "Image"
assert el.props["source"] == "icon.png"
assert el.props["width"] == 48
def test_switch() -> None:
el = Switch(value=True)
assert el.type == "Switch"
assert el.props["value"] is True
def test_progress_bar() -> None:
el = ProgressBar(value=0.5)
assert el.type == "ProgressBar"
assert el.props["value"] == 0.5
def test_activity_indicator() -> None:
el = ActivityIndicator(animating=False)
assert el.type == "ActivityIndicator"
assert el.props["animating"] is False
def test_webview() -> None:
el = WebView(url="https://example.com")
assert el.type == "WebView"
assert el.props["url"] == "https://example.com"
def test_spacer() -> None:
el = Spacer(size=20)
assert el.type == "Spacer"
assert el.props["size"] == 20
def test_spacer_empty() -> None:
el = Spacer()
assert el.type == "Spacer"
assert el.props == {}
# ---------------------------------------------------------------------------
# Key support
# ---------------------------------------------------------------------------
def test_key_propagation() -> None:
el = Text("keyed", key="k1")
assert el.key == "k1"
def test_column_key() -> None:
el = Column(key="col-1")
assert el.key == "col-1"
# ---------------------------------------------------------------------------
# New components
# ---------------------------------------------------------------------------
def test_view_container() -> None:
child = Text("inside")
el = View(child, background_color="#FFF", padding=8, width=200)
assert el.type == "View"
assert len(el.children) == 1
assert el.props["background_color"] == "#FFF"
assert el.props["padding"] == 8
assert el.props["width"] == 200
def test_safe_area_view() -> None:
el = SafeAreaView(Text("safe"), background_color="#000")
assert el.type == "SafeAreaView"
assert len(el.children) == 1
def test_modal() -> None:
cb = lambda: None # noqa: E731
el = Modal(Text("content"), visible=True, on_dismiss=cb, title="Alert")
assert el.type == "Modal"
assert el.props["visible"] is True
assert el.props["on_dismiss"] is cb
assert el.props["title"] == "Alert"
assert len(el.children) == 1
def test_slider() -> None:
cb = lambda v: None # noqa: E731
el = Slider(value=0.5, min_value=0, max_value=10, on_change=cb)
assert el.type == "Slider"
assert el.props["value"] == 0.5
assert el.props["min_value"] == 0
assert el.props["max_value"] == 10
assert el.props["on_change"] is cb
def test_pressable() -> None:
cb = lambda: None # noqa: E731
child = Text("tap me")
el = Pressable(child, on_press=cb)
assert el.type == "Pressable"
assert el.props["on_press"] is cb
assert len(el.children) == 1
def test_flat_list_basic() -> None:
el = FlatList(
data=["a", "b", "c"],
render_item=lambda item, i: Text(item),
)
assert el.type == "ScrollView"
assert len(el.children) == 1
inner = el.children[0]
assert inner.type == "Column"
assert len(inner.children) == 3
assert inner.children[0].props["text"] == "a"
def test_flat_list_with_keys() -> None:
el = FlatList(
data=[{"id": "x", "name": "X"}, {"id": "y", "name": "Y"}],
render_item=lambda item, i: Text(item["name"]),
key_extractor=lambda item, i: item["id"],
)
inner = el.children[0]
assert inner.children[0].key == "x"
assert inner.children[1].key == "y"
def test_flat_list_empty() -> None:
el = FlatList(data=[], render_item=lambda item, i: Text(str(item)))
inner = el.children[0]
assert len(inner.children) == 0
def test_spacer_flex() -> None:
el = Spacer(flex=1)
assert el.props["flex"] == 1