forked from zauberzeug/nicegui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lifecycle.py
80 lines (59 loc) · 2.17 KB
/
test_lifecycle.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
from typing import List
from nicegui import Client, app, ui
from .screen import Screen
def test_adding_elements_during_onconnect_on_auto_index_page(screen: Screen):
connections = []
ui.label('Adding labels on_connect')
app.on_connect(lambda _: connections.append(ui.label(f'new connection {len(connections)}')))
screen.open('/')
screen.should_contain('new connection 0')
screen.open('/')
screen.should_contain('new connection 0')
screen.should_contain('new connection 1')
screen.open('/')
screen.should_contain('new connection 0')
screen.should_contain('new connection 1')
screen.should_contain('new connection 2')
def test_async_connect_handler(screen: Screen):
async def run_js():
result.text = await ui.run_javascript('41 + 1')
result = ui.label()
app.on_connect(run_js)
screen.open('/')
screen.should_contain('42')
def test_connect_disconnect_is_called_for_each_client(screen: Screen):
events: List[str] = []
@ui.page('/')
def page(client: Client):
ui.label(f'client id: {client.id}')
app.on_connect(lambda: events.append('connect'))
app.on_disconnect(lambda: events.append('disconnect'))
screen.open('/')
screen.wait(0.5)
screen.open('/')
screen.wait(0.5)
screen.open('/')
screen.wait(0.5)
assert events == ['connect', 'disconnect', 'connect', 'disconnect', 'connect']
def test_startup_and_shutdown_handlers(screen: Screen):
events: List[str] = []
def startup():
events.append('startup')
async def startup_async():
events.append('startup_async')
def shutdown():
events.append('shutdown')
async def shutdown_async():
events.append('shutdown_async')
app.on_startup(startup)
app.on_startup(startup_async)
app.on_startup(startup_async())
app.on_shutdown(shutdown)
app.on_shutdown(shutdown_async)
app.on_shutdown(shutdown_async())
screen.open('/')
screen.wait(0.5)
assert events == ['startup', 'startup_async', 'startup_async']
app.shutdown()
screen.wait(0.5)
assert events == ['startup', 'startup_async', 'startup_async', 'shutdown', 'shutdown_async', 'shutdown_async']