dmitry-zaets/redux-mock-store


A mock store for your testing your redux async action creators and middleware

License: MIT

Language: JavaScript

Keywords: javascript, redux, test


redux-mock-store Circle CI

npm

A mock store for testing your redux async action creators and middleware. The mock store will create an array of dispatched actions which serve as an action log for tests.

Install

npm install redux-mock-store --save-dev

Usage

Synchronous action

The simplest usecase is for the synchronous actions. In this example, we will test if the addTodo action returns the right payload. redux-mock-store saves all the dispatched actions inside the store instance. You can get all the action by calling store.getActions(). Finally, you can use any assertion library to test the payload.

import configureStore from 'redux-mock-store' //ES6 modules
const { configureStore } = require('redux-mock-store') //CommonJS

const middlewares = []
const mockStore = configureStore(middlewares)

// You would import the action from your codebase in a real scenario
const addTodo = () => ({ type: 'ADD_TODO' })

it('should dispatch action', () => {

  // Initialize mockstore with empty state
  const initialState = {}
  const store = mockStore(initialState)

  // Dispatch the action
  store.dispatch(addTodo())

  // Test if your store dispatched the expected actions
  const actions = store.getActions()
  const expectedPayload = { type: 'ADD_TODO' }
  expect(actions).toEqual([expectedPayload])
})

Asynchronous action

A common usecase for an asynchronous action is a HTTP request to a server. In order to test those types of actions, you will need to call store.getActions() at the end of the request.

import configureStore from 'redux-mock-store'
import thunk from 'redux-thunk'

const middlewares = [thunk] // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares)

// You would import the action from your codebase in a real scenario
function success() {
  return {
    type: 'FETCH_DATA_SUCCESS'
  }
}

function fetchData () {
  return dispatch => {
    return fetch('/users.json') // Some async action with promise
      .then(() => dispatch(success()))
  };
}

it('should execute fetch data', () => {
  const store = mockStore({})

  // Return the promise
  return store.dispatch(fetchData())
    .then(() => {
      const actions = store.getActions()
      expect(actions[0]).toEqual(success())
    })
})

API

configureStore(middlewares?: Array) => mockStore: Function

Configure mock store by applying the middlewares

mockStore(getState?: Object,Function) => store: Function

Returns an instance of the configured mock store. If you want to reset your store after every test, you should call this function.

store.dispatch(action) => action

Dispatches an action through the mock store. The action will be stored in an array inside the instance and executed.

store.getState() => state: Object

Returns the state of the mock store

store.getActions() => actions: Array

Returns the actions of the mock store

store.clearActions()

Clears the stored actions

store.subscribe(callback: Function) => unsubscribe: Function

Subscribe to the store

store.replaceReducer(nextReducer: Function)

Follows the redux API

Old version (< 1.x.x)

https://github.com/arnaudbenard/redux-mock-store/blob/v0.0.6/README.md

Versions

The following versions are exposed by redux-mock-store from the package.json:

  • main: commonJS Version
  • module/js:next: ES Module Version
  • browser : UMD version

License

MIT

Project Statistics

Sourcerank 18
Repository Size 113 KB
Stars 1,471
Forks 95
Watchers 18
Open issues 10
Dependencies 499
Contributors 27
Tags 11
Created
Last updated
Last pushed

Top Contributors See all

Dmitry Zaets Arnaud Bénard Brian Vaughn Sam Bason Kyle Lamse David Jensen Mayank Jethva Braulio Diez Tom Beddard Haroen Viaene Alexandre Lima Ben Neill Julian Fell Chris Blossom Tom Anthony Ng Dylan Parry Sebastian Thiel Jacob Rask knewzen

Packages Referencing this Repo

redux-mock-store-jasmine
A mock store for your testing your redux async action creators and middleware. This is a fork of ...
Latest release 0.0.1 - Published - 1.47K stars
redux-mock-store
A mock store for testing your redux async action creators and middleware
Latest release 1.5.3 - Updated - 1.47K stars
redux-mock-store-jest
[![Circle CI](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master.svg?style=svg)](h...
Latest release 0.0.2 - Published - 1.47K stars

Recent Tags See all

v1.5.3 June 13, 2018
v1.5.0 January 26, 2018
v1.4.0 November 05, 2017
v1.2.3 April 09, 2017
v1.0.3 May 20, 2016
v1.0.2 March 10, 2016
v1.0.1 March 10, 2016
v1.0.0 February 25, 2016
v0.0.6 January 08, 2016
v0.0.5 January 08, 2016
v0.1.0 January 08, 2016

Interesting Forks See all

jedmao/redux-mock-store
A mock store for testing Redux async action creators and middleware.
TypeScript - MIT - Last pushed - 2 stars - 1 forks

Something wrong with this page? Make a suggestion

Last synced: 2018-01-26 19:44:56 UTC

Login to resync this repository