test_input.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. app = rx.App(state=State)
  14. @app.add_page
  15. def index():
  16. return rx.fragment(
  17. rx.input(
  18. id="debounce_input_input",
  19. on_change=State.set_text, # type: ignore
  20. value=State.text,
  21. ),
  22. rx.input(value=State.text, id="value_input"),
  23. rx.input(on_change=State.set_text, id="on_change_input"), # type: ignore
  24. rx.button("CLEAR", on_click=rx.set_value("on_change_input", "")),
  25. )
  26. app.compile()
  27. @pytest.fixture()
  28. def fully_controlled_input(tmp_path) -> Generator[AppHarness, None, None]:
  29. """Start FullyControlledInput app at tmp_path via AppHarness.
  30. Args:
  31. tmp_path: pytest tmp_path fixture
  32. Yields:
  33. running AppHarness instance
  34. """
  35. with AppHarness.create(
  36. root=tmp_path,
  37. app_source=FullyControlledInput, # type: ignore
  38. ) as harness:
  39. yield harness
  40. @pytest.mark.asyncio
  41. async def test_fully_controlled_input(fully_controlled_input: AppHarness):
  42. """Type text after moving cursor. Update text on backend.
  43. Args:
  44. fully_controlled_input: harness for FullyControlledInput app
  45. """
  46. assert fully_controlled_input.app_instance is not None, "app is not running"
  47. driver = fully_controlled_input.frontend()
  48. # get a reference to the connected client
  49. assert len(fully_controlled_input.poll_for_clients()) == 1
  50. token, backend_state = list(
  51. fully_controlled_input.app_instance.state_manager.states.items()
  52. )[0]
  53. # find the input and wait for it to have the initial state value
  54. debounce_input = driver.find_element(By.ID, "debounce_input_input")
  55. value_input = driver.find_element(By.ID, "value_input")
  56. on_change_input = driver.find_element(By.ID, "on_change_input")
  57. clear_button = driver.find_element(By.TAG_NAME, "button")
  58. assert fully_controlled_input.poll_for_value(debounce_input) == "initial"
  59. assert fully_controlled_input.poll_for_value(value_input) == "initial"
  60. # move cursor to home, then to the right and type characters
  61. debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT)
  62. debounce_input.send_keys("foo")
  63. time.sleep(0.5)
  64. assert debounce_input.get_attribute("value") == "ifoonitial"
  65. assert backend_state.text == "ifoonitial"
  66. assert fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
  67. # clear the input on the backend
  68. backend_state.text = ""
  69. fully_controlled_input.app_instance.state_manager.set_state(token, backend_state)
  70. await fully_controlled_input.emit_state_updates()
  71. assert backend_state.text == ""
  72. assert (
  73. fully_controlled_input.poll_for_value(
  74. debounce_input, exp_not_equal="ifoonitial"
  75. )
  76. == ""
  77. )
  78. # type more characters
  79. debounce_input.send_keys("getting testing done")
  80. time.sleep(0.5)
  81. assert debounce_input.get_attribute("value") == "getting testing done"
  82. assert backend_state.text == "getting testing done"
  83. assert fully_controlled_input.poll_for_value(value_input) == "getting testing done"
  84. # type into the on_change input
  85. on_change_input.send_keys("overwrite the state")
  86. time.sleep(0.5)
  87. assert debounce_input.get_attribute("value") == "overwrite the state"
  88. assert on_change_input.get_attribute("value") == "overwrite the state"
  89. assert backend_state.text == "overwrite the state"
  90. assert fully_controlled_input.poll_for_value(value_input) == "overwrite the state"
  91. clear_button.click()
  92. time.sleep(0.5)
  93. assert on_change_input.get_attribute("value") == ""
  94. # potential bug: clearing the on_change field doesn't itself trigger on_change
  95. # assert backend_state.text == ""
  96. # assert debounce_input.get_attribute("value") == ""
  97. # assert value_input.get_attribute("value") == ""