|
@@ -2,6 +2,7 @@
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import asyncio
|
|
|
+import time
|
|
|
from typing import Callable, Coroutine, Generator
|
|
|
|
|
|
import pytest
|
|
@@ -25,6 +26,12 @@ def TestEventAction():
|
|
|
def on_click2(self):
|
|
|
self.order.append("on_click2")
|
|
|
|
|
|
+ def on_click_throttle(self):
|
|
|
+ self.order.append("on_click_throttle")
|
|
|
+
|
|
|
+ def on_click_debounce(self):
|
|
|
+ self.order.append("on_click_debounce")
|
|
|
+
|
|
|
class EventFiringComponent(rx.Component):
|
|
|
"""A component that fires onClick event without passing DOM event."""
|
|
|
|
|
@@ -124,6 +131,20 @@ def TestEventAction():
|
|
|
"custom-prevent-default"
|
|
|
).prevent_default,
|
|
|
),
|
|
|
+ rx.button(
|
|
|
+ "Throttle",
|
|
|
+ id="btn-throttle",
|
|
|
+ on_click=lambda: EventActionState.on_click_throttle.throttle(
|
|
|
+ 200
|
|
|
+ ).stop_propagation,
|
|
|
+ ),
|
|
|
+ rx.button(
|
|
|
+ "Debounce",
|
|
|
+ id="btn-debounce",
|
|
|
+ on_click=EventActionState.on_click_debounce.debounce(
|
|
|
+ 200
|
|
|
+ ).stop_propagation,
|
|
|
+ ),
|
|
|
rx.chakra.list(
|
|
|
rx.foreach(
|
|
|
EventActionState.order, # type: ignore
|
|
@@ -280,3 +301,36 @@ async def test_event_actions(
|
|
|
assert driver.current_url != prev_url
|
|
|
else:
|
|
|
assert driver.current_url == prev_url
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.usefixtures("token")
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_event_actions_throttle_debounce(
|
|
|
+ driver: WebDriver,
|
|
|
+ poll_for_order: Callable[[list[str]], Coroutine[None, None, None]],
|
|
|
+):
|
|
|
+ """Click buttons with debounce and throttle and assert on fired events.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ driver: WebDriver instance.
|
|
|
+ poll_for_order: function that polls for the order list to match the expected order.
|
|
|
+ """
|
|
|
+ btn_throttle = driver.find_element(By.ID, "btn-throttle")
|
|
|
+ assert btn_throttle
|
|
|
+ btn_debounce = driver.find_element(By.ID, "btn-debounce")
|
|
|
+ assert btn_debounce
|
|
|
+
|
|
|
+ exp_events = 10
|
|
|
+ throttle_duration = exp_events * 0.2 # 200ms throttle
|
|
|
+ throttle_start = time.time()
|
|
|
+ while time.time() - throttle_start < throttle_duration:
|
|
|
+ btn_throttle.click()
|
|
|
+ btn_debounce.click()
|
|
|
+
|
|
|
+ try:
|
|
|
+ await poll_for_order(["on_click_throttle"] * exp_events + ["on_click_debounce"])
|
|
|
+ except AssertionError:
|
|
|
+ # Sometimes the last event gets throttled due to race, this is okay.
|
|
|
+ await poll_for_order(
|
|
|
+ ["on_click_throttle"] * (exp_events - 1) + ["on_click_debounce"]
|
|
|
+ )
|