test_input: replace sleep with _poll_for (#2677)

Reduce test flakiness and improve runtime in optimal case
This commit is contained in:
Masen Furer 2024-02-21 20:12:31 -08:00 committed by GitHub
parent 24ccb2a2aa
commit 789d50e0e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,4 @@
"""Integration tests for text input and related components."""
import time
from typing import Generator
import pytest
@ -97,10 +96,11 @@ async def test_fully_controlled_input(fully_controlled_input: AppHarness):
# move cursor to home, then to the right and type characters
debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT)
debounce_input.send_keys("foo")
time.sleep(0.5)
assert AppHarness._poll_for(
lambda: fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
)
assert debounce_input.get_attribute("value") == "ifoonitial"
assert await get_state_text() == "ifoonitial"
assert fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
assert fully_controlled_input.poll_for_value(plain_value_input) == "ifoonitial"
# clear the input on the backend
@ -116,10 +116,12 @@ async def test_fully_controlled_input(fully_controlled_input: AppHarness):
# type more characters
debounce_input.send_keys("getting testing done")
time.sleep(0.5)
assert AppHarness._poll_for(
lambda: fully_controlled_input.poll_for_value(value_input)
== "getting testing done"
)
assert debounce_input.get_attribute("value") == "getting testing done"
assert await get_state_text() == "getting testing done"
assert fully_controlled_input.poll_for_value(value_input) == "getting testing done"
assert (
fully_controlled_input.poll_for_value(plain_value_input)
== "getting testing done"
@ -127,19 +129,20 @@ async def test_fully_controlled_input(fully_controlled_input: AppHarness):
# type into the on_change input
on_change_input.send_keys("overwrite the state")
time.sleep(0.5)
assert AppHarness._poll_for(
lambda: fully_controlled_input.poll_for_value(value_input)
== "overwrite the state"
)
assert debounce_input.get_attribute("value") == "overwrite the state"
assert on_change_input.get_attribute("value") == "overwrite the state"
assert await get_state_text() == "overwrite the state"
assert fully_controlled_input.poll_for_value(value_input) == "overwrite the state"
assert (
fully_controlled_input.poll_for_value(plain_value_input)
== "overwrite the state"
)
clear_button.click()
time.sleep(0.5)
assert on_change_input.get_attribute("value") == ""
assert AppHarness._poll_for(lambda: on_change_input.get_attribute("value") == "")
# potential bug: clearing the on_change field doesn't itself trigger on_change
# assert backend_state.text == ""
# assert debounce_input.get_attribute("value") == ""