npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-waitfor-middleware

v1.0.1

Published

Lightweight Redux middleware to assert on actions being dispatched

Downloads

864

Readme

redux-waitfor-middleware

Lightweight Redux middleware to wait for specific actions to be dispatched. Very useful for testing purposes.

Installation

npm install redux-waitfor-middleware

Usage

Inject Waitfor Middleware in your modules (or React components) to start recording actions. Then, you can use it in tests to wait for a given action to be dispatched before a given delay.

import createWaitForMiddleware from 'redux-waitfor-middleware';
import {createStore} from 'redux';
  
const reducers = [
  /* import your reducers */
];
const initialState = {
  /* Initial state store */
};

const waitForMiddleware = createWaitForMiddleware();

const store = applyMiddleware(waitForMiddleware)(createStore)(reducers, initialState);

The waitFor middleware has started recording ! You can then use its waitFor method to wait asynchronously for some actions to be dispatched :

/**
* Allows to await FOO_ACTION and BAR_ACTION actions to be dispatched :
*/
async function requiredActionsDispatched() {
  await waitForMiddleware.waiFor([
    'FOO_ACTION',
    'BAR_ACTION'
  ]);
}

Heads up ! If the actions have already been dispatched since the last call to clean, the method will immediately resolve with the payload of the actions found in its records.

This kind of method is very useful in cases where you want to test that a part of your app that might take some time to respond, for example your back-end, replies accordingly.
Check-list :

  • [x] you are able to fire a call to the back end in your test (you may be achieving this by simulating UI interaction)
  • [x] you are dispatching an action when a receiving a response from the back-end, whose payload is the content of the response
describe('Potatoes list', () => {
  it('should load a list of 5 potatoes', async () => {
    // fire a request to the back-end, for example by simulating
    // a click on the "load potaotes" button
    
    // This line will throw an Error if the POTATOES_RECEIVED action is not dispatched
    // before 5000 ms, marking the test as failed :
    const potaoesActions = await waitForMiddleware.waitFor(['POTATOES_RECEIVED'], 5000);
    
    // If the action is dispatched, you will now be able to make assertions on its payload :
    
    // waitFor resolves on an array of matching actions, but we only wait for one action to be dispatched, hence this assertion :
    assert(potaoesActions.length === 1);
    const potatoesList = potaoesActions[0].potatoesList;
    assert(potatoesList.length === 5);
  });
});

API

The object returned by the createWaitForMiddleware method exposes the following methods :

async waitFor(actions, timeout)

Wait asynchronously for actions to be dispatched.
Heads up ! If the actions have already been dispatched since the last call to clean, the method will immediately resolve with the payload of the actions found in its records.

  • actions: Array<String> : Actions types you are waiting for.
  • timeout: number : the timeout, in ms, after which waitFor will throw an Error. Default value : 2000.

clean()

Cleans the recorded actions history. This allows to avoid side effects between several test cases (see "Heads up !" above).