test_table.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.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. @pytest.fixture()
  79. def table(tmp_path_factory) -> Generator[AppHarness, None, None]:
  80. """Start Table app at tmp_path via AppHarness.
  81. Args:
  82. tmp_path_factory: pytest tmp_path_factory fixture
  83. Yields:
  84. running AppHarness instance
  85. """
  86. with AppHarness.create(
  87. root=tmp_path_factory.mktemp("table"),
  88. app_source=Table, # type: ignore
  89. ) as harness:
  90. assert harness.app_instance is not None, "app is not running"
  91. yield harness
  92. @pytest.fixture
  93. def driver(table: AppHarness):
  94. """GEt an instance of the browser open to the table app.
  95. Args:
  96. table: harness for Table app
  97. Yields:
  98. WebDriver instance.
  99. """
  100. driver = table.frontend()
  101. try:
  102. token_input = driver.find_element(By.ID, "token")
  103. assert token_input
  104. # wait for the backend connection to send the token
  105. token = table.poll_for_value(token_input)
  106. assert token is not None
  107. yield driver
  108. finally:
  109. driver.quit()
  110. @pytest.mark.parametrize("route", ["", "/another"])
  111. def test_table(driver, table: AppHarness, route):
  112. """Test that a table component is rendered properly.
  113. Args:
  114. driver: Selenium WebDriver open to the app
  115. table: Harness for Table app
  116. route: Page route or path.
  117. """
  118. driver.get(f"{table.frontend_url}/{route}")
  119. assert table.app_instance is not None, "app is not running"
  120. thead = driver.find_element(By.TAG_NAME, "thead")
  121. # poll till page is fully loaded.
  122. table.poll_for_content(element=thead)
  123. # check headers
  124. assert thead.find_element(By.TAG_NAME, "tr").text == "NAME AGE LOCATION"
  125. # check first row value
  126. assert (
  127. driver.find_element(By.TAG_NAME, "tbody")
  128. .find_elements(By.TAG_NAME, "tr")[0]
  129. .text
  130. == "John 30 New York"
  131. )
  132. # check footer
  133. assert (
  134. driver.find_element(By.TAG_NAME, "tfoot")
  135. .find_element(By.TAG_NAME, "tr")
  136. .text.lower()
  137. == "footer1 footer2 footer3"
  138. )
  139. # check caption
  140. assert driver.find_element(By.TAG_NAME, "caption").text == "random caption"