redux-wait-actions
Waits for actions to be dispatched until a timeout expires.
Installation
$ npm install redux-wait-actions --save-dev
Motivation
This module shall be used only for testing purposes to wait for the dispatch of actions that are dispatched in an asynchronous fashion by thunks.
When thunk
dispatches various actions asynchronously, the following test will fail:
store; ;
To solve this, one can use setTimeout
explicitly in each test:
store; ;
To get around this ugliness, Redux Wait Actions sets the timeout for you and returns a Promise
to wait for actions dispatch and handle timeouts.
Usage
Example #1: single action
const waitForActions = ; const action = type: 'action' ; const promise = ; store; await promise;
Example #2: thunk
const waitForActions = ; const action1 = type: 'action1' ;const action2 = type: 'action1' ;const action3 = type: 'action1' ;const thunk = { ; ; ;}; const promise = ; store; await promise;
API
waitForActions(store, actions, [options])
Returns a Promise
which fulfills if all actions
are dispatched before the timeout expires, otherwise it is rejected.
store
Type: Object
The Redux store.
actions
Type: Object
Array
The actions to wait for.
options
timeout
Type: Number
Default: 50
The timeout given in milliseconds.
actionArgument
Type: any
Default: undefined
A custom argument to inject in thunk actions.
Tests
$ npm test
$ npm test:watch
during development