All Projects → reduxjs → Redux Mock Store

reduxjs / Redux Mock Store

Licence: mit
A mock store for testing Redux async action creators and middleware.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Redux Mock Store

Testen
✔️ Run tests for multiple versions of Node.js in local env.
Stars: ✭ 176 (-92.77%)
Mutual labels:  test
Gradle Testsets Plugin
A plugin for the Gradle build system that allows specifying test sets (like integration or acceptance tests).
Stars: ✭ 182 (-92.53%)
Mutual labels:  test
Split Folders
🗂 Split folders with files (i.e. images) into training, validation and test (dataset) folders
Stars: ✭ 203 (-91.66%)
Mutual labels:  test
Preact Render Spy
Render preact components with access to the produced virtual dom for testing.
Stars: ✭ 178 (-92.69%)
Mutual labels:  test
Zson
专为测试人员打造的JSON解析器
Stars: ✭ 181 (-92.57%)
Mutual labels:  test
Hitchhiker
a Restful Api test tool
Stars: ✭ 2,175 (-10.68%)
Mutual labels:  test
Snap Shot
Jest-like snapshot feature for the rest of us, works magically by finding the right caller function
Stars: ✭ 170 (-93.02%)
Mutual labels:  test
Softest
Recording Browser Interactions And Generating Test Scripts.
Stars: ✭ 208 (-91.46%)
Mutual labels:  test
Buildbuddy
BuildBuddy is an open source Bazel build event viewer, result store, and remote cache.
Stars: ✭ 182 (-92.53%)
Mutual labels:  test
Mocha.parallel
Run async mocha specs in parallel
Stars: ✭ 194 (-92.03%)
Mutual labels:  test
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (-92.65%)
Mutual labels:  test
Video Recorder Java
This library allows easily record video of your UI tests by just putting couple annotations.
Stars: ✭ 179 (-92.65%)
Mutual labels:  test
Dbsetup
An API for populating a database in unit tests
Stars: ✭ 190 (-92.2%)
Mutual labels:  test
Vscode Java Test
Run and debug Java test cases in Visual Studio Code.
Stars: ✭ 177 (-92.73%)
Mutual labels:  test
Androidunittest
Save time & clear your unit tests on Android !
Stars: ✭ 205 (-91.58%)
Mutual labels:  test
Reactor Addons
Official modules for the Reactor project
Stars: ✭ 175 (-92.81%)
Mutual labels:  test
Grunt Saucelabs
Grunt task for running all your browser tests using Sauce Labs
Stars: ✭ 182 (-92.53%)
Mutual labels:  test
Cornichon
Scala DSL for testing HTTP JSON API
Stars: ✭ 211 (-91.33%)
Mutual labels:  test
Mgodatagen
Generate random data for MongoDB
Stars: ✭ 206 (-91.54%)
Mutual labels:  test
Correlation
🔗 Methods for Correlation Analysis
Stars: ✭ 192 (-92.11%)
Mutual labels:  test

redux-mock-store Circle CI

npm

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

Please note that this library is designed to test the action-related logic, not the reducer-related one. In other words, it does not update the Redux store. If you want a complex test combining actions and reducers together, take a look at other libraries (e.g., redux-actions-assertions). Refer to issue #71 for more details.

Install

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

Or

yarn add redux-mock-store --dev

Usage

Synchronous actions

The simplest usecase is for 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 actions 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 actions

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

The MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].