Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_exception_handlers: add test case that triggers ErrorBoundary #4327

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion tests/integration/test_exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from reflex.testing import AppHarness
from reflex.testing import AppHarness, AppHarnessProd


def TestApp():
Expand All @@ -26,6 +26,8 @@ class TestAppConfig(rx.Config):
class TestAppState(rx.State):
"""State for the TestApp app."""

react_error: bool = False

def divide_by_number(self, number: int):
"""Divide by number and print the result.

Expand All @@ -50,6 +52,18 @@ def index():
on_click=lambda: TestAppState.divide_by_number(0), # type: ignore
id="induce-backend-error-btn",
),
rx.button(
"induce_react_error",
on_click=TestAppState.set_react_error(True), # type: ignore
id="induce-react-error-btn",
),
rx.box(
rx.cond(
TestAppState.react_error,
rx.Var.create({"invalid": "cannot have object as child"}),
"",
),
),
)


Expand Down Expand Up @@ -152,3 +166,37 @@ def test_backend_exception_handler_during_runtime(
"divide_by_number" in captured_default_handler_output.out
and "ZeroDivisionError" in captured_default_handler_output.out
)


def test_frontend_exception_handler_with_react(
test_app: AppHarness,
driver: WebDriver,
capsys,
):
"""Test calling frontend exception handler during runtime.

Render an object as a react child, which is invalid.

Args:
test_app: harness for TestApp app
driver: WebDriver instance.
capsys: pytest fixture for capturing stdout and stderr.

"""
reset_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.ID, "induce-react-error-btn"))
)

reset_button.click()

# Wait for the error to be logged
time.sleep(2)

captured_default_handler_output = capsys.readouterr()
if isinstance(test_app, AppHarnessProd):
assert "Error: Minified React error #31" in captured_default_handler_output.out
else:
assert (
"Error: Objects are not valid as a React child (found: object with keys \n{invalid})"
in captured_default_handler_output.out
)
Loading