Struct composable::TestStore
source · pub struct TestStore<State: Reducer>{ /* 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>
impl<State: Reducer> TestStore<State>
sourcepub fn new<F>(with: F) -> Selfwhere
F: FnOnce() -> State,
pub fn new<F>(with: F) -> Selfwhere
F: FnOnce() -> State,
Creates a new Store
with its initial state generated by a function.
sourcepub fn with_initial(state: State) -> Self
pub fn with_initial(state: State) -> Self
Creates a new Store
with state
as its initial state.
sourcepub fn send(
&mut self,
action: <State as Reducer>::Action,
assert: impl FnOnce(&mut State),
)
pub fn send( &mut self, action: <State as Reducer>::Action, assert: impl FnOnce(&mut State), )
Calls the Store
’s Reducer
with action
and asserts the
expected state changes.
sourcepub fn recv(
&mut self,
action: <State as Reducer>::Action,
assert: impl FnOnce(&mut State),
)
pub fn recv( &mut self, action: <State as Reducer>::Action, assert: impl FnOnce(&mut State), )
Checks that the Store
’s Reducer
was called with action
and asserts the expected state changes.
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more