66
77import idom
88from idom .backend .utils import find_available_port
9- from idom .testing import BackendFixture , DisplayFixture
9+ from idom .testing import BackendFixture , DisplayFixture , poll
1010from tests .tooling .common import DEFAULT_TYPE_DELAY
11+ from tests .tooling .hooks import use_counter
1112
1213
1314JS_DIR = Path (__file__ ).parent / "js"
@@ -21,45 +22,53 @@ async def test_automatic_reconnect(browser: Browser):
2122 page .set_default_timeout (10000 )
2223
2324 @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+ )
2631
2732 async with AsyncExitStack () as exit_stack :
2833 server = await exit_stack .enter_async_context (BackendFixture (port = port ))
2934 display = await exit_stack .enter_async_context (
3035 DisplayFixture (server , driver = page )
3136 )
3237
33- await display .show (OldComponent )
38+ await display .show (SomeComponent )
3439
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" )
4042
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 ()
4246
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" )
4749
4850 async with AsyncExitStack () as exit_stack :
4951 server = await exit_stack .enter_async_context (BackendFixture (port = port ))
5052 display = await exit_stack .enter_async_context (
5153 DisplayFixture (server , driver = page )
5254 )
5355
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 ))
5567
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" )
5970
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 ()
6372
6473
6574async def test_style_can_be_changed (display : DisplayFixture ):
0 commit comments