项目作者: doniyor2109

项目描述 :
Testing redux never been easy
高级语言: JavaScript
项目地址: git://github.com/doniyor2109/jest-redux.git
创建时间: 2018-11-14T11:24:01Z
项目社区:https://github.com/doniyor2109/jest-redux

开源协议:MIT License

下载


jest-redux

Jest matchers for testing redux easier and more declaritive.


Build Status
GitHub

Table of Contents

Getting start

Installation

  1. yarn add --dev jest-redux

or

  1. npm install --save-dev jest-redux

Setup

Via setupTestFrameworkScriptFile config

Add jest-redux to your Jest setupTestFrameworkScriptFile configuration

  1. "jest": {
  2. "setupTestFrameworkScriptFile": "jest-redux"
  3. }

Via setupTest script

Require jest-redux from setupTest script

  1. // ./setupTest.js
  2. require('jest-redux');

Then add this config

  1. "jest": {
  2. "setupTestFrameworkScriptFile": "./setupTest.js"
  3. }

Usage

Test your standart redux actions and reducers with one of the jest-redux matchers.

  1. const initialState = { number: 0 };
  2. function reducer(state = initialState, action) {
  3. switch(action.type) {
  4. case "ADD":
  5. return state.number + action.payload;
  6. case "REMOVE":
  7. return state.number + action.payload;
  8. default:
  9. return state;
  10. }
  11. }
  12. const add = (number) => ({ type: "ADD", payload: number });
  13. const remove = (number) => ({ type: "REMOVE", payload: number });
  14. expect(reducer).toHaveInitialState(initialState); // Passes tests
  15. const readyActions = {
  16. add: () => add(1),
  17. remove: () => remove(1),
  18. };
  19. expect(reducer).toHandleActions(readyActions); // Passes tests

API Reference

toHandleActions(reducer, actions)

Checks reducer to handle given action creators. Compares snpashots of returned reducer result for given actions.

Passed actions should not expect arguments

Example

  1. expect(reducer).toHandleActions(actions);

toHaveInitialState(reducer, initialState)

Checks reducer against given initialState.

Example

  1. expect(reducer).toHaveInitialState(initialState);

toMatchActionSnapshot(actions)

Checks given action creators to match with previous snapshot.

Passed actions should not expect arguments

Example

  1. expect(actions).toMatchActionSnapshot();

License

MIT