Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to unit test redux-observable epic #580

Open
tzurae opened this issue Oct 22, 2018 · 1 comment
Open

Unable to unit test redux-observable epic #580

tzurae opened this issue Oct 22, 2018 · 1 comment

Comments

@tzurae
Copy link

tzurae commented Oct 22, 2018

I'm trying to write unit test for following epic

const fetchUserSuccess = data => ({
  type: authActionTypes.FACEBOOK_LOGIN_SUCCESS,
  payload: data,
});

const fetchUserFailure = error => ({
  type: authActionTypes.FACEBOOK_LOGIN_FAILURE,
  payload: error,
});

const saveUserInfoSuccess = data => ({
  type: profileActionTypes.SAVE_USER_PROFILE_SUCCESS,
  payload: data,
});

const navigate = routerName => NavigationService.reset({
  routeName: routerName,
});

export const facebookLoginEpic = action$ => action$.pipe(
  ofType(authActionTypes.FACEBOOK_LOGIN_INIT),
  switchMap(() => from$(FacebookSDK.fbLogin()).pipe(
    mergeMap(data=> {
      navigate('DRAWER');
      return of$(
        fetchUserSuccess(data),
        saveUserInfoSuccess(data),
      );
    }),
    catchError(error => fetchUserFailure(error)),
  )),
);

test.js

import { ActionsObservable } from 'redux-observable';
import * as authActionTypes from '../authTypes';
import { facebookLoginEpic } from '../authEpics';

it('Should return the fetchUserSuccess and saveUserInfoSuccess actions', () => {
  const action$ = ActionsObservable.of({
    type: authActionTypes.FACEBOOK_LOGIN_INIT,
  });

  const output$ = facebookLoginEpic(action$);
  console.log(output$)
});

I'm trying to log the output$, but always get the error

xxxxxxx/node_modules/rxjs/internal/util/hostReportError.js:4
    setTimeout(function () { throw err; });
                             ^

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    at Object.<anonymous>.exports.subscribeTo (/Users/rae/Project/partido/node_modules/rxjs/internal/util/subscribeTo.js:42:15)
    at Object.subscribeToResult (/Users/rae/Project/partido/node_modules/rxjs/internal/util/subscribeToResult.js:10:26)
    at CatchSubscriber.Object.<anonymous>.CatchSubscriber.error (/Users/rae/Project/partido/node_modules/rxjs/internal/operators/catchError.js:57:33)
    at MergeMapSubscriber.Object.<anonymous>.Subscriber._error (/Users/rae/Project/partido/node_modules/rxjs/internal/Subscriber.js:96:26)
    at MergeMapSubscriber.Object.<anonymous>.Subscriber.error (/Users/rae/Project/partido/node_modules/rxjs/internal/Subscriber.js:74:18)
    at /Users/rae/Project/partido/node_modules/rxjs/internal/util/subscribeToPromise.js:10:43
    at tryCallOne (/Users/rae/Project/partido/node_modules/promise/lib/core.js:37:12)
    at /Users/rae/Project/partido/node_modules/promise/lib/core.js:123:15
    at flush (/Users/rae/Project/partido/node_modules/asap/raw.js:50:29)
    at process._tickCallback (internal/process/next_tick.js:172:11)
error Command failed with exit code 1.

Is there anything I did wrong in my test or epic?

@evertbouw
Copy link
Member

You should return an observable-like in the catchError operator. Put the action in an array or pass it to of()

catchError(error => of$(fetchUserFailure(error))),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants