test_table.py 4.9 KB

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