Coder Social home page Coder Social logo

jest-redux's Introduction

jest-redux

Jest matchers for testing redux easier and more declaritive.

Build Status GitHub

Table of Contents

Getting start

Installation

yarn add --dev jest-redux

or

npm install --save-dev jest-redux

Setup

Via setupTestFrameworkScriptFile config

Add jest-redux to your Jest setupTestFrameworkScriptFile configuration

"jest": {
  "setupTestFrameworkScriptFile": "jest-redux"
}

Via setupTest script

Require jest-redux from setupTest script

// ./setupTest.js

require('jest-redux');

Then add this config

"jest": {
  "setupTestFrameworkScriptFile": "./setupTest.js"
}

Usage

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

const initialState = { number: 0 };

function reducer(state = initialState, action) {
 switch(action.type) {
   case "ADD":
    return state.number + action.payload;
   case "REMOVE":
    return state.number + action.payload;
   default:
     return state;
 }
}

const add = (number) => ({ type: "ADD", payload: number });
const remove = (number) => ({ type: "REMOVE", payload: number });


expect(reducer).toHaveInitialState(initialState); // Passes tests

const readyActions = {
  add: () => add(1),
  remove: () => remove(1),
};

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

expect(reducer).toHandleActions(actions);

toHaveInitialState(reducer, initialState)

Checks reducer against given initialState.

Example

expect(reducer).toHaveInitialState(initialState);

toMatchActionSnapshot(actions)

Checks given action creators to match with previous snapshot.

Passed actions should not expect arguments

Example

expect(actions).toMatchActionSnapshot();

License

MIT

jest-redux's People

Contributors

dependabot[bot] avatar doniyor2109 avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.