Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import configureMockStore from 'redux-mock-store';
- import thunk from 'redux-thunk';
- import fetchMock from 'fetch-mock';
- import keyMirror from 'keymirror';
- import configureStore from 'redux-mock-store';
- import * as actions from '../actions/loginActions';
- import {
- LOGIN_FAILURE,
- LOGIN_SUCCESS,
- LOGIN_REQUEST,
- } from '../actions/types';
- const responsedata_success = {
- user: {
- email: 'testuser@email.com',
- username: 'testuser',
- token: 'user-token',
- },
- };
- const responsedata_failure = {
- errors: {
- error: ['A user with this email and password was not found.'],
- },
- };
- const types = keyMirror({
- LOGIN_FAILURE: null,
- LOGIN_SUCCESS: null,
- });
- describe('sync login actions', () => {
- it('has a LOGIN_SUCCESS action', () => {
- const expectedAction = {
- type: LOGIN_SUCCESS,
- payload: responsedata_success,
- };
- expect(actions.loginSuccess(responsedata_success)).toEqual(expectedAction);
- });
- it('has a LOGIN_FAILURE action', () => {
- const expectedAction = {
- type: LOGIN_FAILURE,
- payload: responsedata_failure,
- };
- expect(actions.loginFailure(responsedata_failure)).toEqual(expectedAction);
- });
- });
- describe('async login actions', () => {
- const mockStore = configureStore([thunk]);
- let store;
- beforeEach(() => {
- store = mockStore({
- message: '',
- user: {},
- status: 'none',
- });
- });
- afterEach(() => {
- fetchMock.reset();
- fetchMock.restore();
- });
- it('creates LOGIN_SUCCESSFUL when login is successful', () => {
- const userdata = {
- user: {
- email: 'testuser@email.com',
- password: 'Testuser1',
- },
- };
- fetchMock.getOnce('/api/u sers/login/', {
- method: 'POST',
- headers: {
- 'Content-type': 'application/json',
- },
- body: JSON.stringify(userdata),
- });
- const expectedActions = [
- {
- type: LOGIN_REQUEST,
- },
- {
- type: LOGIN_SUCCESS,
- payload: responsedata_success,
- }];
- const store = mockStore;
- return store.dispatch(actions.loginAction(userdata)).then(() => {
- expect(store.getActions()).toEqual(expectedActions);
- });
- });
- });
Add Comment
Please, Sign In to add comment