Code Monkey home page Code Monkey logo

redux-mock-saga's Introduction

Build Status Dependencies NPM Version codecov

redux-mock-saga

Elegant testing strategy to check what your saga's really do, i.e. testing the result of the side effects.

Why?

There are different strategies for saga testing. Some use snapshots, others check what's in the generator. Or in many projects, they are untested altogether.

I found many testing strategies insuficient where I just wanted to test the saga as a function: f(saga, params) => [state, side-effects].

What it does

This library helps you to setup a full functional SagaMiddleware with a mocked store and a custom state.

What it doesn't

However it doesn't help you mock all the side effects like API-calls, and other objects which you need. A test might be simple, the amount of fixtures can be complicated.

Test setup

What do you need?

  • Jest
  • This package
  • App using redux and redux-saga

(All samples in Typescript/Flow)

import { mockStoreFactory, ReduxSagaMock, runSagaMock, stopSagaMock } from "redux-mock-saga";

describe("some saga", () => {
    // Declare test-wide variables
    let sagaMock: ReduxSagaMock;

    beforeEach(() => {
        // Create the middleware and mockStore, provide the effective store used in your application
        // and an initial state (optional)
        sagaMock = mockStoreFactory(rootReducer, store, someInitialState);
    });

    it("Base test", async () => {
        // Arrange
        const done = runSagaMock(sagaMock, yourSagaToTest);
        // const done = runSagaMock(sagaMock, yourSagaToTest, someOtherSagaToTest,...)

        // Act ... dispatch actions ...
        // sagaMock.mockStore.dispatch(createSomeAction(...));

        // Await
        stopSagaMock(sagaMock);
        await done;

        // Assert ... assert calls on api, state changes ...
    });
});

Let's split that in parts. First of all the the mock is generated via mockStoreFactory before every test. The mockStoreFactory returns an ReduxSagaMock object containing:

  • mockSagaMiddleware: Mocked saga middleware.
  • mockStore: the mocked store object. Use this object to get the state mockStore.getState() to assert state changes
  • actionHistory: an array of dispatched actions in order of dispatching

Then you start testing sagas. Also remark that the test method should be async! There are a few obligatory things in a test. First you should start the sagas to test via runSagaMock. Provide the factory-generated mock sagaMock and the saga (worker) you want to test. You get a promise in return. Read on.

Then you can dispatch whatever you want to trigger the running saga. After you dispatched your actions the running saga middleware must be told to stop. Thats done in the await block above. stopSagaMock to dispatch the END signal. Then await the promise done.

Finally, you can assert the results.

Typings

The project provides Typescript and Flow typings.

redux-mock-saga's People

Contributors

giespaepen avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

rombit-be

redux-mock-saga's Issues

stopSagaMock is dispatched to early

The stopSagaMock ends the saga middleware immediately and too early in many cases. Add a little timeout to push the END signal to the end.

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.