def test_debug_log_dynamic_children_must_have_keys(caplog): idom.vdom("div", [idom.vdom("div")]) assert len(caplog.records) == 1 assert caplog.records[0].message.startswith("Key not specified for child") caplog.records.clear() @idom.component def MyComponent(): return idom.vdom("div") idom.vdom("div", [MyComponent()]) assert len(caplog.records) == 1 assert caplog.records[0].message.startswith("Key not specified for child")
async def test_callable_attributes_are_cast_to_event_handlers(): params_from_calls = [] node = idom.vdom("div", {"onEvent": lambda *args: params_from_calls.append(args)}) event_handlers = node.pop("eventHandlers") assert node == {"tagName": "div"} handler = event_handlers["onEvent"] assert event_handlers == {"onEvent": EventHandler(handler.function)} await handler.function([1, 2]) await handler.function([3, 4, 5]) assert params_from_calls == [(1, 2), (3, 4, 5)]
async def test_event_handlers_and_callable_attributes_are_automatically_merged( ): calls = [] node = idom.vdom( "div", {"onEvent": lambda: calls.append("callable_attr")}, event_handlers={ "onEvent": EventHandler(lambda data: calls.append("normal_event_handler")) }, ) event_handlers = node.pop("eventHandlers") assert node == {"tagName": "div"} handler = event_handlers["onEvent"] assert event_handlers == {"onEvent": EventHandler(handler.function)} await handler.function([]) assert calls == ["normal_event_handler", "callable_attr"]
async def simple_stateful_element(self, tag): return idom.vdom(tag)
async def simple_param_element(self, tag): return idom.vdom(tag)
def test_vdom_attribute_arguments_come_before_children(): with pytest.raises(ValueError): idom.vdom("div", ["c1", "c2"], {"attr": 1})
import idom from idom.core.vdom import make_vdom_constructor fake_events = idom.Events() @fake_events.on("Click") async def handler(event): pass @pytest.mark.parametrize( "actual, expected", [ ( idom.vdom("div", [idom.vdom("div")]), { "tagName": "div", "children": [{ "tagName": "div" }] }, ), ( idom.vdom("div", {"style": { "backgroundColor": "red" }}), { "tagName": "div", "attributes": { "style": {
import idom from idom.core.vdom import make_vdom_constructor, component fake_events = idom.Events() @fake_events.on("Click") async def handler(event): pass @pytest.mark.parametrize( "actual, expected", [ ( idom.vdom("div", [idom.vdom("div")]), { "tagName": "div", "children": [{ "tagName": "div" }] }, ), ( idom.vdom("div", {"style": { "backgroundColor": "red" }}), { "tagName": "div", "attributes": { "style": {
async def SimpleElement(self, tag): return idom.vdom(tag)
def SimpleParamComponent(tag): return idom.vdom(tag)
import idom from idom.core.vdom import component, make_vdom_constructor fake_events = idom.Events() @fake_events.on("Click") async def handler(event): pass @pytest.mark.parametrize( "actual, expected", [ ( idom.vdom("div", [idom.vdom("div")]), { "tagName": "div", "children": [{ "tagName": "div" }] }, ), ( idom.vdom("div", {"style": { "backgroundColor": "red" }}), { "tagName": "div", "attributes": { "style": {
def SimpleComponent(): tag, set_state_hook.current = idom.hooks.use_state("div") return idom.vdom(tag)
def MyComponent(): return idom.vdom("div")
def test_debug_log_cannot_verify_keypath_for_genereators(caplog): idom.vdom("div", (1 for i in range(10))) assert len(caplog.records) == 1 assert caplog.records[0].message.startswith( "Did not verify key-path integrity of children in generator") caplog.records.clear()
def test_debug_log_if_children_in_attributes(caplog): idom.vdom("div", {"children": ["hello"]}) assert len(caplog.records) == 1 assert caplog.records[0].message.startswith( "Reserved key 'children' found in attributes") caplog.records.clear()
(False, VdomDict()), (True, { "tagName": "" }), (True, VdomDict(tagName="")), ], ) def test_is_vdom(result, value): assert is_vdom(value) == result @pytest.mark.parametrize( "actual, expected", [ ( idom.vdom("div", [idom.vdom("div")]), { "tagName": "div", "children": [{ "tagName": "div" }] }, ), ( idom.vdom("div", {"style": { "backgroundColor": "red" }}), { "tagName": "div", "attributes": { "style": {