Skip to content

Latest commit

 

History

History
92 lines (66 loc) · 2.3 KB

testing.md

File metadata and controls

92 lines (66 loc) · 2.3 KB

Testing Sagas

store-saga ships with a testing helper called SagaTester that extends SagaRunner. It exposes some helpful methods to interact with a saga effect and make assertions about the effect's resultant stream.

Extending from our authEffect example written in the Getting Started guide, let's write a test that ensures the effect is responding correctly to dispatched actions:

Note: for this example I am using respond-ng, a simple Http mocking library I wrote to make unit testing Http calls easier

import {Injector} from '@angular/core';
import RESPOND_PROVIDERS, {Respond} from 'respond-ng';
import {SagaTester} from 'store-saga/testing';

import {authEffect} from './auth-effect';

describe('Authentication Effect', () => {
  let sagaTester: SagaTester;
  let respond: Respond;

  beforeEach(() => {
    const injector = Injector.resolveAndCreate([
      SagaTester,
      RESPOND_PROVIDERS
    ]);

    sagaTester = injector.get(SagaTester);
    respond = injector.get(Respond);

    sagaTester.run(authEffect);
  });

  it('should auth a user when an auth action is dispatched', () => {
    const payload = { username: 'Mike', password: 'test' };
    respond.ok().when.post('/auth', payload)

    sagaTester.sendAction({ type: 'AUTH', payload });

    expect(sagaTester.last).toEqual({ type: 'AUTH_SUCCESS' });
  });
});

SagaTester API

Parameters

output

Output stream of all running sagas.

Type BehaviorSubject

sagaTester.output.subscribe(action => { });

last

Most recent action dispatched from a saga

Type Action

expect(sagaTester.last).toEqual({ type: 'INCREMENT' });

Methods

sendAction(action)

Sends an action and an empty state object to all running sagas.

Params

  • action Action Action object to dispatch to sagas
sagaTester.sendAction({ type: 'INCREMENT' });

sendState(state)

Sends state and an empty action object to all running sagas.

Params

  • state any State object to dispatch to sagas
sagaTester.sendState({ counter: 3 });

send(state, action)

Sends both state and action objects to all running sagas.

Params

  • state any State object to dispatch to sagas
  • action Action Action object to dispatch to sagas
sagaTester.send({ counter: 3 }, { type: 'INCREMENT' });