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 🙏

© 2025 – Pkg Stats / Ryan Hefner

redux-jest-helper

v1.0.1

Published

Provides easily testable classes that generate redux-middlewares or -reducers

Downloads

5

Readme

ReduxJestHelper

Provides an easy way to create and test Redux Reducers and Middlewares.

Installation

Using yarn you only need to execute

yarn add redux-jest-helper

or if you are using npm, execute

npm install redux-jest-helper --save

Usage

Middleware

Implement a MiddlewareFactory<S>. The abstract class provides an interface for 3 functions that can be implemented:

  • onBeforeAction?: (api: MiddlewareAPI<S>, action: Action) => void
  • onAfterAction?: (api: MiddlewareAPI<S>, action: Action, prevState: S) => void
  • onCreateMiddleware?: (api: MiddlewareAPI<S>) => void

Example:

import { MiddlewareFactory } from 'redux-jest-helper'

class CounterMiddlewareFactory extends MiddlewareFactory<CounterState> {
  onBeforeAction = (api: MiddlewareAPI<CounterState>, action: Action) => {
    switch (action.type) {
      case 'INCREMENT':
        this.onIncrement(action as IncrementAction) // do something before an INCREMENT action
        break
    }
  }

  onAfterAction = (api: MiddlewareAPI<CounterState>, action: Action, prevState: CounterState) => {
    switch (action.type) {
      case 'DECREMENT':
        this.afterDecrement(action as DecrementAction) // do something after a DECREMENT action
        break
    }
  }
}

Testing:

The following example will test, if the given methods are called on the given actions.

import { middlewareTestHelper } from 'redux-jest-helper'

middlewareTestHelper(new CounterMiddlewareFactory(), [
  { actionType: 'INCREMENT', methods: ['onIncrement'] },
  { actionType: 'DECREMENT', methods: ['afterDecrement'] }
])

Now that you tested if the methods are really called when the according action happens you only need to unit-test the methods themselves.

Reducer

Implement a ReducerFactory<S>. The abstract class provides an interface for onAction: (state: S | undefined, action: Action) => S.

Example:

class CounterReducerFactory extends ReducerFactory<CounterState> {
  onIncrement = (state: CounterState, action: IncrementAction) => {
    return {
      ...state,
      count: state.count + action.amount
    }
  }

  onDecrement = (state: CounterState, action: DecrementAction) => {
    return {
      ...state,
      count: state.count - action.amount
    }
  }

  onAction = (state: CounterState = initialState, action: Action) => {
    let newState = state

    switch (action.type) {
      case 'DECREMENT':
        newState = this.onDecrement(state, action as DecrementAction)
        break
      case 'INCREMENT':
        newState = this.onIncrement(state, action as IncrementAction)
        break
    }

    return newState
  }
}

Testing:

The following example will test, if the given methods are called on the given actions with the given payload.

import { reducerTestHelper } from 'redux-jest-helper'

describe('reducerTestHelper', () => {
  reducerTestHelper(new TestReducerFactory(), initialState, [
    { actionType: 'INCREMENT', actionValue: { amount: 1 }, methods: 'onIncrement' },
    { actionType: 'DECREMENT', actionValue: { amount: 1 }, methods: 'onDecrement' }
  ])
})

Now that you tested if the methods are really called when the according action happens you only need to unit-test the methods themselves.

Adding everything to the store

const counterReducerFactory = new CounterReducerFactory()
const counterMiddlewareFactory = new CounterMiddlewareFactory()

const rootReducer = combineReducers({
  counter: counterReducerFactory.toReducer()
})

createStore(rootReducer, applyMiddleware(counterMiddlewareFactory.toMiddleware()))