Skip to content

Commit cb0d534

Browse files
committed
Add START_FORCE_LOGOUT action to authReducer and corresponding test case
1 parent 615133a commit cb0d534

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/reducers/__tests__/authReducer.test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { isEmpty } from 'lodash';
22
import { authReducer } from '../authReducer';
3-
import { SET_CURRENT_USER, SET_HEADER_DATA } from '../../constants/auth';
3+
import { SET_CURRENT_USER, SET_HEADER_DATA, START_FORCE_LOGOUT } from '../../constants/auth';
44

55
describe('authReducer', () => {
66
const initialState = {
77
isAuthenticated: false,
88
user: {},
99
firstName: '',
1010
profilePic: '',
11+
forceLogoutAt: null,
12+
timerId: null,
1113
};
1214

1315
it('should return the initial state when action type is unknown', () => {
1416
const action = { type: 'UNKNOWN_ACTION' };
1517
expect(authReducer(undefined, action)).toEqual(initialState);
1618
});
17-
1819
it('should handle SET_CURRENT_USER with null payload (logout scenario)', () => {
1920
const action = { type: SET_CURRENT_USER, payload: null };
2021
expect(authReducer(initialState, action)).toEqual(initialState);
2122
});
22-
2323
it('should handle SET_CURRENT_USER with a new user', () => {
2424
const newUser = { new: true, id: '1', name: 'New User' };
2525
const action = { type: SET_CURRENT_USER, payload: newUser };
@@ -30,7 +30,6 @@ describe('authReducer', () => {
3030
};
3131
expect(authReducer(initialState, action)).toEqual(expectedState);
3232
});
33-
3433
it('should handle SET_CURRENT_USER with a valid user (login scenario)', () => {
3534
const userPayload = { id: '123', name: 'John Doe' };
3635
const action = { type: SET_CURRENT_USER, payload: userPayload };
@@ -41,7 +40,6 @@ describe('authReducer', () => {
4140
};
4241
expect(authReducer(initialState, action)).toEqual(expectedState);
4342
});
44-
4543
it('should handle SET_HEADER_DATA', () => {
4644
const headerData = {
4745
firstName: 'John',
@@ -55,4 +53,21 @@ describe('authReducer', () => {
5553
};
5654
expect(authReducer(initialState, action)).toEqual(expectedState);
5755
});
56+
it('should handle START_FORCE_LOGOUT', () => {
57+
const logoutTime = Date.now() + 60000; // 1 minute from now
58+
const timerId = 123;
59+
const action = {
60+
type: START_FORCE_LOGOUT,
61+
payload: {
62+
forceLogoutAt: logoutTime,
63+
timerId,
64+
},
65+
};
66+
const expectedState = {
67+
...initialState,
68+
forceLogoutAt: logoutTime,
69+
timerId,
70+
};
71+
expect(authReducer(initialState, action)).toEqual(expectedState);
72+
});
5873
});

src/reducers/authReducer.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { isEmpty } from 'lodash';
2-
import { SET_CURRENT_USER, SET_HEADER_DATA } from '../constants/auth';
2+
import { SET_CURRENT_USER, SET_HEADER_DATA, START_FORCE_LOGOUT } from '../constants/auth';
33

44
const initialState = {
55
isAuthenticated: false,
66
user: {},
77
firstName: '',
88
profilePic: '',
9+
forceLogoutAt: null,
10+
timerId: null,
911
};
1012

1113
// eslint-disable-next-line default-param-last
1214
export const authReducer = (auth = initialState, action) => {
1315
if (action.type === SET_CURRENT_USER) {
1416
if (!action.payload) {
15-
return initialState;
17+
return {
18+
...initialState,
19+
forceLogoutAt: null, // Ensure logout clears these values
20+
timerId: null,
21+
};
1622
}
1723
if (action.payload.new) {
1824
return {
@@ -34,6 +40,13 @@ export const authReducer = (auth = initialState, action) => {
3440
profilePic: action.payload.profilePic,
3541
};
3642
}
43+
if (action.type === START_FORCE_LOGOUT) {
44+
return {
45+
...auth,
46+
forceLogoutAt: action.payload.forceLogoutAt,
47+
timerId: action.payload.timerId,
48+
};
49+
}
3750

3851
return auth;
3952
};

0 commit comments

Comments
 (0)