11
11
from selenium .webdriver .support import expected_conditions as EC
12
12
from selenium .webdriver .support .ui import WebDriverWait
13
13
14
- from reflex .testing import AppHarness
14
+ from reflex .testing import AppHarness , AppHarnessProd
15
15
16
16
17
17
def TestApp ():
@@ -26,6 +26,8 @@ class TestAppConfig(rx.Config):
26
26
class TestAppState (rx .State ):
27
27
"""State for the TestApp app."""
28
28
29
+ react_error : bool = False
30
+
29
31
def divide_by_number (self , number : int ):
30
32
"""Divide by number and print the result.
31
33
@@ -50,6 +52,18 @@ def index():
50
52
on_click = lambda : TestAppState .divide_by_number (0 ), # type: ignore
51
53
id = "induce-backend-error-btn" ,
52
54
),
55
+ rx .button (
56
+ "induce_react_error" ,
57
+ on_click = TestAppState .set_react_error (True ), # type: ignore
58
+ id = "induce-react-error-btn" ,
59
+ ),
60
+ rx .box (
61
+ rx .cond (
62
+ TestAppState .react_error ,
63
+ rx .Var .create ({"invalid" : "cannot have object as child" }),
64
+ "" ,
65
+ ),
66
+ ),
53
67
)
54
68
55
69
@@ -152,3 +166,37 @@ def test_backend_exception_handler_during_runtime(
152
166
"divide_by_number" in captured_default_handler_output .out
153
167
and "ZeroDivisionError" in captured_default_handler_output .out
154
168
)
169
+
170
+
171
+ def test_frontend_exception_handler_with_react (
172
+ test_app : AppHarness ,
173
+ driver : WebDriver ,
174
+ capsys ,
175
+ ):
176
+ """Test calling frontend exception handler during runtime.
177
+
178
+ Render an object as a react child, which is invalid.
179
+
180
+ Args:
181
+ test_app: harness for TestApp app
182
+ driver: WebDriver instance.
183
+ capsys: pytest fixture for capturing stdout and stderr.
184
+
185
+ """
186
+ reset_button = WebDriverWait (driver , 20 ).until (
187
+ EC .element_to_be_clickable ((By .ID , "induce-react-error-btn" ))
188
+ )
189
+
190
+ reset_button .click ()
191
+
192
+ # Wait for the error to be logged
193
+ time .sleep (2 )
194
+
195
+ captured_default_handler_output = capsys .readouterr ()
196
+ if isinstance (test_app , AppHarnessProd ):
197
+ assert "Error: Minified React error #31" in captured_default_handler_output .out
198
+ else :
199
+ assert (
200
+ "Error: Objects are not valid as a React child (found: object with keys \n {invalid})"
201
+ in captured_default_handler_output .out
202
+ )
0 commit comments