5
5
6
6
import idom
7
7
from idom import html
8
+ from idom .config import IDOM_DEBUG_MODE
8
9
from idom .core .hooks import COMPONENT_DID_RENDER_EFFECT , LifeCycleHook , current_hook
9
10
from idom .core .layout import Layout
10
11
from idom .core .serve import render_json_patch
11
- from idom .testing import DisplayFixture , HookCatcher , assert_idom_logged , poll
12
+ from idom .testing import DisplayFixture , HookCatcher , assert_idom_did_log , poll
13
+ from idom .testing .logs import assert_idom_did_not_log
12
14
from idom .utils import Ref
13
15
from tests .tooling .asserts import assert_same_items
14
16
@@ -553,7 +555,7 @@ def bad_effect():
553
555
554
556
return idom .html .div ()
555
557
556
- with assert_idom_logged (match_message = r"Layout post-render effect .* failed" ):
558
+ with assert_idom_did_log (match_message = r"Layout post-render effect .* failed" ):
557
559
async with idom .Layout (ComponentWithEffect ()) as layout :
558
560
await layout .render () # no error
559
561
@@ -574,7 +576,7 @@ def bad_cleanup():
574
576
575
577
return idom .html .div ()
576
578
577
- with assert_idom_logged (match_error = r"Layout post-render effect .* failed" ):
579
+ with assert_idom_did_log (match_error = r"Layout post-render effect .* failed" ):
578
580
async with idom .Layout (ComponentWithEffect ()) as layout :
579
581
await layout .render ()
580
582
component_hook .latest .schedule_render ()
@@ -600,7 +602,7 @@ def bad_cleanup():
600
602
601
603
return idom .html .div ()
602
604
603
- with assert_idom_logged (
605
+ with assert_idom_did_log (
604
606
match_message = r"Pre-unmount effect .*? failed" ,
605
607
error_type = ValueError ,
606
608
):
@@ -843,7 +845,7 @@ def test_bad_schedule_render_callback():
843
845
def bad_callback ():
844
846
raise ValueError ("something went wrong" )
845
847
846
- with assert_idom_logged (
848
+ with assert_idom_did_log (
847
849
match_message = f"Failed to schedule render via { bad_callback } "
848
850
):
849
851
LifeCycleHook (bad_callback ).schedule_render ()
@@ -1137,7 +1139,7 @@ def bad_effect():
1137
1139
hook .add_effect (COMPONENT_DID_RENDER_EFFECT , bad_effect )
1138
1140
return idom .html .div ()
1139
1141
1140
- with assert_idom_logged (
1142
+ with assert_idom_did_log (
1141
1143
match_message = "Component post-render effect .*? failed" ,
1142
1144
error_type = ValueError ,
1143
1145
match_error = "The error message" ,
@@ -1168,3 +1170,80 @@ def SetStateDuringRender():
1168
1170
# there should be no more renders to perform
1169
1171
with pytest .raises (asyncio .TimeoutError ):
1170
1172
await asyncio .wait_for (layout .render (), timeout = 0.1 )
1173
+
1174
+
1175
+ @pytest .mark .skipif (not IDOM_DEBUG_MODE .current , reason = "only logs in debug mode" )
1176
+ async def test_use_debug_mode ():
1177
+ set_message = idom .Ref ()
1178
+ component_hook = HookCatcher ()
1179
+
1180
+ @idom .component
1181
+ @component_hook .capture
1182
+ def SomeComponent ():
1183
+ message , set_message .current = idom .use_state ("hello" )
1184
+ idom .use_debug_value (f"message is { message !r} " )
1185
+ return idom .html .div ()
1186
+
1187
+ async with idom .Layout (SomeComponent ()) as layout :
1188
+
1189
+ with assert_idom_did_log (r"SomeComponent\(.*?\) message is 'hello'" ):
1190
+ await layout .render ()
1191
+
1192
+ set_message .current ("bye" )
1193
+
1194
+ with assert_idom_did_log (r"SomeComponent\(.*?\) message is 'bye'" ):
1195
+ await layout .render ()
1196
+
1197
+ component_hook .latest .schedule_render ()
1198
+
1199
+ with assert_idom_did_not_log (r"SomeComponent\(.*?\) message is 'bye'" ):
1200
+ await layout .render ()
1201
+
1202
+
1203
+ @pytest .mark .skipif (not IDOM_DEBUG_MODE .current , reason = "only logs in debug mode" )
1204
+ async def test_use_debug_mode_with_factory ():
1205
+ set_message = idom .Ref ()
1206
+ component_hook = HookCatcher ()
1207
+
1208
+ @idom .component
1209
+ @component_hook .capture
1210
+ def SomeComponent ():
1211
+ message , set_message .current = idom .use_state ("hello" )
1212
+ idom .use_debug_value (lambda : f"message is { message !r} " )
1213
+ return idom .html .div ()
1214
+
1215
+ async with idom .Layout (SomeComponent ()) as layout :
1216
+
1217
+ with assert_idom_did_log (r"SomeComponent\(.*?\) message is 'hello'" ):
1218
+ await layout .render ()
1219
+
1220
+ set_message .current ("bye" )
1221
+
1222
+ with assert_idom_did_log (r"SomeComponent\(.*?\) message is 'bye'" ):
1223
+ await layout .render ()
1224
+
1225
+ component_hook .latest .schedule_render ()
1226
+
1227
+ with assert_idom_did_not_log (r"SomeComponent\(.*?\) message is 'bye'" ):
1228
+ await layout .render ()
1229
+
1230
+
1231
+ @pytest .mark .skipif (IDOM_DEBUG_MODE .current , reason = "logs in debug mode" )
1232
+ async def test_use_debug_mode_does_not_log_if_not_in_debug_mode ():
1233
+ set_message = idom .Ref ()
1234
+
1235
+ @idom .component
1236
+ def SomeComponent ():
1237
+ message , set_message .current = idom .use_state ("hello" )
1238
+ idom .use_debug_value (lambda : f"message is { message !r} " )
1239
+ return idom .html .div ()
1240
+
1241
+ async with idom .Layout (SomeComponent ()) as layout :
1242
+
1243
+ with assert_idom_did_not_log (r"SomeComponent\(.*?\) message is 'hello'" ):
1244
+ await layout .render ()
1245
+
1246
+ set_message .current ("bye" )
1247
+
1248
+ with assert_idom_did_not_log (r"SomeComponent\(.*?\) message is 'bye'" ):
1249
+ await layout .render ()
0 commit comments