fix unit tests
This commit is contained in:
parent
13b041ce5d
commit
3947b8430d
@ -7,7 +7,7 @@ import typing
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from functools import lru_cache, wraps
|
from functools import lru_cache, wraps
|
||||||
from hashlib import md5
|
from hashlib import md5
|
||||||
from types import SimpleNamespace, LambdaType
|
from types import SimpleNamespace
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
Callable,
|
||||||
@ -1113,38 +1113,35 @@ class Component(BaseComponent, ABC):
|
|||||||
|
|
||||||
return vars
|
return vars
|
||||||
|
|
||||||
|
|
||||||
def _event_trigger_values_use_state(self) -> bool:
|
def _event_trigger_values_use_state(self) -> bool:
|
||||||
|
"""Check if the values of a component's event trigger use state.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if any of the component's event trigger values uses State.
|
||||||
|
"""
|
||||||
for trigger in self.event_triggers.values():
|
for trigger in self.event_triggers.values():
|
||||||
if isinstance(trigger, EventChain):
|
if isinstance(trigger, EventChain):
|
||||||
for event in trigger.events:
|
for event in trigger.events:
|
||||||
if event.handler.state_full_name or isinstance(event.handler.fn, LambdaType) and event.handler.fn.__name__== (lambda: None).__name__:
|
if event.handler.state_full_name:
|
||||||
return True
|
return True
|
||||||
elif isinstance(trigger, Var) and trigger._var_state:
|
elif isinstance(trigger, Var) and trigger._var_state:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _has_stateful_event_triggers(self):
|
def _has_stateful_event_triggers(self):
|
||||||
|
"""Check if component or children have any event triggers that use state.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if the component or children have any event triggers that uses state.
|
||||||
|
"""
|
||||||
if self.event_triggers and self._event_trigger_values_use_state():
|
if self.event_triggers and self._event_trigger_values_use_state():
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
if isinstance(child, Component) and child._has_stateful_event_triggers():
|
if (
|
||||||
return True
|
isinstance(child, Component)
|
||||||
return False
|
and child._has_stateful_event_triggers()
|
||||||
|
):
|
||||||
def _has_event_triggers(self) -> bool:
|
|
||||||
"""Check if the component or children have any event triggers.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if the component or children have any event triggers.
|
|
||||||
"""
|
|
||||||
if self.event_triggers:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
for child in self.children:
|
|
||||||
if isinstance(child, Component) and child._has_event_triggers():
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -2082,37 +2082,27 @@ class TriggerState(rx.State):
|
|||||||
"""Sample event handler."""
|
"""Sample event handler."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def do_another_thing(self, value):
|
||||||
|
"""Sample event handler with arg."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"component, exclude_event_trigger_values, output",
|
"component, output",
|
||||||
[
|
[
|
||||||
(rx.box(rx.text("random text")), None, False),
|
(rx.box(rx.text("random text")), False),
|
||||||
(rx.box(rx.text("random text", on_click=rx.console_log("log"))), None, True),
|
|
||||||
(
|
(
|
||||||
rx.box(rx.text("random text", on_click=rx.console_log("log"))),
|
rx.box(rx.text("random text", on_click=rx.console_log("log"))),
|
||||||
["_console"],
|
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
rx.box(
|
rx.box(
|
||||||
rx.text("random text", on_click=rx.console_log("log")),
|
rx.text("random text", on_click=TriggerState.do_something),
|
||||||
rx.text(
|
rx.text(
|
||||||
"random text",
|
"random text",
|
||||||
on_click=BaseVar(_var_name="toggleColorMode", _var_type=EventChain),
|
on_click=BaseVar(_var_name="toggleColorMode", _var_type=EventChain),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
["_console", "toggleColorMode"],
|
|
||||||
False,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
rx.box(
|
|
||||||
rx.text("random text", on_click=rx.console_log("log")),
|
|
||||||
rx.text(
|
|
||||||
"random text",
|
|
||||||
on_click=BaseVar(_var_name="toggleColorMode", _var_type=EventChain),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
["_console"],
|
|
||||||
True,
|
True,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@ -2123,17 +2113,10 @@ class TriggerState(rx.State):
|
|||||||
on_click=BaseVar(_var_name="toggleColorMode", _var_type=EventChain),
|
on_click=BaseVar(_var_name="toggleColorMode", _var_type=EventChain),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
["toggleColorMode"],
|
|
||||||
True,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
rx.box(rx.text("random text", on_click=TriggerState.do_something)),
|
|
||||||
["do_something"],
|
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
rx.box(rx.text("random text", on_click=TriggerState.do_something)),
|
rx.box(rx.text("random text", on_click=TriggerState.do_something)),
|
||||||
["non_existent"],
|
|
||||||
True,
|
True,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@ -2143,26 +2126,27 @@ class TriggerState(rx.State):
|
|||||||
on_click=[rx.console_log("log"), rx.window_alert("alert")],
|
on_click=[rx.console_log("log"), rx.window_alert("alert")],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
["_console", "_alert"],
|
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
rx.box(
|
rx.box(
|
||||||
rx.text(
|
rx.text(
|
||||||
"random text",
|
"random text",
|
||||||
on_click=lambda x: x,
|
on_click=[rx.console_log("log"), TriggerState.do_something],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
["_console", "_alert"],
|
True,
|
||||||
False,
|
),
|
||||||
|
(
|
||||||
|
rx.box(
|
||||||
|
rx.text(
|
||||||
|
"random text",
|
||||||
|
on_click=lambda val: TriggerState.do_another_thing(val), # type: ignore
|
||||||
|
),
|
||||||
|
),
|
||||||
|
True,
|
||||||
),
|
),
|
||||||
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_has_event_triggers(component, exclude_event_trigger_values, output):
|
def test_has_state_event_triggers(component, output):
|
||||||
assert (
|
assert component._has_stateful_event_triggers() == output
|
||||||
component._has_event_triggers(
|
|
||||||
exclude_event_trigger_values=exclude_event_trigger_values
|
|
||||||
)
|
|
||||||
== output
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user