Skip to content

Commit

Permalink
fix: add tests for registration reducer and service
Browse files Browse the repository at this point in the history
  • Loading branch information
attiyaIshaque committed Aug 2, 2024
1 parent fccfc91 commit 2c926f8
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 29 deletions.
64 changes: 58 additions & 6 deletions src/forms/registration-popup/data/tests/reducer.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import {
COMPLETE_STATE, DEFAULT_STATE, PENDING_STATE,
COMPLETE_STATE,
DEFAULT_STATE,
PENDING_STATE,
} from '../../../../data/constants';
import registerReducer, {
backupRegistrationForm,
clearAllRegistrationErrors,
clearRegistrationBackendError,
fetchRealtimeValidations,
fetchRealtimeValidationsFailed,
fetchRealtimeValidationsSuccess,
registerInitialState,
registerUser,
registerUserFailed,
registerUserSuccess,
setRegistrationFields,
} from '../reducers';

describe('registerSlice reducer', () => {
Expand All @@ -19,7 +26,7 @@ describe('registerSlice reducer', () => {
const nextState = registerReducer(registerInitialState, fetchRealtimeValidations());

expect(nextState.validationState).toEqual(PENDING_STATE);
expect(nextState.validations).toEqual(null);
expect(nextState.validations).toBeNull();
});

it('should handle fetchRealtimeValidationsSuccess action', () => {
Expand All @@ -43,7 +50,7 @@ describe('registerSlice reducer', () => {
});

it('should handle clearRegistrationBackendError action', () => {
const nextState = registerReducer({
const initialStateWithErrors = {
...registerInitialState,
registrationError: {
email: [
Expand All @@ -53,14 +60,15 @@ describe('registerSlice reducer', () => {
],
errorCode: 'duplicate-email',
},
};

}, clearRegistrationBackendError('email'));
const nextState = registerReducer(initialStateWithErrors, clearRegistrationBackendError('email'));

expect(nextState.registrationError).toEqual({ errorCode: 'duplicate-email' });
});

it('should handle clearAllRegistrationErrors action', () => {
const nextState = registerReducer({
const initialStateWithErrors = {
...registerInitialState,
registrationError: {
email: [
Expand All @@ -70,9 +78,53 @@ describe('registerSlice reducer', () => {
],
errorCode: 'duplicate-email',
},
};

const nextState = registerReducer(initialStateWithErrors, clearAllRegistrationErrors());

expect(nextState.registrationError).toEqual({});
});

}, clearAllRegistrationErrors());
it('should handle registerUser action', () => {
const nextState = registerReducer(registerInitialState, registerUser());

expect(nextState.submitState).toEqual(PENDING_STATE);
expect(nextState.registrationError).toEqual({});
});

it('should handle registerUserSuccess action', () => {
const mockPayload = { user: 'testUser' };
const nextState = registerReducer(registerInitialState, registerUserSuccess(mockPayload));

expect(nextState.submitState).toEqual(COMPLETE_STATE);
expect(nextState.registrationResult).toEqual(mockPayload);
});

it('should handle registerUserFailed action', () => {
const mockPayload = { error: 'Some error occurred' };
const nextState = registerReducer(registerInitialState, registerUserFailed(mockPayload));

expect(nextState.submitState).toEqual(DEFAULT_STATE);
expect(nextState.registrationError).toEqual(mockPayload);
expect(nextState.registrationResult).toEqual({});
expect(nextState.validations).toBeNull();
});

it('should handle setRegistrationFields action', () => {
const mockPayload = { marketingEmailsOptIn: false };
const nextState = registerReducer(registerInitialState, setRegistrationFields(mockPayload));

expect(nextState.registrationFields).toEqual(mockPayload);
});

it('should handle backupRegistrationForm action', () => {
const mockPayload = {
isFormFilled: true,
formFields: { name: 'John Doe', email: '[email protected]', password: 'password123' },
errors: { name: '', email: '', password: '' },
};
const nextState = registerReducer(registerInitialState, backupRegistrationForm(mockPayload));

expect(nextState.registrationFormData).toEqual(mockPayload);
});
});
81 changes: 58 additions & 23 deletions src/forms/registration-popup/data/tests/service.test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,84 @@
import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

import { getFieldsValidations } from '../service';
import registerRequest, { getFieldsValidations } from '../service';

jest.mock('@edx/frontend-platform/auth');
jest.mock('@edx/frontend-platform', () => ({
getConfig: jest.fn(() => ({ LMS_BASE_URL: 'http://example.com' })),
}));

describe('getFieldsValidations Tests', () => {
describe('Service Function Tests', () => {
const mockGetAuthenticatedHttpClient = jest.fn();
getAuthenticatedHttpClient.mockReturnValue({ post: mockGetAuthenticatedHttpClient });

afterEach(() => {
jest.clearAllMocks();
});

it('should return backend validation message and success status if request succeeds', async () => {
const mockCreds = {
email: '[email protected]',
};
const mockData = {
validation_decisions: {
email: 'This email is already associated with an existing or previous edX account',
},
};
describe('getFieldsValidations Tests', () => {
it('should return backend validation message if request succeeds', async () => {
const mockCreds = { email: '[email protected]' };
const mockData = {
validation_decisions: {
email: 'This email is already associated with an existing or previous edX account',
},
};

mockGetAuthenticatedHttpClient.mockResolvedValue({ data: mockData });
mockGetAuthenticatedHttpClient.mockResolvedValue({ data: mockData });

const result = await getFieldsValidations(mockCreds);
const result = await getFieldsValidations(mockCreds);

expect(result).toEqual({ fieldValidations: mockData });
expect(result).toEqual({ fieldValidations: mockData });
});

it('should throw an error if the request fails', async () => {
const mockCreds = { email: '[email protected]' };
const errorMessage = 'Validation request failed';

mockGetAuthenticatedHttpClient.mockRejectedValue(new Error(errorMessage));

await expect(getFieldsValidations(mockCreds)).rejects.toThrow(errorMessage);
expect(getConfig).toHaveBeenCalled();
expect(mockGetAuthenticatedHttpClient).toHaveBeenCalled();
});
});

it('should throw an error if the request fails', async () => {
const mockCreds = {
email: '[email protected]',
};
const errorMessage = 'Login request failed';
describe('registerRequest Tests', () => {
it('should return redirect URL, success status, and authenticated user details if request succeeds', async () => {
const mockRegistrationInfo = {
email: '[email protected]',
password: 'password123',
};
const mockData = {
redirect_url: 'http://example.com/dashboard',
success: true,
authenticated_user: {
username: 'testuser',
},
};

mockGetAuthenticatedHttpClient.mockResolvedValue({ data: mockData });

const result = await registerRequest(mockRegistrationInfo);

expect(result).toEqual({
redirectUrl: mockData.redirect_url,
success: mockData.success,
authenticatedUser: mockData.authenticated_user,
});
});

mockGetAuthenticatedHttpClient.mockRejectedValue(new Error(errorMessage));
it('should throw an error if the request fails', async () => {
const mockRegistrationInfo = {
email: '[email protected]',
password: 'password123',
};
const errorMessage = 'Registration request failed';

await expect(getFieldsValidations(mockCreds)).rejects.toThrow(errorMessage);
mockGetAuthenticatedHttpClient.mockRejectedValue(new Error(errorMessage));

expect(getConfig).toHaveBeenCalled();
expect(mockGetAuthenticatedHttpClient).toHaveBeenCalled();
await expect(registerRequest(mockRegistrationInfo)).rejects.toThrow(errorMessage);
});
});
});

0 comments on commit 2c926f8

Please sign in to comment.