import { expect } from 'chai';
import reducer from '../../reducers/auth';
import {
  LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT, CHECK_AUTH
} from '../../actions';

describe('reducers/auth.js', () => {
  const initialState = {
    response: {},
    token: null,
    isFetching: false,
    failed: false
  };

  it('should return the initial state', () => {

    expect(reducer(undefined, {})).to.eql(initialState);

  });

  it('should handle LOGIN_REQUEST', () => {

    const action = {
      type: LOGIN_REQUEST
    };

    expect(reducer(undefined, action)).to.eql({
      ...initialState,
      isFetching: true,
      failed: false,
      response: {},
      token: null
    });

  });

  it('should handle LOGIN_SUCCESS', () => {

    const fakeState = {
      isFetching: true,
      failed: false,
      response: {},
      token: null
    };

    expect(reducer(fakeState, {}).token).to.be.null;

    const action = {
      type: LOGIN_SUCCESS,
      payload: {
        access_token: '123HELLOWORLDTOKEN'
      }
    };

    expect(reducer(fakeState, action)).to.eql({
      ...initialState,
      isFetching: false,
      token: action.payload.access_token
    });

    expect(reducer(fakeState, action).token).to.not.be.null;

  });

  it('should handle LOGIN_FAILURE', () => {

    const fakeState = {
      isFetching: true,
      failed: false,
      response: {},
      token: null
    };

    expect(reducer(fakeState, {}).token).to.be.null;

    const action = {
      type: LOGIN_FAILURE,
      payload: 'Failed to login.'
    };

    expect(reducer(fakeState, action)).to.eql({
      ...initialState,
      isFetching: false,
      failed: true,
      response: action.payload
    });

    expect(reducer(fakeState, action).token).to.be.null;

  });

  it('should handle LOGOUT', () => {

    const fakeState = {
      isFetching: false,
      failed: false,
      response: {},
      token: 'LOGGEDINTOKEN'
    };

    expect(reducer(fakeState, {}).token).to.not.be.null;

    const action = {
      type: LOGOUT
    };

    expect(reducer(fakeState, action)).to.eql({
      ...initialState,
      token: null,
      response: null
    });

    expect(reducer(fakeState, action).token).to.be.null;

  });
});

@@ -2218,22 +2218,527 @@
 o.be.null;%0A%0A
+  %7D);%0A%0A  it('should handle CHECK_AUTH', () =%3E %7B%0A%0A    const fakeState = %7B%0A      isFetching: false,%0A      failed: false,%0A      response: %7B%7D,%0A      token: null%0A    %7D;%0A%0A    const fakeToken = '123HELLOWORLDTOKEN';%0A%0A    expect(reducer(fakeState, %7B%7D).token).to.be.null;%0A%0A    const action = %7B%0A      type: CHECK_AUTH,%0A      token: fakeToken%0A    %7D;%0A%0A    expect(reducer(fakeState, action)).to.eql(%7B%0A      ...initialState,%0A      token: fakeToken%0A    %7D);%0A%0A    expect(reducer(fakeState, action).token).to.not.be.null;%0A%0A
   %7D);%0A%7D);%0A

Complete Reducers Tests