pub struct StoreTestHarness<S, A: Action> { /* private fields */ }Expand description
Test harness combining Store + action channel + render capabilities.
Provides an integrated testing experience for applications using the
standard Store with bool reducers.
§Example
use tui_dispatch::testing::StoreTestHarness;
let mut harness = StoreTestHarness::new(AppState::default(), reducer);
// Dispatch and check state
harness.dispatch(Action::Increment);
harness.assert_state(|s| s.count == 1);
// Send keys through component
let actions = harness.send_keys::<NumericComponentId, _, _>("j j enter", |state, event| {
component.handle_event(&event.kind, Props { state })
});
actions.assert_contains(Action::Select(2));
// Snapshot testing
let output = harness.render_plain(60, 24, |f, area, state| {
component.render(f, area, Props { state });
});
assert!(output.contains("expected text"));Implementations§
Source§impl<S, A: Action> StoreTestHarness<S, A>
impl<S, A: Action> StoreTestHarness<S, A>
Sourcepub fn new(state: S, reducer: Reducer<S, A>) -> Self
pub fn new(state: S, reducer: Reducer<S, A>) -> Self
Create a new harness with initial state and reducer.
Sourcepub fn with_size(self, width: u16, height: u16) -> Self
pub fn with_size(self, width: u16, height: u16) -> Self
Set the default terminal size for rendering.
Sourcepub fn dispatch(&mut self, action: A) -> bool
pub fn dispatch(&mut self, action: A) -> bool
Dispatch an action to the store.
Returns true if the state changed.
Sourcepub fn dispatch_all(
&mut self,
actions: impl IntoIterator<Item = A>,
) -> Vec<bool>
pub fn dispatch_all( &mut self, actions: impl IntoIterator<Item = A>, ) -> Vec<bool>
Dispatch multiple actions in sequence.
Returns a vector of booleans indicating which dispatches changed state.
Sourcepub fn sender(&self) -> UnboundedSender<A>
pub fn sender(&self) -> UnboundedSender<A>
Get a sender clone for simulating async action completions.
Sourcepub fn complete_action(&self, action: A)
pub fn complete_action(&self, action: A)
Simulate async action completion (semantic alias for Self::emit).
Sourcepub fn complete_actions(&self, actions: impl IntoIterator<Item = A>)
pub fn complete_actions(&self, actions: impl IntoIterator<Item = A>)
Simulate multiple async action completions.
Sourcepub fn drain_emitted(&mut self) -> Vec<A>
pub fn drain_emitted(&mut self) -> Vec<A>
Drain all emitted actions from the channel.
Sourcepub fn has_emitted(&mut self) -> bool
pub fn has_emitted(&mut self) -> bool
Check if any actions were emitted.
Sourcepub fn process_emitted(&mut self) -> (usize, usize)
pub fn process_emitted(&mut self) -> (usize, usize)
Process all emitted actions through the store.
Drains the channel and dispatches each action to the store.
Returns (changed_count, total_count).
Sourcepub fn send_keys<C, H, I>(&mut self, keys: &str, handler: H) -> Vec<A>
pub fn send_keys<C, H, I>(&mut self, keys: &str, handler: H) -> Vec<A>
Send a sequence of key events and collect actions from a handler.
Parses the space-separated key string and calls the handler for each event, collecting all returned actions.
Sourcepub fn send_keys_dispatch<C, H, I>(&mut self, keys: &str, handler: H) -> Vec<A>
pub fn send_keys_dispatch<C, H, I>(&mut self, keys: &str, handler: H) -> Vec<A>
Send keys and dispatch returned actions to the store.
Returns the actions that were dispatched.
Sourcepub fn assert_state<F>(&self, predicate: F)
pub fn assert_state<F>(&self, predicate: F)
Sourcepub fn assert_state_msg<F>(&self, predicate: F, msg: &str)
pub fn assert_state_msg<F>(&self, predicate: F, msg: &str)
Assert a condition with a custom message.
Sourcepub fn render<F>(&mut self, width: u16, height: u16, render_fn: F) -> String
pub fn render<F>(&mut self, width: u16, height: u16, render_fn: F) -> String
Render using the provided function, returns string with ANSI codes.
Sourcepub fn render_plain<F>(
&mut self,
width: u16,
height: u16,
render_fn: F,
) -> String
pub fn render_plain<F>( &mut self, width: u16, height: u16, render_fn: F, ) -> String
Render to plain string (no ANSI codes).
Sourcepub fn render_default<F>(&mut self, render_fn: F) -> String
pub fn render_default<F>(&mut self, render_fn: F) -> String
Render with default terminal size (set via Self::with_size).
Trait Implementations§
Auto Trait Implementations§
impl<S, A> Freeze for StoreTestHarness<S, A>where
S: Freeze,
impl<S, A> RefUnwindSafe for StoreTestHarness<S, A>where
S: RefUnwindSafe,
A: RefUnwindSafe,
impl<S, A> Send for StoreTestHarness<S, A>where
S: Send,
impl<S, A> Sync for StoreTestHarness<S, A>
impl<S, A> Unpin for StoreTestHarness<S, A>
impl<S, A> UnwindSafe for StoreTestHarness<S, A>where
S: UnwindSafe,
A: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more