Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_event_handler_serialization():
assert EventHandler(target_id="uuid").serialize() == {
"target": "uuid",
"stopPropagation": False,
"preventDefault": False,
}
assert EventHandler(target_id="uuid", prevent_default=True).serialize() == {
"target": "uuid",
"stopPropagation": False,
"preventDefault": True,
}
assert EventHandler(target_id="uuid", stop_propagation=True).serialize() == {
"target": "uuid",
"stopPropagation": True,
"preventDefault": False,
}
def test_event_handler_serialization():
assert EventHandler(target_id="uuid").serialize() == {
"target": "uuid",
"stopPropagation": False,
"preventDefault": False,
}
assert EventHandler(target_id="uuid", prevent_default=True).serialize() == {
"target": "uuid",
"stopPropagation": False,
"preventDefault": True,
}
assert EventHandler(target_id="uuid", stop_propagation=True).serialize() == {
"target": "uuid",
"stopPropagation": True,
"preventDefault": False,
}
def test_event_handler_serialization():
assert EventHandler(target_id="uuid").serialize() == {
"target": "uuid",
"stopPropagation": False,
"preventDefault": False,
}
assert EventHandler(target_id="uuid", prevent_default=True).serialize() == {
"target": "uuid",
"stopPropagation": False,
"preventDefault": True,
}
assert EventHandler(target_id="uuid", stop_propagation=True).serialize() == {
"target": "uuid",
"stopPropagation": True,
"preventDefault": False,
}
def test_simple_events_object():
events = idom.Events()
@events.on("Click")
async def click_handler():
pass
@events.on("keyPress")
async def key_press_handler():
pass
assert isinstance(events["onClick"], EventHandler)
assert isinstance(events["onKeyPress"], EventHandler)
assert "onClick" in events
assert "onKeyPress" in events
assert len(events) == 2
async def test_multiple_callbacks_per_event_handler():
calls = []
event_handler = EventHandler()
@event_handler.add
async def callback_1(event):
calls.append(1)
@event_handler.add
async def callback_2(event):
calls.append(2)
await event_handler([{}])
assert calls == [1, 2]
def test_simple_events_object():
events = idom.Events()
@events.on("Click")
async def click_handler():
pass
@events.on("keyPress")
async def key_press_handler():
pass
assert isinstance(events["onClick"], EventHandler)
assert isinstance(events["onKeyPress"], EventHandler)
assert "onClick" in events
assert "onKeyPress" in events
assert len(events) == 2
def _load_event_handlers(
self, model: Dict[str, Any], element_id: str
) -> Dict[str, Dict[str, Any]]:
# gather event handler from eventHandlers and attributes fields
handlers: Dict[str, EventHandler] = {}
if "eventHandlers" in model:
handlers.update(model["eventHandlers"])
if "attributes" in model:
attrs = model["attributes"]
for k, v in list(attrs.items()):
if callable(v):
if not isinstance(v, EventHandler):
h = handlers[k] = EventHandler()
h.add(attrs.pop(k))
else:
h = attrs.pop(k)
handlers[k] = h
event_targets = {}
for event, handler in handlers.items():
handler_spec = handler.serialize()
event_targets[event] = handler_spec
self._event_handlers[handler.id] = handler
self._element_state[element_id]["event_handlers"].append(handler.id)
return event_targets
def _load_event_handlers(
self, model: Dict[str, Any], element_id: str
) -> Dict[str, Dict[str, Any]]:
# gather event handler from eventHandlers and attributes fields
handlers: Dict[str, EventHandler] = {}
if "eventHandlers" in model:
handlers.update(model["eventHandlers"])
if "attributes" in model:
attrs = model["attributes"]
for k, v in list(attrs.items()):
if callable(v):
if not isinstance(v, EventHandler):
h = handlers[k] = EventHandler()
h.add(attrs.pop(k))
else:
h = attrs.pop(k)
handlers[k] = h
event_targets = {}
for event, handler in handlers.items():
handler_spec = handler.serialize()
def _load_event_handlers(
self, model: Dict[str, Any], element_id: str
) -> Dict[str, Dict[str, Any]]:
# gather event handler from eventHandlers and attributes fields
handlers: Dict[str, EventHandler] = {}
if "eventHandlers" in model:
handlers.update(model["eventHandlers"])
if "attributes" in model:
attrs = model["attributes"]
for k, v in list(attrs.items()):
if callable(v):
if not isinstance(v, EventHandler):
h = handlers[k] = EventHandler()
h.add(attrs.pop(k))
else:
h = attrs.pop(k)
handlers[k] = h
event_targets = {}
for event, handler in handlers.items():
handler_spec = handler.serialize()
event_targets[event] = handler_spec
self._event_handlers[handler.id] = handler
self._element_state[element_id]["event_handlers"].append(handler.id)
return event_targets