test_table.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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=rx.State)
  20. @app.add_page
  21. def index():
  22. return rx.center(
  23. rx.chakra.input(
  24. id="token",
  25. value=TableState.router.session.client_token,
  26. is_read_only=True,
  27. ),
  28. rx.chakra.table_container(
  29. rx.chakra.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.chakra.table_container(
  44. rx.chakra.table( # type: ignore
  45. rx.chakra.thead( # type: ignore
  46. rx.chakra.tr( # type: ignore
  47. rx.chakra.th("Name"),
  48. rx.chakra.th("Age"),
  49. rx.chakra.th("Location"),
  50. )
  51. ),
  52. rx.chakra.tbody( # type: ignore
  53. rx.chakra.tr( # type: ignore
  54. rx.chakra.td("John"),
  55. rx.chakra.td(30),
  56. rx.chakra.td("New York"),
  57. ),
  58. rx.chakra.tr( # type: ignore
  59. rx.chakra.td("Jane"),
  60. rx.chakra.td(31),
  61. rx.chakra.td("San Francisco"),
  62. ),
  63. rx.chakra.tr( # type: ignore
  64. rx.chakra.td("Joe"),
  65. rx.chakra.td(32),
  66. rx.chakra.td("Los Angeles"),
  67. ),
  68. ),
  69. rx.chakra.tfoot( # type: ignore
  70. rx.chakra.tr(
  71. rx.chakra.td("footer1"),
  72. rx.chakra.td("footer2"),
  73. rx.chakra.td("footer3"),
  74. ) # type: ignore
  75. ),
  76. rx.chakra.table_caption("random caption"),
  77. variant="striped",
  78. color_scheme="teal",
  79. )
  80. )
  81. )
  82. @pytest.fixture()
  83. def table(tmp_path_factory) -> Generator[AppHarness, None, None]:
  84. """Start Table app at tmp_path via AppHarness.
  85. Args:
  86. tmp_path_factory: pytest tmp_path_factory fixture
  87. Yields:
  88. running AppHarness instance
  89. """
  90. with AppHarness.create(
  91. root=tmp_path_factory.mktemp("table"),
  92. app_source=Table, # type: ignore
  93. ) as harness:
  94. assert harness.app_instance is not None, "app is not running"
  95. yield harness
  96. @pytest.fixture
  97. def driver(table: AppHarness):
  98. """GEt an instance of the browser open to the table app.
  99. Args:
  100. table: harness for Table app
  101. Yields:
  102. WebDriver instance.
  103. """
  104. driver = table.frontend()
  105. try:
  106. token_input = driver.find_element(By.ID, "token")
  107. assert token_input
  108. # wait for the backend connection to send the token
  109. token = table.poll_for_value(token_input)
  110. assert token is not None
  111. yield driver
  112. finally:
  113. driver.quit()
  114. @pytest.mark.parametrize("route", ["", "/another"])
  115. def test_table(driver, table: AppHarness, route):
  116. """Test that a table component is rendered properly.
  117. Args:
  118. driver: Selenium WebDriver open to the app
  119. table: Harness for Table app
  120. route: Page route or path.
  121. """
  122. driver.get(f"{table.frontend_url}/{route}")
  123. assert table.app_instance is not None, "app is not running"
  124. thead = driver.find_element(By.TAG_NAME, "thead")
  125. # poll till page is fully loaded.
  126. table.poll_for_content(element=thead)
  127. # check headers
  128. assert thead.find_element(By.TAG_NAME, "tr").text == "NAME AGE LOCATION"
  129. # check first row value
  130. assert (
  131. driver.find_element(By.TAG_NAME, "tbody")
  132. .find_elements(By.TAG_NAME, "tr")[0]
  133. .text
  134. == "John 30 New York"
  135. )
  136. # check footer
  137. assert (
  138. driver.find_element(By.TAG_NAME, "tfoot")
  139. .find_element(By.TAG_NAME, "tr")
  140. .text.lower()
  141. == "footer1 footer2 footer3"
  142. )
  143. # check caption
  144. assert driver.find_element(By.TAG_NAME, "caption").text == "random caption"