Skip to content

Commit

Permalink
chore: add tests for the missing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
attiyaIshaque committed Jul 15, 2024
1 parent a14d2e8 commit bd067fb
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const forgotPasswordSlice = createSlice({
forgotPasswordClearStatus: (state) => {
state.status = DEFAULT_STATE;
},
forgotPassweordTokenInvalidFailure: (state, { payload }) => {
forgotPasswordTokenInvalidFailure: (state, { payload }) => {
state.status = payload;
},
setForgotPasswordFormData: (state, { payload }) => {
Expand All @@ -59,7 +59,7 @@ export const {
forgotPasswordForbidden,
forgotPasswordFailed,
forgotPasswordClearStatus,
forgotPassweordTokenInvalidFailure,
forgotPasswordTokenInvalidFailure,
setForgotPasswordFormData,
} = forgotPasswordSlice.actions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import forgotPasswordReducer, {
forgotPasswordForbidden,
forgotPasswordInitialState,
forgotPasswordSuccess,
forgotPasswordTokenInvalidFailure,
setForgotPasswordFormData,
} from '../reducers';

describe('forgotPasswordSlice reducer', () => {
Expand Down Expand Up @@ -43,4 +45,16 @@ describe('forgotPasswordSlice reducer', () => {
const nextState = forgotPasswordReducer(forgotPasswordInitialState, forgotPasswordClearStatus(PENDING_STATE));
expect(nextState.status).toEqual(DEFAULT_STATE);
});

it('should handle forgotPasswordTokenInvalidFailure action', () => {
const payload = 'INVALID_TOKEN';
const nextState = forgotPasswordReducer(forgotPasswordInitialState, forgotPasswordTokenInvalidFailure(payload));
expect(nextState.status).toEqual(payload);
});

it('should handle setForgotPasswordFormData action', () => {
const payload = { email: 'test@example.com', error: 'Some error' };
const nextState = forgotPasswordReducer(forgotPasswordInitialState, setForgotPasswordFormData(payload));
expect(nextState.forgotPasswordFormData).toEqual(payload);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import messages from '../../../messages';
import getValidationMessage from '../utils';

describe('getValidationMessage', () => {
const mockFormatMessage = jest.fn((msg) => msg.defaultMessage);

it('should return an empty string for a valid email', () => {
const email = 'valid@example.com';
const validationMessage = getValidationMessage(email, mockFormatMessage);
expect(validationMessage).toBe('');
});

it('should return an error message for an empty email', () => {
const email = '';
const validationMessage = getValidationMessage(email, mockFormatMessage);
expect(validationMessage).toBe(messages.forgotPasswordEmptyEmailFieldError.defaultMessage);
});

it('should return an error message for an undefined email', () => {
const email = undefined;
const validationMessage = getValidationMessage(email, mockFormatMessage);
expect(validationMessage).toBe(messages.forgotPasswordEmptyEmailFieldError.defaultMessage);
});

it('should return an error message for an invalid email', () => {
const email = 'invalid-email';
const validationMessage = getValidationMessage(email, mockFormatMessage);
expect(validationMessage).toBe(messages.forgotPasswordPageInvalidEmaiMessage.defaultMessage);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { render } from '@testing-library/react';

import { FORBIDDEN_STATE, INTERNAL_SERVER_ERROR } from '../../../../data/constants';
import messages from '../../messages';
import { PASSWORD_RESET } from '../../reset-password/data/constants';
import ForgotPasswordFailureAlert from '../ForgotPasswordFailureAlert';

const IntlForgotPasswordFailureAlert = injectIntl(ForgotPasswordFailureAlert);
Expand Down Expand Up @@ -93,4 +94,21 @@ describe('ForgotPasswordFailureAlert', () => {

expect(container.querySelector('#forgot-password-failure-alert').textContent).toBe(expectedMessage);
});

it('should match invalid token error message', () => {
props = {
status: PASSWORD_RESET.INVALID_TOKEN,
emailError: '',
};

const { container } = render(
<IntlProvider locale="en">
<IntlForgotPasswordFailureAlert {...props} />
</IntlProvider>,
);

const expectedMessage = messages['invalid.token.error.message'].defaultMessage;

expect(container.querySelector('#forgot-password-failure-alert').textContent).toBe(expectedMessage);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,36 @@ describe('ForgotPasswordPage', () => {
]));
});

// TODO: skipping because failing due to useRef. will be fixed later
it.skip('handles COMPLETE_STATE correctly in useEffect', () => {
// Update initial state to COMPLETE_STATE
it('sets form errors to an empty string and sets success state when status is COMPLETE_STATE', () => {
store = mockStore({
...initialState,
forgotPassword: {
status: DEFAULT_STATE,
forgotPasswordFormData: {
email: '',
error: 'Some error',
},
},
});

const { rerender } = render(reduxWrapper(<IntlForgotPasswordPage />));

// Update the state to COMPLETE_STATE and rerender
store = mockStore({
...initialState,
forgotPassword: {
status: COMPLETE_STATE,
forgotPasswordFormData: {
email: 'test@example.com',
error: '',
},
},
});

render(reduxWrapper(<IntlForgotPasswordPage />));
rerender(reduxWrapper(<IntlForgotPasswordPage />));

const emailInput = screen.queryByLabelText('Email');
expect(emailInput).toBeNull();
// Check if isSuccess is true
expect(screen.getByText('Email has been sent')).toBeTruthy();
});

it('dispatches forgotPassword action with valid email on form submission', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ResetPasswordSuccess = () => {

return (
<Alert id="reset-password-success" variant="success" className="mb-5">
<p>{formatMessage(messages.resetPassowrdSuccess)}</p>
<p>{formatMessage(messages.resetPasswordSuccess)}</p>
</Alert>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const REGISTER_SLICE_NAME = 'resetPassword';

export const resetPasswordInitialState = {
tokenValidationState: DEFAULT_STATE,
resetPasswordsubmitState: DEFAULT_STATE,
resetPasswordSubmitState: DEFAULT_STATE,
status: TOKEN_STATE.PENDING,
token: null,
errorMsg: null,
Expand Down Expand Up @@ -44,15 +44,15 @@ export const resetPasswordSlice = createSlice({
},
resetPassword: (state) => {
state.status = PENDING_STATE;
state.resetPasswordsubmitState = PENDING_STATE;
state.resetPasswordSubmitState = PENDING_STATE;
},
resetPasswordSuccess: (state) => {
state.status = SUCCESS;
state.resetPasswordsubmitState = COMPLETE_STATE;
state.resetPasswordSubmitState = COMPLETE_STATE;
},
resetPasswordFailure: (state, { payload }) => {
state.status = payload.status;
state.resetPasswordsubmitState = FAILURE_STATE;
state.resetPasswordSubmitState = FAILURE_STATE;
state.errorMsg = payload.errorMsg;
},
validatePassword: (state) => {
Expand Down
10 changes: 5 additions & 5 deletions src/forms/reset-password-popup/reset-password/data/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from './reducers';
import { resetPasswordRequest, validatePasswordRequest, validateTokenRequest } from './service';
import { setShowPasswordResetBanner } from '../../../login-popup/data/reducers';
import { forgotPassweordTokenInvalidFailure } from '../../forgot-password/data/reducers';
import { forgotPasswordTokenInvalidFailure } from '../../forgot-password/data/reducers';

// Services
export function* handleValidateToken(action) {
Expand All @@ -27,16 +27,16 @@ export function* handleValidateToken(action) {
yield put(validateTokenSuccess(isValid, action.payload));
} else {
yield put(validateTokenFailed(PASSWORD_RESET.INVALID_TOKEN));
yield put(forgotPassweordTokenInvalidFailure(PASSWORD_RESET.INVALID_TOKEN));
yield put(forgotPasswordTokenInvalidFailure(PASSWORD_RESET.INVALID_TOKEN));
}
} catch (err) {
if (err.response && err.response.status === 429) {
yield put(validateTokenFailed(PASSWORD_RESET.FORBIDDEN_REQUEST));
yield put(forgotPassweordTokenInvalidFailure(PASSWORD_RESET.FORBIDDEN_REQUEST));
yield put(forgotPasswordTokenInvalidFailure(PASSWORD_RESET.FORBIDDEN_REQUEST));
logInfo(err);
} else {
yield put(validateTokenFailed(PASSWORD_RESET.INTERNAL_SERVER_ERROR));
yield put(forgotPassweordTokenInvalidFailure(PASSWORD_RESET.INTERNAL_SERVER_ERROR));
yield put(forgotPasswordTokenInvalidFailure(PASSWORD_RESET.INTERNAL_SERVER_ERROR));
logError(err);
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ export function* handleResetPassword(action) {
yield put(resetPasswordFailure({
status: PASSWORD_RESET.INVALID_TOKEN,
}));
yield put(forgotPassweordTokenInvalidFailure(PASSWORD_RESET.INVALID_TOKEN));
yield put(forgotPasswordTokenInvalidFailure(PASSWORD_RESET.INVALID_TOKEN));
} else {
yield put(resetPasswordFailure({
status: PASSWORD_VALIDATION_ERROR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {
FAILURE_STATE,
PENDING_STATE,
} from '../../../../../data/constants';
import { PASSWORD_RESET_ERROR, SUCCESS, TOKEN_STATE } from '../constants';
import resetPasswordReducer, {
resetPassword,
resetPasswordFailure,
resetPasswordInitialState,
resetPasswordSuccess,
validatePassword,
validatePasswordFailure,
validatePasswordSuccess,
validateToken,
validateTokenFailed,
validateTokenSuccess,
Expand Down Expand Up @@ -40,4 +45,28 @@ describe('resetPasswordSlice reducer', () => {
const nextState = resetPasswordReducer(resetPasswordInitialState, resetPasswordSuccess());
expect(nextState.status).toEqual(SUCCESS);
});

it('should handle resetPasswordFailure action', () => {
const payload = { status: FAILURE_STATE, errorMsg: 'Error message' };
const nextState = resetPasswordReducer(resetPasswordInitialState, resetPasswordFailure(payload));
expect(nextState.status).toEqual(FAILURE_STATE);
expect(nextState.resetPasswordSubmitState).toEqual(FAILURE_STATE);
expect(nextState.errorMsg).toEqual('Error message');
});

it('should handle validatePassword action', () => {
const nextState = resetPasswordReducer(resetPasswordInitialState, validatePassword());
expect(nextState.backendValidationError).toEqual(null);
});

it('should handle validatePasswordSuccess action', () => {
const payload = 'Password validation success';
const nextState = resetPasswordReducer(resetPasswordInitialState, validatePasswordSuccess(payload));
expect(nextState.backendValidationError).toEqual(payload);
});

it('should handle validatePasswordFailure action', () => {
const nextState = resetPasswordReducer(resetPasswordInitialState, validatePasswordFailure());
expect(nextState.backendValidationError).toEqual(null);
});
});
Loading

0 comments on commit bd067fb

Please sign in to comment.