diff --git a/src/forms/registration-popup/data/tests/reducer.test.js b/src/forms/registration-popup/data/tests/reducer.test.js index 552bbeac..815caa4e 100644 --- a/src/forms/registration-popup/data/tests/reducer.test.js +++ b/src/forms/registration-popup/data/tests/reducer.test.js @@ -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', () => { @@ -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', () => { @@ -43,7 +50,7 @@ describe('registerSlice reducer', () => { }); it('should handle clearRegistrationBackendError action', () => { - const nextState = registerReducer({ + const initialStateWithErrors = { ...registerInitialState, registrationError: { email: [ @@ -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: [ @@ -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: 'john@example.com', password: 'password123' }, + errors: { name: '', email: '', password: '' }, + }; + const nextState = registerReducer(registerInitialState, backupRegistrationForm(mockPayload)); + + expect(nextState.registrationFormData).toEqual(mockPayload); + }); }); diff --git a/src/forms/registration-popup/data/tests/service.test.js b/src/forms/registration-popup/data/tests/service.test.js index b63a1b9b..651ab916 100644 --- a/src/forms/registration-popup/data/tests/service.test.js +++ b/src/forms/registration-popup/data/tests/service.test.js @@ -1,14 +1,14 @@ 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 }); @@ -16,34 +16,69 @@ describe('getFieldsValidations Tests', () => { jest.clearAllMocks(); }); - it('should return backend validation message and success status if request succeeds', async () => { - const mockCreds = { - email: 'test@example.com', - }; - 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: 'test@example.com' }; + 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: 'test@example.com' }; + 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: 'test@example.com', - }; - 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: 'test@example.com', + 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: 'test@example.com', + 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); + }); }); });