lundi 19 décembre 2016

Testing a Redux login Action

I'm hoping that I could ask for some help on how to test a Redux Action that involves a login API call. I've looked at some examples of testing an async Action, but I haven't wrapped my head around how to test the code below.

As a starting point, I would like to test that a)AUTH_USER is called if the .post request returns a 200 and b)localStorage` contains the token from the API call.

I've looked at using redux-mock-store, fetch-mock and isomorphic-fetch in order to mock the API calls to make sure I always receive the expected API response, but I have no idea where to start with the test.

Any help would be highly appreciated on a starting point for the tests! Even some help on just testing that 200 will return AUTH_USER would be appreciated!

Note: Elsewhere for other tests I'm using, redux-mock-store, enzyme, chai, expect, fetch-mock, isomorphic-fetch

import axios from 'axios';
import { browserHistory } from 'react-router';
import { API_URL } from 'config';
import {
  AUTH_USER
} from './types';

export function loginUser({ email, password }) {
  return function (dispatch) {
    axios.post(`${API_URL}/auth/login`, { email, password })
      .then((response) => {
        dispatch({ type: AUTH_USER });
        localStorage.setItem('token', response.data.token);
        browserHistory.push('/feature');
      })
      .catch(() => {
        dispatch(authError('Bad Login Info'));
      });
  };
}

Aucun commentaire:

Enregistrer un commentaire