test_input.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Integration tests for text input and related components."""
  2. import time
  3. from typing import Generator
  4. import pytest
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.common.keys import Keys
  7. from reflex.testing import AppHarness
  8. def FullyControlledInput():
  9. """App using a fully controlled input with implicit debounce wrapper."""
  10. import reflex as rx
  11. class State(rx.State):
  12. text: str = "initial"
  13. @rx.var
  14. def token(self) -> str:
  15. return self.get_token()
  16. app = rx.App(state=State)
  17. @app.add_page
  18. def index():
  19. return rx.fragment(
  20. rx.input(value=State.token, is_read_only=True, id="token"),
  21. rx.input(
  22. id="debounce_input_input",
  23. on_change=State.set_text, # type: ignore
  24. value=State.text,
  25. ),
  26. rx.input(value=State.text, id="value_input"),
  27. rx.input(on_change=State.set_text, id="on_change_input"), # type: ignore
  28. rx.button("CLEAR", on_click=rx.set_value("on_change_input", "")),
  29. )
  30. app.compile()
  31. @pytest.fixture()
  32. def fully_controlled_input(tmp_path) -> Generator[AppHarness, None, None]:
  33. """Start FullyControlledInput app at tmp_path via AppHarness.
  34. Args:
  35. tmp_path: pytest tmp_path fixture
  36. Yields:
  37. running AppHarness instance
  38. """
  39. with AppHarness.create(
  40. root=tmp_path,
  41. app_source=FullyControlledInput, # type: ignore
  42. ) as harness:
  43. yield harness
  44. @pytest.mark.asyncio
  45. async def test_fully_controlled_input(fully_controlled_input: AppHarness):
  46. """Type text after moving cursor. Update text on backend.
  47. Args:
  48. fully_controlled_input: harness for FullyControlledInput app
  49. """
  50. assert fully_controlled_input.app_instance is not None, "app is not running"
  51. driver = fully_controlled_input.frontend()
  52. # get a reference to the connected client
  53. token_input = driver.find_element(By.ID, "token")
  54. assert token_input
  55. # wait for the backend connection to send the token
  56. token = fully_controlled_input.poll_for_value(token_input)
  57. assert token
  58. # find the input and wait for it to have the initial state value
  59. debounce_input = driver.find_element(By.ID, "debounce_input_input")
  60. value_input = driver.find_element(By.ID, "value_input")
  61. on_change_input = driver.find_element(By.ID, "on_change_input")
  62. clear_button = driver.find_element(By.TAG_NAME, "button")
  63. assert fully_controlled_input.poll_for_value(debounce_input) == "initial"
  64. assert fully_controlled_input.poll_for_value(value_input) == "initial"
  65. # move cursor to home, then to the right and type characters
  66. debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT)
  67. debounce_input.send_keys("foo")
  68. time.sleep(0.5)
  69. assert debounce_input.get_attribute("value") == "ifoonitial"
  70. assert (await fully_controlled_input.get_state(token)).text == "ifoonitial"
  71. assert fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
  72. # clear the input on the backend
  73. async with fully_controlled_input.modify_state(token) as state:
  74. state.text = ""
  75. assert (await fully_controlled_input.get_state(token)).text == ""
  76. assert (
  77. fully_controlled_input.poll_for_value(
  78. debounce_input, exp_not_equal="ifoonitial"
  79. )
  80. == ""
  81. )
  82. # type more characters
  83. debounce_input.send_keys("getting testing done")
  84. time.sleep(0.5)
  85. assert debounce_input.get_attribute("value") == "getting testing done"
  86. assert (
  87. await fully_controlled_input.get_state(token)
  88. ).text == "getting testing done"
  89. assert fully_controlled_input.poll_for_value(value_input) == "getting testing done"
  90. # type into the on_change input
  91. on_change_input.send_keys("overwrite the state")
  92. time.sleep(0.5)
  93. assert debounce_input.get_attribute("value") == "overwrite the state"
  94. assert on_change_input.get_attribute("value") == "overwrite the state"
  95. assert (await fully_controlled_input.get_state(token)).text == "overwrite the state"
  96. assert fully_controlled_input.poll_for_value(value_input) == "overwrite the state"
  97. clear_button.click()
  98. time.sleep(0.5)
  99. assert on_change_input.get_attribute("value") == ""
  100. # potential bug: clearing the on_change field doesn't itself trigger on_change
  101. # assert backend_state.text == ""
  102. # assert debounce_input.get_attribute("value") == ""
  103. # assert value_input.get_attribute("value") == ""