vendredi 28 décembre 2018

Mock navigation service in jest

I'm using react-navigation and calling that service in my code. Although I'm not sure how to mock the navigate function. Here is my code:

import { NavigationActions } from 'react-navigation';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function navigate(routeName, params) {
  _navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params,
    })
  );
}

// add other navigation functions that you need and export them

export default {
  navigate,
  setTopLevelNavigator,
};

Here is what I got so far:

export const loginEpic = (action$, state$, { ajax, navigate }) =>
  action$.pipe(
    ofType(LOGIN_REQUEST),
    map(() => state$.value),
    switchMap((options) =>
      ajax(options).pipe(
        pluck("response"),
        map((res) => loginSuccess(res)),
        tap((r) => navigate(ROUTES.DASHBOARD_SCREEN))
      )
    )
  );

navigate is navigationService.navigate and I'm passing it from the dependencies of redux-observables.

The test looks like this:

const dependencies = {
      ajax: ({ }) => of(mockedResponseFromAjax),
      navigate: () => // ??? 
    };
    const result$ = loginEpic(action$, state$, dependencies).pipe(toArray());

Aucun commentaire:

Enregistrer un commentaire