6
6
7
7
import idom
8
8
from idom .backend .utils import find_available_port
9
- from idom .testing import BackendFixture , DisplayFixture
9
+ from idom .testing import BackendFixture , DisplayFixture , poll
10
10
from tests .tooling .common import DEFAULT_TYPE_DELAY
11
+ from tests .tooling .hooks import use_counter
11
12
12
13
13
14
JS_DIR = Path (__file__ ).parent / "js"
@@ -21,45 +22,53 @@ async def test_automatic_reconnect(browser: Browser):
21
22
page .set_default_timeout (10000 )
22
23
23
24
@idom .component
24
- def OldComponent ():
25
- return idom .html .p ("old" , id = "old-component" )
25
+ def SomeComponent ():
26
+ count , incr_count = use_counter (0 )
27
+ return idom .html ._ (
28
+ idom .html .p ("count" , count , data_count = count , id = "count" ),
29
+ idom .html .button ("incr" , on_click = lambda e : incr_count (), id = "incr" ),
30
+ )
26
31
27
32
async with AsyncExitStack () as exit_stack :
28
33
server = await exit_stack .enter_async_context (BackendFixture (port = port ))
29
34
display = await exit_stack .enter_async_context (
30
35
DisplayFixture (server , driver = page )
31
36
)
32
37
33
- await display .show (OldComponent )
38
+ await display .show (SomeComponent )
34
39
35
- # ensure the element is displayed before stopping the server
36
- await page .wait_for_selector ("#old-component" )
37
-
38
- # the server is disconnected but the last view state is still shown
39
- await page .wait_for_selector ("#old-component" )
40
+ count = await page .wait_for_selector ("#count" )
41
+ incr = await page .wait_for_selector ("#incr" )
40
42
41
- set_state = idom .Ref (None )
43
+ for i in range (3 ):
44
+ assert (await count .get_attribute ("data-count" )) == str (i )
45
+ await incr .click ()
42
46
43
- @idom .component
44
- def NewComponent ():
45
- state , set_state .current = idom .hooks .use_state (0 )
46
- return idom .html .p (f"new-{ state } " , id = f"new-component-{ state } " )
47
+ # the server is disconnected but the last view state is still shown
48
+ await page .wait_for_selector ("#count" )
47
49
48
50
async with AsyncExitStack () as exit_stack :
49
51
server = await exit_stack .enter_async_context (BackendFixture (port = port ))
50
52
display = await exit_stack .enter_async_context (
51
53
DisplayFixture (server , driver = page )
52
54
)
53
55
54
- await display .show (NewComponent )
56
+ # use mount instead of show to avoid a page refesh
57
+ display .backend .mount (SomeComponent )
58
+
59
+ async def get_count ():
60
+ # need to refetch element because may unmount on reconnect
61
+ count = await page .wait_for_selector ("#count" )
62
+ return await count .get_attribute ("data-count" )
63
+
64
+ for i in range (3 ):
65
+ # it may take a moment for the websocket to reconnect so need to poll
66
+ await poll (get_count ).until_equals (str (i ))
55
67
56
- # Note the lack of a page refresh before looking up this new component. The
57
- # client should attempt to reconnect and display the new view automatically.
58
- await page .wait_for_selector ("#new-component-0" )
68
+ # need to refetch element because may unmount on reconnect
69
+ incr = await page .wait_for_selector ("#incr" )
59
70
60
- # check that we can resume normal operation
61
- set_state .current (1 )
62
- await page .wait_for_selector ("#new-component-1" )
71
+ await incr .click ()
63
72
64
73
65
74
async def test_style_can_be_changed (display : DisplayFixture ):
0 commit comments