Struct composable::TestStore

source ·
pub struct TestStore<State: Reducer>
where <State as Reducer>::Action: Debug,
{ /* private fields */ }
Expand description

A state container for the application testing.

§Example

Here is the second Reducer example being tested with a TestStore.

#[derive(Clone, Debug, Default, PartialEq)]
struct State {
    n: usize,
}

#[derive(Debug, PartialEq)]
enum Action {
    Increment,
    Decrement,
}

use Action::*;
impl Reducer for State {
    type Action = Action;
    type Output = Self;

    // This reducer ensures the value is always an even number
    fn reduce(&mut self, action: Action, send: impl Effects<Action>) {
        match action {
            Increment => {
                self.n += 1;
                if self.n % 2 == 1 {
                    send.action(Increment);
                }
            }
            Decrement => {
                self.n -= 1;
                if self.n % 2 == 1 {
                    send.action(Decrement);
                }
            }
        }
    }
}

let mut store = TestStore::<State>::default();

store.send(Increment, |state| state.n = 1);
store.recv(Increment, |state| state.n = 2);

store.send(Increment, |state| state.n = 3);
store.recv(Increment, |state| state.n = 4);

store.send(Decrement, |state| state.n = 3);
store.recv(Decrement, |state| state.n = 2);

let n = store.into_inner().n;
assert_eq!(n, 2);

Implementations§

source§

impl<State: Reducer> TestStore<State>
where <State as Reducer>::Action: Debug,

source

pub fn new<F>(with: F) -> Self
where F: FnOnce() -> State,

Creates a new Store with its initial state generated by a function.

source

pub fn with_initial(state: State) -> Self

Creates a new Store with state as its initial state.

source

pub fn send( &mut self, action: <State as Reducer>::Action, assert: impl FnOnce(&mut State), )
where State: Clone + Debug + PartialEq, <State as Reducer>::Action: 'static,

Calls the Store’s Reducer with action and asserts the expected state changes.

source

pub fn recv( &mut self, action: <State as Reducer>::Action, assert: impl FnOnce(&mut State), )
where State: Clone + Debug + PartialEq, <State as Reducer>::Action: Debug + PartialEq + 'static,

Checks that the Store’s Reducer was called with action and asserts the expected state changes.

source

pub fn wait(&mut self)

Waits until all scheduled tasks have completed.

A timeout should be added to tests calling wait() to ensure that it does not wait forever if there are bugs in the asynchronous tasks. For example

source

pub fn into_inner(self) -> <State as Reducer>::Output
where State: Into<<State as Reducer>::Output>,

Consumes the Store and returns its current state value.

Trait Implementations§

source§

impl<State> Default for TestStore<State>
where State: Default + Reducer, <State as Reducer>::Action: Debug,

source§

fn default() -> Self

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

impl<State: Reducer> Drop for TestStore<State>
where <State as Reducer>::Action: Debug,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<State: Reducer> TestClock for TestStore<State>
where <State as Reducer>::Action: Debug,

source§

fn advance(&mut self, duration: Duration)

Auto Trait Implementations§

§

impl<State> !Freeze for TestStore<State>

§

impl<State> !RefUnwindSafe for TestStore<State>

§

impl<State> !Send for TestStore<State>

§

impl<State> !Sync for TestStore<State>

§

impl<State> Unpin for TestStore<State>
where State: Unpin,

§

impl<State> !UnwindSafe for TestStore<State>

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, 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.