// tests/leptos/event_handler.rs use event_handlers_types::leptos::Cancelable; use event_handlers_types::leptos::{CancelToken, EventHandler}; use futures_util::future::ready; use leptos::create_runtime; #[test] fn event_handler_should_execute_successfully_when_not_canceled() { // Arrange let runtime = create_runtime(); // Use a type that implements `Display` for the future output. let executable_closure = || -> Result { Ok("Success".to_string()) }; let mut event_handler = EventHandler::new( ready(executable_closure()), CancelToken::new(), // Now the `new` function is in scope. ); let waker = futures_util::task::noop_waker(); // Act: Execute the EventHandler with a dummy context. let result = event_handler.execute(&waker); // Assert: Ensure that the result is Ok, indicating successful execution. assert!(result.is_ok()); runtime.dispose(); } #[test] fn event_handler_should_return_error_when_canceled() { // Arrange let runtime = create_runtime(); // Use a type that implements `Display` for the future output. let executable_closure = || -> Result { Ok("Success".to_string()) }; let mut cancel_token = CancelToken::new(); // Now the `new` function is in scope. cancel_token.cancel(); // Simulate a canceled operation. let mut event_handler = EventHandler::new(ready(executable_closure()), cancel_token); let waker = futures_util::task::noop_waker(); // Act: Execute the EventHandler with a dummy context. let result = event_handler.execute(&waker); // Assert: Ensure that the result is an error indicating cancellation. assert_eq!(result, Err("Operation was canceled.".to_string())); runtime.dispose(); }