Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
fix(#91): wrote test cases for auth redux tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Gasser committed Mar 28, 2019
1 parent 5776185 commit 43cb7a1
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/redux/auth/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe('auth: reducer test suite', () => {
.toEqual(expectedState);
});

it('should handle AUTH_LOG_IN', () => {
it('should handle AUTH_LOG_OUT', () => {
const remember = true;

const newInitialState = {
Expand All @@ -478,4 +478,64 @@ describe('auth: reducer test suite', () => {
expect(reducer(newInitialState, __testables__.authLogOut('logout message')))
.toEqual(expectedState);
});

it('should handle AUTH_SET_USER_ID', () => {
const userId = mockedData.id;

const expectedState = {
...initialState,
userId,
};

expect(reducer(initialState, __testables__.authSetUserId(userId)))
.toEqual(expectedState);
});

it('should handle AUTH_SET_TOKEN', () => {
const { token } = mockedData;

const expectedState = {
...initialState,
token,
};

expect(reducer(initialState, __testables__.authSetToken(token)))
.toEqual(expectedState);
});

it('should handle AUTH_SET_VALID_EMAIL', () => {
expect(reducer(initialState, __testables__.authSetValidEmail()))
.toEqual({
...initialState,
validEmail: true,
});
});

it('should handle AUTH_SET_INVALID_EMAIL', () => {
expect(reducer(initialState, __testables__.authSetInvalidEmail()))
.toEqual({
...initialState,
validEmail: false,
});
});

it('should handle AUTH_SET_INVALID_EMAIL', () => {
const newInitialState = {
...initialState,
validEmail: true,
}

expect(reducer(newInitialState, __testables__.authResetValidEmail()))
.toEqual(initialState);
});

it('should create HOC request initial state', () => {
expect(reducer(initialState, { type: reduxApplication.APP_IDLE }))
.toEqual({
...initialState,
loginRequest: testUtils.createHocReducerState(),
signupRequest: testUtils.createHocReducerState(),
checkEmailRequest: testUtils.createHocReducerState(),
});
});
});
108 changes: 108 additions & 0 deletions src/redux/auth/__tests__/selectors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* global testUtils */
import * as selectors from '../selectors';

const initialState = {
auth: {
userId: null,
username: '',
token: null,
meta: {},
validEmail: null,
loginRequest: testUtils.createHocReducerState(),
signupRequest: testUtils.createHocReducerState(),
checkEmailRequest: testUtils.createHocReducerState(),
},
};

const state = {
auth: {
...initialState.auth,
userId: '730dd215-a5f6-4b32-bb81-cf1e8ec89099',
username: 'Test user',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI3MzBkZDIxNS1hNWY2LTRiMzItYmI4MS1jZjFlOGVjODkwMzkiLCJjcmVhdGVkQXQiOjE1NTMyMDEwMjM5NzYsImlhdCI6MTU1MzIwMTAyM30.CI1ZSQodWkuGMAQJQRZ5F7bGFKJHTWj-ql0f_INVALID',
meta: {
loggedIn: true,
loggedInSince: Date.now(),
remember: true,
},
validEmail: true,
},
};

describe('auth selector test suite', () => {
it('isAuthenticatedSelector value', () => {
// value
expect(selectors.isAuthenticatedSelector(state))
.toEqual(state.auth.meta.loggedIn);

// default value
expect(selectors.isAuthenticatedSelector(initialState))
.toEqual(false);
});

it('authUserIdSelector value', () => {
// value
expect(selectors.authUserIdSelector(state))
.toEqual(state.auth.userId);

// default value
expect(selectors.authUserIdSelector(initialState))
.toEqual(null);
});

it('tokenSelector value', () => {
// value
expect(selectors.tokenSelector(state))
.toEqual(state.auth.token);

// default value
expect(selectors.tokenSelector(initialState))
.toEqual(null);
});

it('authMetaSelector value', () => {
// value
expect(selectors.authMetaSelector(state))
.toEqual(state.auth.meta);
});

it('authRememberSelector value', () => {
// value
expect(selectors.authRememberSelector(state))
.toEqual(state.auth.meta.remember);

// default value
expect(selectors.authRememberSelector(initialState))
.toEqual(false);
});

it('authUsernameSelector value', () => {
// value
expect(selectors.authUsernameSelector(state))
.toEqual(state.auth.username);
});

it('isValidEmailSelector value', () => {
// value
expect(selectors.isValidEmailSelector(state))
.toEqual(state.auth.validEmail);
});

it('loginRequestSelector value', () => {
// value
expect(selectors.loginRequestSelector(state))
.toEqual(state.auth.loginRequest);
});

it('signUpRequestSelector value', () => {
// value
expect(selectors.signUpRequestSelector(state))
.toEqual(state.auth.signupRequest);
});

it('checkEmailRequestSelector value', () => {
// value
expect(selectors.checkEmailRequestSelector(state))
.toEqual(state.auth.checkEmailRequest);
});
});
4 changes: 4 additions & 0 deletions src/redux/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,18 @@ export const logOutUser = (message, broadcast = true) => (dispatch) => {
window.localStorage.clear();
window.sessionStorage.clear();

/* istanbul ignore next */
if (broadcast) {
window.localStorage.logout = true;
}
}
catch (error) {
/* istanbul ignore next */
console.log('could not clear local and session storage');
}

// force reload to clear cache
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'development') {
window.location = '/';
}
Expand Down Expand Up @@ -292,6 +295,7 @@ export const refreshToken = (token, userId) => (dispatch, getState, { GraphApi }
dispatch(handleAuth(token, user, remember));
})
.catch((error) => {
/* istanbul ignore next */
dispatch(logOutUser('Could not refresh token', true));
})
};
Expand Down

0 comments on commit 43cb7a1

Please sign in to comment.