StoreTestHarness

Struct StoreTestHarness 

Source
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>

Source

pub fn new(state: S, reducer: Reducer<S, A>) -> Self

Create a new harness with initial state and reducer.

Source

pub fn with_size(self, width: u16, height: u16) -> Self

Set the default terminal size for rendering.

Source

pub fn dispatch(&mut self, action: A) -> bool

Dispatch an action to the store.

Returns true if the state changed.

Source

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.

Source

pub fn state(&self) -> &S

Get a reference to the current state.

Source

pub fn state_mut(&mut self) -> &mut S

Get a mutable reference to the state for test setup.

Source

pub fn sender(&self) -> UnboundedSender<A>

Get a sender clone for simulating async action completions.

Source

pub fn emit(&self, action: A)

Emit an action to the channel (simulates async completion).

Source

pub fn complete_action(&self, action: A)

Simulate async action completion (semantic alias for Self::emit).

Source

pub fn complete_actions(&self, actions: impl IntoIterator<Item = A>)

Simulate multiple async action completions.

Source

pub fn drain_emitted(&mut self) -> Vec<A>

Drain all emitted actions from the channel.

Source

pub fn has_emitted(&mut self) -> bool

Check if any actions were emitted.

Source

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).

Source

pub fn send_keys<C, H, I>(&mut self, keys: &str, handler: H) -> Vec<A>
where C: ComponentId, I: IntoIterator<Item = A>, H: FnMut(&mut S, Event<C>) -> I,

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.

Source

pub fn send_keys_dispatch<C, H, I>(&mut self, keys: &str, handler: H) -> Vec<A>
where C: ComponentId, I: IntoIterator<Item = A>, H: FnMut(&mut S, Event<C>) -> I,

Send keys and dispatch returned actions to the store.

Returns the actions that were dispatched.

Source

pub fn assert_state<F>(&self, predicate: F)
where F: FnOnce(&S) -> bool,

Assert a condition on the current state.

§Panics

Panics if the predicate returns false.

Source

pub fn assert_state_msg<F>(&self, predicate: F, msg: &str)
where F: FnOnce(&S) -> bool,

Assert a condition with a custom message.

Source

pub fn render<F>(&mut self, width: u16, height: u16, render_fn: F) -> String
where F: FnOnce(&mut Frame<'_>, Rect, &S),

Render using the provided function, returns string with ANSI codes.

Source

pub fn render_plain<F>( &mut self, width: u16, height: u16, render_fn: F, ) -> String
where F: FnOnce(&mut Frame<'_>, Rect, &S),

Render to plain string (no ANSI codes).

Source

pub fn render_default<F>(&mut self, render_fn: F) -> String
where F: FnOnce(&mut Frame<'_>, Rect, &S),

Render with default terminal size (set via Self::with_size).

Source

pub fn render_default_plain<F>(&mut self, render_fn: F) -> String
where F: FnOnce(&mut Frame<'_>, Rect, &S),

Render with default size to plain string.

Trait Implementations§

Source§

impl<S: Default, A: Action> Default for StoreTestHarness<S, A>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S, A> Freeze for StoreTestHarness<S, A>
where S: Freeze,

§

impl<S, A> RefUnwindSafe for StoreTestHarness<S, A>

§

impl<S, A> Send for StoreTestHarness<S, A>
where S: Send,

§

impl<S, A> Sync for StoreTestHarness<S, A>
where S: Sync, A: Sync,

§

impl<S, A> Unpin for StoreTestHarness<S, A>
where S: Unpin, A: Unpin,

§

impl<S, A> UnwindSafe for StoreTestHarness<S, A>
where S: UnwindSafe, A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more