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

v0.0.12

Published

Functions to assert dispatched actions in redux

Downloads

559

Readme

redux-asserts

Functions to assert dispatched actions in redux. These functions are aimed at making integration testing with redux easier by abstracting away the need to manage and listen to the store's actions.

How it works

A redux store is created with a wrapper that listens for actions dispatched to it. The assert functions return a Promise which resolves when all expected action types are received, or rejects when an unexpected action type is received.

Installation

npm install redux-asserts --save-dev

Tests

Check out the repository, run npm install, then run npm test.

Usage

In your testing code:

import configureTestStore from 'redux-asserts';
import rootReducer from '../reducers';

Create a redux store for testing using configureTestStore. Then use that store's createListenForActions and createListenForActions functions to create the listenForActions and dispatchThen functions respectively.

listenForActions

Asserts that a list of actions were called during or after the execution of the callback, then resolves the Promise. The Promise will reject on an assertion error, but note that there are cases where the Promise may resolve earlier than the user would want, or the Promise may never be resolved or rejected.

This function takes two required arguments:

listenForActions(actionTypes, callback) => Promise
  • actionTypes: A list of action types to assert. This list must match exactly the types that are dispatched to the store , but the types may be in any order. If there are more action types being expected than actions, the Promise may never resolve. If there are fewer action types, the Promise will resolve since all expected action types were asserted. If at any point there is an unexpected action type the Promise will reject.
  • callback: A function to be executed right after the store starts listening for actions to be asserted. Note that the actions don't need to be dispatched within this function specifically. The store will keep listening until all expected actions are received, or until there is one unexpected action.
  • return value: A Promise which will be resolved or rejected after the action types are asserted.

Example:

describe('cart rendering', () => {
  let listenForActions, store;
  beforeEach(() => {
    store = configureTestStore(rootReducer);
    listenForActions = store.createListenForActions();
  });

  it('clicks the add to cart button', done => {
    listenForActions([UPDATE_CART_ITEMS, UPDATE_CART_VISIBILITY], () => {
      // Click the Update cart button
      let button = TestUtils.findRenderedDOMComponentWithClass(component, "add-to-cart");
      TestUtils.Simulate.click(button);
    }).then(state => {
      done();
    });
  });
});

This runs the function passed into listenForActions, then waits while asserting the types dispatched. If the types are asserted to be dispatched, the promise resolves.

See also the examples in the test directory.

dispatchThen

Dispatches an action or action creator, then asserts a list of action types were called after the dispatch of the action, then resolves the Promise. The Promise will reject on an assertion error, but there are cases where the Promise may resolve earlier than the user would want, or the Promise may never be resolved or rejected.

Note that this function is exactly the same as listenForActions but where the callback is

dispatch(action());

This function takes two required arguments:

dispatchThen(action, actionTypes) => Promise
  • action: The action or action creator, whatever you would pass to dispatch().
  • actionTypes: A list of action types to assert. This list must match exactly the types that are dispatched to the store by action, but the types may be in any order. If there are more action types being expected than actions, the Promise may never resolve. If there are fewer action types, the Promise will resolve since all expected action types were asserted. If at any point there is an unexpected action type the Promise will reject.
  • return value: A Promise which will be resolved or rejected after the action types are asserted.

Example:

describe('course reducers', () => {
  let dispatchThen, store;
  beforeEach(() => {
    // Create a redux store for testing
    store = configureTestStore(rootReducer);
    // Create the dispatchThen function we use below
    dispatchThen = store.createDispatchThen(state => state.courseList);
  });

  it('should fetch a list of courses successfully', done => {
    dispatchThen(fetchCourseList(), [REQUEST_COURSE_LIST, RECEIVE_COURSE_LIST_SUCCESS]).then(courseState => {
      assert.deepEqual(courseState.courseList, COURSE_LIST_DATA);
      assert.equal(courseState.courseListStatus, FETCH_SUCCESS);

      done();
    });
  });
});

This dispatches the fetchCourseList action, asserts that the two action types REQUEST_COURSE_LIST and RECEIVE_COURSE_LIST_SUCCESS were the only actions dispatched, then the promise resolves with the given state.

See also the examples in the test directory.