test_input.py 5.4 KB

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