0
votes

I try to follow the example in the documentation to test epic:

Epic

import { ofType } from 'redux-observable';
import { combineEpics } from 'redux-observable';
import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { switchMap } from 'rxjs/add/operator/switchMap';
import { from } from 'rxjs/observable/from';
import { of } from 'rxjs/observable/of';
import { fromPromise } from 'rxjs/observable/fromPromise';

import { 
  getTopStories
} from '../../../utils/service-helper';

import { type, actions } from './action';

export const getHackernewsStoryEpic = (action$, store) => 
  action$.ofType(type.GET_HACKERNEWS_STORIES_REQUEST)
          .switchMap(
            action =>  { 
              return from(getTopStories())
                        .takeUntil(action$.ofType(type.GET_HACKERNEWS_STORIES_REQUEST_CANCEL))
                        .map(result => actions.getHackernewsStoriesRequestSuccess(result))
                        .catch((error) => actions.getHackernewsStoriesRequestFailure(error))
            }
        );

export default combineEpics(
  getHackernewsStoryEpic
);

Get getTopStories is service call which talks to hackernews API:

export const getTopStories = async () => await getRequest('/topstories.json');

My test looks like this:

describe('Hackernews stories epic', () => {
  describe('getHackernewsStoryEpic', () => {
    let store;

    beforeEach(() => {
      store = mockStore();
    });

    afterEach(() => {
      nock.cleanAll();
      epicMiddleware.replaceEpic(storiesEpic);
    });
    it('should return success on request success', async () => {
      store.dispatch({ type: type.GET_HACKERNEWS_STORIES_REQUEST });
      expect(store.getActions()).toEqual([
        { type: type.GET_HACKERNEWS_STORIES_REQUEST },
        { type: type.GET_HACKERNEWS_STORIES_SUCCESS }
      ]);
    });
  });
});

Looking at the test it fails as one action is trigger and getTopStories() is never trigger (nock is not complaining that there is no mock) and not getting next action. I think I missing something as from should run async call?

1

1 Answers

0
votes
 it('should return success on request success',  async () => {
      const mock = require('../../../../data/hackernews/topstories.json');

      nock(__ROOT_API__)
        .defaultReplyHeaders({ 'access-control-allow-origin': '*' })
        .get('/topstories.json')
        .reply(200, mock);

        const action$ = ActionsObservable.of(
          {type: type.GET_HACKERNEWS_STORIES_REQUEST}
        );
        const expectedAction = [actions.getHackernewsStoriesRequestSuccess(mock)]
        await getHackernewsStoryEpic(action$)
          .toArray()
          .toPromise()
          .then(actualOutputActions => {
             expect(actualOutputActions).toEqual(expectedAction)
        });
    });