1
0

test_table.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Integration tests for table and related components."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from reflex.testing import AppHarness
  6. def Table():
  7. """App using table component."""
  8. from typing import List
  9. import reflex as rx
  10. class TableState(rx.State):
  11. rows: List[List[str]] = [
  12. ["John", "30", "New York"],
  13. ["Jane", "31", "San Fransisco"],
  14. ["Joe", "32", "Los Angeles"],
  15. ]
  16. headers: List[str] = ["Name", "Age", "Location"]
  17. footers: List[str] = ["footer1", "footer2", "footer3"]
  18. caption: str = "random caption"
  19. app = rx.App(state=TableState)
  20. @app.add_page
  21. def index():
  22. return rx.center(
  23. rx.input(
  24. id="token",
  25. value=TableState.router.session.client_token,
  26. is_read_only=True,
  27. ),
  28. rx.table_container(
  29. rx.table(
  30. headers=TableState.headers,
  31. rows=TableState.rows,
  32. footers=TableState.footers,
  33. caption=TableState.caption,
  34. variant="striped",
  35. color_scheme="blue",
  36. width="100%",
  37. ),
  38. ),
  39. )
  40. @app.add_page
  41. def another():
  42. return rx.center(
  43. rx.table_container(
  44. rx.table( # type: ignore
  45. rx.thead( # type: ignore
  46. rx.tr( # type: ignore
  47. rx.th("Name"),
  48. rx.th("Age"),
  49. rx.th("Location"),
  50. )
  51. ),
  52. rx.tbody( # type: ignore
  53. rx.tr( # type: ignore
  54. rx.td("John"),
  55. rx.td(30),
  56. rx.td("New York"),
  57. ),
  58. rx.tr( # type: ignore
  59. rx.td("Jane"),
  60. rx.td(31),
  61. rx.td("San Francisco"),
  62. ),
  63. rx.tr( # type: ignore
  64. rx.td("Joe"),
  65. rx.td(32),
  66. rx.td("Los Angeles"),
  67. ),
  68. ),
  69. rx.tfoot( # type: ignore
  70. rx.tr(rx.td("footer1"), rx.td("footer2"), rx.td("footer3")) # type: ignore
  71. ),
  72. rx.table_caption("random caption"),
  73. variant="striped",
  74. color_scheme="teal",
  75. )
  76. )
  77. )
  78. app.compile()
  79. @pytest.fixture()
  80. def table(tmp_path_factory) -> Generator[AppHarness, None, None]:
  81. """Start Table app at tmp_path via AppHarness.
  82. Args:
  83. tmp_path_factory: pytest tmp_path_factory fixture
  84. Yields:
  85. running AppHarness instance
  86. """
  87. with AppHarness.create(
  88. root=tmp_path_factory.mktemp("table"),
  89. app_source=Table, # type: ignore
  90. ) as harness:
  91. assert harness.app_instance is not None, "app is not running"
  92. yield harness
  93. @pytest.fixture
  94. def driver(table: AppHarness):
  95. """GEt an instance of the browser open to the table app.
  96. Args:
  97. table: harness for Table app
  98. Yields:
  99. WebDriver instance.
  100. """
  101. driver = table.frontend()
  102. try:
  103. token_input = driver.find_element(By.ID, "token")
  104. assert token_input
  105. # wait for the backend connection to send the token
  106. token = table.poll_for_value(token_input)
  107. assert token is not None
  108. yield driver
  109. finally:
  110. driver.quit()
  111. @pytest.mark.parametrize("route", ["", "/another"])
  112. def test_table(driver, table: AppHarness, route):
  113. """Test that a table component is rendered properly.
  114. Args:
  115. driver: Selenium WebDriver open to the app
  116. table: Harness for Table app
  117. route: Page route or path.
  118. """
  119. driver.get(f"{table.frontend_url}/{route}")
  120. assert table.app_instance is not None, "app is not running"
  121. thead = driver.find_element(By.TAG_NAME, "thead")
  122. # poll till page is fully loaded.
  123. table.poll_for_content(element=thead)
  124. # check headers
  125. assert thead.find_element(By.TAG_NAME, "tr").text == "NAME AGE LOCATION"
  126. # check first row value
  127. assert (
  128. driver.find_element(By.TAG_NAME, "tbody")
  129. .find_elements(By.TAG_NAME, "tr")[0]
  130. .text
  131. == "John 30 New York"
  132. )
  133. # check footer
  134. assert (
  135. driver.find_element(By.TAG_NAME, "tfoot")
  136. .find_element(By.TAG_NAME, "tr")
  137. .text.lower()
  138. == "footer1 footer2 footer3"
  139. )
  140. # check caption
  141. assert driver.find_element(By.TAG_NAME, "caption").text == "random caption"