55
66import idom
77from idom import html
8+ from idom .config import IDOM_DEBUG_MODE
89from idom .core .hooks import COMPONENT_DID_RENDER_EFFECT , LifeCycleHook , current_hook
910from idom .core .layout import Layout
1011from 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
1214from idom .utils import Ref
1315from tests .tooling .asserts import assert_same_items
1416
@@ -553,7 +555,7 @@ def bad_effect():
553555
554556 return idom .html .div ()
555557
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" ):
557559 async with idom .Layout (ComponentWithEffect ()) as layout :
558560 await layout .render () # no error
559561
@@ -574,7 +576,7 @@ def bad_cleanup():
574576
575577 return idom .html .div ()
576578
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" ):
578580 async with idom .Layout (ComponentWithEffect ()) as layout :
579581 await layout .render ()
580582 component_hook .latest .schedule_render ()
@@ -600,7 +602,7 @@ def bad_cleanup():
600602
601603 return idom .html .div ()
602604
603- with assert_idom_logged (
605+ with assert_idom_did_log (
604606 match_message = r"Pre-unmount effect .*? failed" ,
605607 error_type = ValueError ,
606608 ):
@@ -843,7 +845,7 @@ def test_bad_schedule_render_callback():
843845 def bad_callback ():
844846 raise ValueError ("something went wrong" )
845847
846- with assert_idom_logged (
848+ with assert_idom_did_log (
847849 match_message = f"Failed to schedule render via { bad_callback } "
848850 ):
849851 LifeCycleHook (bad_callback ).schedule_render ()
@@ -1137,7 +1139,7 @@ def bad_effect():
11371139 hook .add_effect (COMPONENT_DID_RENDER_EFFECT , bad_effect )
11381140 return idom .html .div ()
11391141
1140- with assert_idom_logged (
1142+ with assert_idom_did_log (
11411143 match_message = "Component post-render effect .*? failed" ,
11421144 error_type = ValueError ,
11431145 match_error = "The error message" ,
@@ -1168,3 +1170,80 @@ def SetStateDuringRender():
11681170 # there should be no more renders to perform
11691171 with pytest .raises (asyncio .TimeoutError ):
11701172 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