Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_layout_cancels_renders_on_close():
event_that_is_never_set = asyncio.Event()
render_is_cancelled = asyncio.Event()
@idom.element
async def MyElement(self):
try:
await event_that_is_never_set.wait()
finally:
render_is_cancelled.set()
async with idom.Layout(MyElement()):
await asyncio.sleep(0.1)
await render_is_cancelled.wait()
def test_cannot_use_update_after_element_is_garbage_collected():
@idom.element
async def SomeElement():
...
element = SomeElement()
hook = idom.core._hooks.HookDispatcher(idom.Layout(element)).get_hook(element)
# cause garbage collection
del hook._dispatcher
del hook._layout
del element
gc.collect()
with pytest.raises(RuntimeError, match=r"Element for hook .* no longer exists"):
hook.use_update()
async def test_simple_layout():
@idom.element
async def SimpleElement(self, tag):
return idom.vdom(tag)
element = SimpleElement("div")
async with idom.Layout(element) as layout:
src, new, old, error = await layout.render()
assert src == element.id
assert new == {element.id: {"tagName": "div"}}
assert old == []
element.update("table")
src, new, old, error = await layout.render()
assert src == element.id
assert new == {element.id: {"tagName": "table"}}
assert old == []
@history.track("main")
@idom.element
async def Main(self):
return Child()
@history.track("child")
@idom.element
async def Child(self):
return idom.html.div()
@idom.element
async def element_not_in_layout(self):
...
async with idom.Layout(Main()) as layout:
await layout.render()
state = layout._element_state
with pytest.raises(KeyError):
layout._element_parent(element_not_in_layout())
assert not state
def test_layout_expects_abstract_element():
with pytest.raises(TypeError, match="Expected an AbstractElement"):
idom.Layout(None)
with pytest.raises(TypeError, match="Expected an AbstractElement"):
idom.Layout(idom.html.div())
async def test_nested_element_layout():
history = RenderHistory()
@history.track("parent")
@idom.element
async def Parent(self):
return idom.html.div([Child()])
@history.track("child")
@idom.element
async def Child(self):
return idom.html.div()
async with idom.Layout(Parent()) as layout:
src, new, old, error = await layout.render()
assert src == history.parent_1.id
assert new == {
history.parent_1.id: {
"tagName": "div",
"children": [{"data": history.child_1.id, "type": "ref"}],
},
history.child_1.id: {"tagName": "div"},
}
assert old == []
history.parent_1.update()
src, new, old, error = await layout.render()
async def _incoming(self, layout, context, message):
raise ValueError("this is a bug")
@idom.element
async def AnyElement(self):
return idom.html.div()
async def send(data):
pass
async def recv():
return {}
with pytest.raises(ExceptionGroup, match="this is a bug"):
async with RendererWithBug(idom.Layout(AnyElement())) as renderer:
await renderer.run(send, recv, None)
async def test_simple_stateful_element():
@idom.element
async def simple_stateful_element():
index, set_index = idom.hooks.use_state(0)
set_index(index + 1)
return idom.html.div(index)
ssd = simple_stateful_element()
async with idom.Layout(ssd) as layout:
assert (await layout.render()).new[ssd.id] == {
"tagName": "div",
"children": [{"data": "0", "type": "str"}],
}
assert (await layout.render()).new[ssd.id] == {
"tagName": "div",
"children": [{"data": "1", "type": "str"}],
}
assert (await layout.render()).new[ssd.id] == {
"tagName": "div",
"children": [{"data": "2", "type": "str"}],
}