|
| 1 | +import asyncio |
| 2 | +import time |
1 | 3 | from pathlib import Path |
2 | 4 |
|
3 | 5 | import idom |
4 | 6 | from idom.testing import ServerMountPoint |
| 7 | +from tests.driver_utils import send_keys |
5 | 8 |
|
6 | 9 |
|
7 | 10 | JS_DIR = Path(__file__).parent / "js" |
@@ -71,14 +74,44 @@ def ButtonWithChangingColor(): |
71 | 74 |
|
72 | 75 | button = driver.find_element("id", "my-button") |
73 | 76 |
|
74 | | - assert get_style(button)["background-color"] == "red" |
| 77 | + assert _get_style(button)["background-color"] == "red" |
75 | 78 |
|
76 | 79 | for color in ["blue", "red"] * 2: |
77 | 80 | button.click() |
78 | | - driver_wait.until(lambda _: get_style(button)["background-color"] == color) |
| 81 | + driver_wait.until(lambda _: _get_style(button)["background-color"] == color) |
79 | 82 |
|
80 | 83 |
|
81 | | -def get_style(element): |
| 84 | +def _get_style(element): |
82 | 85 | items = element.get_attribute("style").split(";") |
83 | 86 | pairs = [item.split(":", 1) for item in map(str.strip, items) if item] |
84 | 87 | return {key.strip(): value.strip() for key, value in pairs} |
| 88 | + |
| 89 | + |
| 90 | +def test_slow_server_response_on_input_change(display, driver, driver_wait): |
| 91 | + """A delay server-side could cause input values to be overwritten. |
| 92 | +
|
| 93 | + For more info see: https://github.com/idom-team/idom/issues/684 |
| 94 | + """ |
| 95 | + |
| 96 | + delay = 0.2 |
| 97 | + |
| 98 | + @idom.component |
| 99 | + def SomeComponent(): |
| 100 | + value, set_value = idom.hooks.use_state("") |
| 101 | + |
| 102 | + async def handle_change(event): |
| 103 | + await asyncio.sleep(delay) |
| 104 | + set_value(event["target"]["value"]) |
| 105 | + |
| 106 | + return idom.html.input({"onChange": handle_change, "id": "test-input"}) |
| 107 | + |
| 108 | + display(SomeComponent) |
| 109 | + |
| 110 | + inp = driver.find_element("id", "test-input") |
| 111 | + |
| 112 | + text = "hello" |
| 113 | + send_keys(inp, text) |
| 114 | + |
| 115 | + time.sleep(delay * len(text) * 1.1) |
| 116 | + |
| 117 | + driver_wait.until(lambda _: inp.get_attribute("value") == "hello") |
0 commit comments