npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-saga-testable

v1.0.5

Published

Make redux-saga more easily testable

Downloads

24

Readme

redux-saga-testable

npm node peer peer travis ci coveralls downloads license

Make redux-saga more easily testable.

Overview

redux-saga-testable is a unit test library for redux-saga that runs your sagas without the need to iterate on the generators yourself, allowing you to map some effects to values and providing the built-in assertions you need.

Contents

Documentation

Motivation

It's about redux-saga and unit tests.

When you write unit tests of sagas, you have to manually iterate on the generator and pass your next value for each yielded effect. The number of iterations you have to next() depends on the number of yield in the saga.

If a yield statement is added, removed or just swapped with another, there is a good chance that you pass your next value to an unexpected effect. This causes tests boring to write, not easy to understand and too prone to breaking.

Ideally, you should not have to worry about what the saga does behind. You would just like to map some effects to some values and assert that some arbitrary effects happened, in a clear and concise way.

If that speaks to you, go to Getting started.

Getting started

Install

Get it with npm:

npm i -D redux-saga-testable

or yarn:

yarn add -D redux-saga-testable

Usage

Considering the following saga:

function* fetchUserWorker(action: FetchUserAction) {
  const { userId } = action.payload;

  yield put({ type: 'FETCH_USER_REQUEST' });

  let user = yield select(selectors.getCurrentUser);
  if (user !== undefined) return;

  user = yield call(services.getUserById, userId);
  yield put({ type: 'FETCH_USER_SUCCESS', payload: user });
}

This saga fetches a user and dispatches FETCH_USER_SUCCESS. Note that if the user already exists, it does nothing instead of calling services.getUserById().

You would like to assert that this saga dispatches the action FETCH_USER_SUCCESS when the call to services.getUserById() returns a user:

import { createRunner } from 'redux-saga-testable';

test('fetchUserWorker() should dispatch FETCH_USER_SUCCESS', () => {
  const userId = 123;
  const user = { user: 'name' };

  createRunner(fetchUserWorker, { type: 'FETCH_USER', payload: { userId } })
    .map(call(services.getUserById, userId), user)
    .should.put({ type: 'FETCH_USER_SUCCESS', payload: user });
});

Let's see what happens step by step:

  • import { createRunner } from 'redux-saga-testable';

    Imports the saga runner creator function.

  • createRunner(fetchUserWorker, { type: 'FETCH_USER', payload: { userId } })

    Creates a saga runner instance from the saga fetchUserWorker and its action.

  • .map(call(services.getUserById, userId), user)

    Maps the effect call(services.getUserById, id) to the value user.

  • .should.put({ type: 'FETCH_USER_SUCCESS', payload: user });

    Asserts that the saga yields the effect put({ type: 'FETCH_USER_SUCCESS', payload: user }).

The test will pass if the runner can make the given assertions. Otherwise an error will be thrown and the test will fail.

See the Tutorial for more advanced examples or see the API documentation.

Contribute

Pull requests are welcome.

To make one, fork this repository, clone it to your local and run npm install. Do your changes. Once you are done, commit and push to your forked repository and make your pull request.

See current issues but feel free to bring what you need.

License

MIT