Skip to content

Commit 5182c83

Browse files
committed
Fix flakey test_script_re_run_on_content_change
1 parent 2959d9b commit 5182c83

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

Diff for: tests/test_html.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,48 @@
11
import pytest
2+
from playwright.async_api import expect
23

3-
from reactpy import component, config, html
4+
from reactpy import component, config, hooks, html
45
from reactpy.testing import DisplayFixture, poll
56
from reactpy.utils import Ref
7+
from tests.tooling.common import DEFAULT_TYPE_DELAY
68
from tests.tooling.hooks import use_counter
79

810

911
async def test_script_re_run_on_content_change(display: DisplayFixture):
10-
incr_count = Ref()
11-
1212
@component
1313
def HasScript():
14-
count, incr_count.current = use_counter(1)
14+
count, set_count = hooks.use_state(0)
15+
16+
def on_click(event):
17+
set_count(count + 1)
18+
1519
return html.div(
1620
html.div({"id": "mount-count", "data_value": 0}),
1721
html.script(
1822
f'document.getElementById("mount-count").setAttribute("data-value", {count});'
1923
),
24+
html.button({"onClick": on_click, "id": "incr"}, "Increment"),
2025
)
2126

2227
await display.show(HasScript)
2328

24-
mount_count = await display.page.wait_for_selector("#mount-count", state="attached")
25-
poll_mount_count = poll(mount_count.get_attribute, "data-value")
29+
await display.page.wait_for_selector("#mount-count", state="attached")
30+
button = await display.page.wait_for_selector("#incr", state="attached")
31+
32+
await button.click(delay=DEFAULT_TYPE_DELAY)
33+
await expect(display.page.locator("#mount-count")).to_have_attribute(
34+
"data-value", "1"
35+
)
36+
37+
await button.click(delay=DEFAULT_TYPE_DELAY)
38+
await expect(display.page.locator("#mount-count")).to_have_attribute(
39+
"data-value", "2"
40+
)
2641

27-
await poll_mount_count.until_equals("1")
28-
incr_count.current()
29-
await poll_mount_count.until_equals("2")
30-
incr_count.current()
31-
await poll_mount_count.until_equals("3")
42+
await button.click(delay=DEFAULT_TYPE_DELAY)
43+
await expect(display.page.locator("#mount-count")).to_have_attribute(
44+
"data-value", "3", timeout=100000
45+
)
3246

3347

3448
async def test_script_from_src(display: DisplayFixture):

Diff for: tests/tooling/hooks.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def use_toggle(init=False):
1010
return state, lambda: set_state(lambda old: not old)
1111

1212

13+
# TODO: Remove this
1314
def use_counter(initial_value):
1415
state, set_state = use_state(initial_value)
1516
return state, lambda: set_state(lambda old: old + 1)

0 commit comments

Comments
 (0)