Skip to content

Commit 8cd7d58

Browse files
committed
add test
1 parent f64b15f commit 8cd7d58

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/test_core/test_hooks.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,3 +1245,32 @@ def SomeComponent():
12451245

12461246
with assert_idom_did_not_log(r"SomeComponent\(.*?\) message is 'bye'"):
12471247
await layout.render()
1248+
1249+
1250+
async def test_conditionally_rendered_components_can_use_context():
1251+
set_state = idom.Ref()
1252+
used_context_values = []
1253+
some_context = idom.Context("some_context", None)
1254+
1255+
@idom.component
1256+
def SomeComponent():
1257+
state, set_state.current = idom.use_state(True)
1258+
if state:
1259+
return FirstCondition()
1260+
else:
1261+
return SecondCondition()
1262+
1263+
@idom.component
1264+
def FirstCondition():
1265+
used_context_values.append(idom.use_context(some_context))
1266+
1267+
@idom.component
1268+
def SecondCondition():
1269+
used_context_values.append(idom.use_context(some_context))
1270+
1271+
async with idom.Layout(some_context(SomeComponent(), value="the-value")) as layout:
1272+
await layout.render()
1273+
assert used_context_values == ["the-value"]
1274+
set_state.current(False)
1275+
await layout.render()
1276+
assert used_context_values == ["the-value", "the-value"]

0 commit comments

Comments
 (0)