|
1 | 1 | import pytest
|
| 2 | +from playwright.async_api import expect |
2 | 3 |
|
3 |
| -from reactpy import component, config, html |
| 4 | +from reactpy import component, config, hooks, html |
4 | 5 | from reactpy.testing import DisplayFixture, poll
|
5 | 6 | from reactpy.utils import Ref
|
| 7 | +from tests.tooling.common import DEFAULT_TYPE_DELAY |
6 | 8 | from tests.tooling.hooks import use_counter
|
7 | 9 |
|
8 | 10 |
|
9 | 11 | async def test_script_re_run_on_content_change(display: DisplayFixture):
|
10 |
| - incr_count = Ref() |
11 |
| - |
12 | 12 | @component
|
13 | 13 | 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 | + |
15 | 19 | return html.div(
|
16 | 20 | html.div({"id": "mount-count", "data_value": 0}),
|
17 | 21 | html.script(
|
18 | 22 | f'document.getElementById("mount-count").setAttribute("data-value", {count});'
|
19 | 23 | ),
|
| 24 | + html.button({"onClick": on_click, "id": "incr"}, "Increment"), |
20 | 25 | )
|
21 | 26 |
|
22 | 27 | await display.show(HasScript)
|
23 | 28 |
|
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 | + ) |
26 | 41 |
|
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 | + ) |
32 | 46 |
|
33 | 47 |
|
34 | 48 | async def test_script_from_src(display: DisplayFixture):
|
|
0 commit comments