test_minified_states.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """Integration tests for minified state names."""
  2. from __future__ import annotations
  3. from functools import partial
  4. from typing import Generator, Optional, Type
  5. import pytest
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.remote.webdriver import WebDriver
  8. from reflex.config import environment
  9. from reflex.testing import AppHarness, AppHarnessProd
  10. def MinifiedStatesApp(minify: bool | None) -> None:
  11. """A test app for minified state names.
  12. Args:
  13. minify: whether to minify state names
  14. """
  15. import reflex as rx
  16. class MinifiedState(rx.State):
  17. """State for the MinifiedStatesApp app."""
  18. pass
  19. app = rx.App()
  20. def index():
  21. return rx.vstack(
  22. rx.input(
  23. value=MinifiedState.router.session.client_token,
  24. is_read_only=True,
  25. id="token",
  26. ),
  27. rx.text(f"minify: {minify}", id="minify"),
  28. rx.text(MinifiedState.get_name(), id="state_name"),
  29. rx.text(MinifiedState.get_full_name(), id="state_full_name"),
  30. )
  31. app.add_page(index)
  32. @pytest.fixture(
  33. params=[
  34. pytest.param(False),
  35. pytest.param(True),
  36. pytest.param(None),
  37. ],
  38. )
  39. def minify_state_env(
  40. request: pytest.FixtureRequest,
  41. ) -> Generator[Optional[bool], None, None]:
  42. """Set the environment variable to minify state names.
  43. Args:
  44. request: pytest fixture request
  45. Yields:
  46. minify_states: whether to minify state names
  47. """
  48. minify_states: Optional[bool] = request.param
  49. environment.REFLEX_MINIFY_STATES.set(minify_states)
  50. yield minify_states
  51. environment.REFLEX_MINIFY_STATES.set(None)
  52. @pytest.fixture
  53. def test_app(
  54. app_harness_env: Type[AppHarness],
  55. tmp_path_factory: pytest.TempPathFactory,
  56. minify_state_env: Optional[bool],
  57. ) -> Generator[AppHarness, None, None]:
  58. """Start MinifiedStatesApp app at tmp_path via AppHarness.
  59. Args:
  60. app_harness_env: either AppHarness (dev) or AppHarnessProd (prod)
  61. tmp_path_factory: pytest tmp_path_factory fixture
  62. minify_state_env: need to request this fixture to set env before the app starts
  63. Yields:
  64. running AppHarness instance
  65. """
  66. name = f"testminifiedstates_{app_harness_env.__name__.lower()}"
  67. with app_harness_env.create(
  68. root=tmp_path_factory.mktemp(name),
  69. app_name=name,
  70. app_source=partial(MinifiedStatesApp, minify=minify_state_env), # type: ignore
  71. ) as harness:
  72. yield harness
  73. @pytest.fixture
  74. def driver(test_app: AppHarness) -> Generator[WebDriver, None, None]:
  75. """Get an instance of the browser open to the test_app app.
  76. Args:
  77. test_app: harness for MinifiedStatesApp app
  78. Yields:
  79. WebDriver instance.
  80. """
  81. assert test_app.app_instance is not None, "app is not running"
  82. driver = test_app.frontend()
  83. try:
  84. yield driver
  85. finally:
  86. driver.quit()
  87. def test_minified_states(
  88. test_app: AppHarness,
  89. driver: WebDriver,
  90. minify_state_env: Optional[bool],
  91. ) -> None:
  92. """Test minified state names.
  93. Args:
  94. test_app: harness for MinifiedStatesApp
  95. driver: WebDriver instance.
  96. minify_state_env: whether state minification is enabled by env var.
  97. """
  98. assert test_app.app_instance is not None, "app is not running"
  99. is_prod = isinstance(test_app, AppHarnessProd)
  100. # default to minifying in production
  101. should_minify: bool = is_prod
  102. # env overrides default
  103. if minify_state_env is not None:
  104. should_minify = minify_state_env
  105. # get a reference to the connected client
  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 = test_app.poll_for_value(token_input)
  110. assert token
  111. state_name_text = driver.find_element(By.ID, "state_name")
  112. assert state_name_text
  113. state_name = state_name_text.text
  114. state_full_name_text = driver.find_element(By.ID, "state_full_name")
  115. assert state_full_name_text
  116. _ = state_full_name_text.text
  117. assert test_app.app_module
  118. module_state_prefix = test_app.app_module.__name__.replace(".", "___")
  119. if should_minify:
  120. assert len(state_name) == 1
  121. else:
  122. assert state_name == f"{module_state_prefix}____minified_state"