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

v0.4.10

Published

A library to testing your redux application

Downloads

50

Readme

redux-testing

Tests Build npm version

A library to testing your Redux application.

Features:

  • Reset store's state
  • Update any part of store's state
  • Record and check recorded actions

Installation

npm install --save-dev redux-testing

Initialization

First you have to install testEnhancer to your store:

import { createStore } from 'redux'
import testEnhancer from 'redux-testing'

// A simple reducer for an example
const reducer = (state = 0, action) => {
    switch (action.type) {
    case 'INCREMENT':
        return state + 1;
    case 'DECREMENT':
        return state - 1;
    default:
        return state;
    }
};

// Initialize a store with the testEnhancer
const store = createStore(reducer, undefined, testEnhancer)

If you are using middlewares or other enhancers

import { createStore, compose, applyMiddleware } from 'redux'

// Prepare middlewares
let enhancer = applyMiddleware(middleware1, middleware2)

// Optionally apply the testEnhancer for testing
if (process.env.NODE_ENV == 'DEVELOPMENT') enhancer = compose(testEnhancer, enhancer)

// Initialize a store with the enhancer
const store = createStore(reducer, undefined, enhancer)

For correct usage with redux-thunk add import 'redux-thunk/extend-redux' to your store file.

Usage

Reset store

Reset you store and actions log any time:

// Lets make some actions
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'DECREMENT' })

// State has been changed
expect(store.getState()).toBe(1) // 0 + 1 + 1 - 1 = 1

store.reset()

expect(store.getState()).toBe(0) // initial state

Update store state

Customize your store state:

store.update(42)
expect(store.getState()).toBe(42)

/** Other examples
*
* // Update by object
* store.update({deep: {child: {state: { value: 42}}}})
*
* // Update by path
* store.update(42, 'deep.child.state.value')
*/

Using with Immer

Immer freezes your state. If you want to store.update is working, be sure that you have disable the autofreezing in your tests.

// Setup tests globally
import { setAutoFreeze } from 'immer'

setAutoFreeze(false)

Get recorded actions

// Lets make some actions
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'DECREMENT' })

const actions = store.getActions()
expect(actions).toEqual([
  {type: 'INCREMENT'}, {type: 'INCREMENT'}, {type: 'DECREMENT'}
])

Get an recorded action

// Lets make some actions
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'DECREMENT' });

let action;

// Get latest action
action = store.getAction();
expect(action).toEqual({ type: 'DECREMENT' });

// Get action by index
action = store.getAction(0);
expect(action).toEqual({ type: 'INCREMENT' });
action = store.getAction(2);
expect(action).toEqual({ type: 'DECREMENT' });

// Get relative action
action = store.getAction(-1); // previous before latest
expect(action).toEqual({ type: 'INCREMENT' });
action = store.getAction(-2); // previous before above
expect(action).toEqual({ type: 'INCREMENT' });

Clear actions

// Lets make some actions
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'DECREMENT' });

// Ensure that we have actions recorded
expect(store.getActions()).toBeTruthy();

// Reset the recorded actions
store.clearActions();

// Ensure that we have cleared the records
expect(store.getActions()).toEqual([]);

License

This project is licensed under the MIT license, Copyright (c) 2017 Kirill Klenov. For more information see LICENSE.md.

Acknowledgements

Dan Abramov for Redux