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-mocha-test-generators

v1.0.0

Published

A collection of methods to generate tests for redux actions, reducers, etc.

Downloads

8

Readme

redux-mocha-test-generators

Methods for testing redux action creators, async action creators, and reducers with mocha to avoid repetitive boilerplate.

Build Status Code Climate Test Coverage

Installation

npm install redux-mocha-test-generators

Usage

Table Of Contents

Testing action creators

Call the actionCreator method by passing the action creator you want to test and then chain the test methods to the result.

You can test that an actionCreator creates the correct action

import {actionCreator} from 'redux-mocha-test-generators';
import {SOME_ACTION_NO_ARGS, someActionNoArgs, SOME_ACTION_WITH_ARGS, someActionWithArgs} from 'actions';

actionCreator(someActionNoArgs)
    .shouldCreateAction({type: SOME_ACTION_NO_ARGS});

// wrap the it assertion in a describe block using the name of the action creator
actionCreator(someActionNoArgs)
    .wrapInDescribe(true)
    .shouldCreateAction({type: SOME_ACTION_NO_ARGS});

// calling withArgs tells the method that the arguments list needs to be passed 
// to the action creator. In this case a call to someActionWithArgs(1, 2) should create the action in shouldCreateAction.
actionCreator(someActionWithArgs)
    .withArgs(1, 2)
    .shouldCreateAction({type: SOME_ACTION_WITH_ARGS, arg1: 1, arg2: 2});

Testing async action creators

Call the asyncActionCreator method by passing the action creator you want to test and then chain the test methods to the result.

You can test the actions dispatched for both successful and unsuccessful async calls.

NOTE: your async action creators must return a promise for this to work

import {asyncActionCreator} from 'redux-mocha-test-generators';
import {someAsyncActionCreator} from 'async-action-creators';
import {someMockFn} from 'mocks';

const actions = [
    {type: 'SOME_ACTION'}
];

// checks if dispatching someAsyncActionCreator dispatches actions with optional 
// custom message for assertion
asyncActionCreator(someAsyncActionCreator)
    .shouldDispatchActions(actions, "should blah blah blah");

// checks if dispatching someAsyncActionCreator(1, 2, 3) dispatches actions
asyncActionCreator(someAsyncActionCreator)
    .withArgs(1, 2, 3)
    .shouldDispatchActions(actions);

// checks if dispatching someAsyncActionCreator(1, 2, 3) dispatches actions when 
// the code in setUp has been executed previously. setUp is a good place to set up mocks.
asyncActionCreator(someAsyncActionCreator)
    .setUp(() => {
        const promise = new Promise(resolve => resolve());
        someMockFn.withArgs(1, 2, 3).returns(promise);
    })
    .withArgs(1, 2, 3)
    .shouldDispatchActions(actions);

const failureActions = [
    {type: 'SOME_ACTION_FAILED'}
];

// checks if dispatching someAsyncActionCreator(1, 2, 3) dispatches actions when the async method fails and  
// the code in setUp has been executed previously. setUp is a good place to set up mocks.
asyncActionCreator(someAsyncActionCreator)
    .success(false)
    .setUp(() => {
        const promise = new Promise((resolve, reject) => reject());
        someMockFn.withArgs(1, 2, 3).returns(promise);
    })
    .withArgs(1, 2, 3)
    .shouldDispatchActions(failureActions);

Testing reducers

Call the reducer method by passing in the reducer you want to test and then chain the test methods to the result.

You can test that a reducer is returning the correct initial state

import {reducer} from 'redux-mocha-test-generators';
import {someReducer} from 'reducers';

reducer(someReducer)
    .shouldReturnTheInitialState(1);

You can test that a reducer handles an action correctly by passing in the action, the expected value, an optional previous value, and an optional message.

import {reducer} from 'redux-mocha-test-generators';
import {SOME_ACTION} from 'actions';
import {someReducer} from 'reducers';

// required parameters
reducer(someReducer)
    .shouldHandleAction({type: SOME_ACTION, val: 1}, 1);

// with a previous value (2)
reducer(someReducer)
    .shouldHandleAction({type: SOME_ACTION, val: 1}, 1, 2);

// with a previous value and the message for the mocha it assertion
reducer(someReducer)
    .shouldHandleAction({type: SOME_ACTION, val: 1}, 1, 2, 'should set the value to 1');

You can also chain together a shouldReturnTheInitialState and multiple shouldHandleAction methods.

import {reducer} from 'redux-mocha-test-generators';
import {SOME_ACTION, ANOTHER_ACTION} from 'actions';
import {someReducer} from 'reducers';

// this would execute three tests
reducer(someReducer)
    .shouldReturnTheInitialState(1)
    .shouldHandleAction({type: SOME_ACTION, val: 1}, 1)
    .shouldHandleAction({type: ANOTHER_ACTION}, val: 3}, 3);

Examples

The examples directory contains examples of all three types of tests using actual reducers, action creators, and async action creators.

License

MIT