This package has been deprecated

Author message:

deprecated in favor of redux-mock-store-await-actions

redux-wait-actions

1.0.0 • Public • Published

redux-wait-actions

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status Greenkeeper badge Conventional Commits

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.dispatch(thunk(...));
 
expect(store.getActions()).toEqual(expect.arrayContaining(expectedActions));

To solve this, one can use setTimeout explicitly in each test:

store.dispatch(thunk(...));
 
setTimeout(() => {
    expect(store.getActions()).toEqual(expect.arrayContaining(expectedActions));
}, 50);

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 = require('redux-wait-actions');
 
const action = { type: 'action' };
 
const promise = waitForActions(store, action);
 
store.dispatch(action);
 
await promise;
 

Example #2: thunk

const waitForActions = require('redux-wait-actions');
 
const action1 = { type: 'action1' };
const action2 = { type: 'action1' };
const action3 = { type: 'action1' };
const thunk = () => (dispatch, getState, customArgument) => {
    dispatch(action1);
    dispatch(action2);
    dispatch(action3);
};
 
const promise = waitForActions(store, [action1, action2, action3]);
 
store.dispatch(thunk());
 
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

License

MIT License

Package Sidebar

Install

npm i redux-wait-actions

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • acostalima