[Redux-Observable && Unit testing] Testing the output of epics

Epics can be unit-tested just like any other function in your application - they have a very specific set of inputs (the action$ stream) and the output is always an Observable. We can subscribe to this output Observable to assert that the actions going back into the Redux are the ones we expect.

export function fetchUserEpic(action$) {
  return action$.ofType('FETCH_USER')
    .map(action => ({
      type: 'FETCH_USER_FULFILLED',
      payload: {
        name: 'Shane',
        user: action.payload
      }
    }))
}
import {Observable} from 'rxjs';
import {ActionsObservable} from 'redux-observable';
import {fetchUserEpic} from "./fetch-user-epic";
it('should return correct actions', function () {
  const action$ = ActionsObservable.of({
    type: 'FETCH_USER',
    payload: 'shakyshane'
  });

  const output$ = fetchUserEpic(action$);
  output$.toArray().subscribe(actions => {
    expect(actions.length).toBe(1);
  });
});
原文地址:https://www.cnblogs.com/Answer1215/p/7683195.html