From 2806c43553dbaa02220db73bcbeaa0297d628161 Mon Sep 17 00:00:00 2001 From: Attiya Ishaque Date: Fri, 2 Aug 2024 14:59:32 +0500 Subject: [PATCH] fix: add unit tests to improve coverage --- .../SocialAuthButtons/index.test.jsx | 27 +++++++ src/data/tests/cookies.test.js | 14 +++- src/data/tests/utils.test.js | 24 +++++- .../tests/AuthenticatedRedirection.test.jsx | 20 +++++ .../enterprise-sso-popup/data/utils.test.js | 69 ++++++++++++++++ src/forms/enterprise-sso-popup/index.test.jsx | 11 +++ .../AccountActivationMessage.test.jsx | 16 +++- .../login-popup/data/tests/reducer.test.js | 56 ++++++++++++- .../data/tests/reducer.test.js | 64 +++++++++++++-- .../data/tests/service.test.js | 81 +++++++++++++------ .../data/tests/utils.test.js | 35 +++++++- src/onboarding-component/tests/index.test.jsx | 78 +++++++++++++++--- 12 files changed, 445 insertions(+), 50 deletions(-) create mode 100644 src/forms/enterprise-sso-popup/data/utils.test.js diff --git a/src/common-ui/SocialAuthButtons/index.test.jsx b/src/common-ui/SocialAuthButtons/index.test.jsx index 1b041c7d..bbddbc2f 100644 --- a/src/common-ui/SocialAuthButtons/index.test.jsx +++ b/src/common-ui/SocialAuthButtons/index.test.jsx @@ -6,6 +6,7 @@ import { fireEvent, render } from '@testing-library/react'; import configureStore from 'redux-mock-store'; import { COMPLETE_STATE, PENDING_STATE } from '../../data/constants'; +import { setCookie } from '../../data/cookies'; import { OnboardingComponentContext } from '../../data/storeHooks'; import SocialAuthProviders, { SocialAuthButton } from './index'; @@ -18,6 +19,9 @@ jest.mock('@edx/frontend-platform', () => ({ })); const mockStore = configureStore(); +jest.mock('../../data/cookies', () => ({ + setCookie: jest.fn(), +})); describe('SocialAuthButton', () => { let store = {}; @@ -84,6 +88,29 @@ describe('SocialAuthButton', () => { expect(window.location.href).toEqual('http://example.com/login/google'); }); + + it('sets marketingEmailsOptIn cookie when isLoginForm is false', () => { + store = mockStore({ + register: { + registrationFields: { + marketingEmailsOptIn: true, + }, + }, + }); + + const { getByText } = render(reduxWrapper( + , + )); + + delete window.location; + window.location = { href: 'http://base-url.com' }; + + const button = getByText('Sign up with Google'); + fireEvent.click(button); + + expect(setCookie).toHaveBeenCalledWith('marketingEmailsOptIn', true); + expect(window.location.href).toEqual('http://example.com/register/google'); + }); }); describe('SocialAuthProviders', () => { diff --git a/src/data/tests/cookies.test.js b/src/data/tests/cookies.test.js index bd95edf1..b9a30e29 100644 --- a/src/data/tests/cookies.test.js +++ b/src/data/tests/cookies.test.js @@ -1,7 +1,7 @@ import { getConfig } from '@edx/frontend-platform'; import Cookies from 'universal-cookie'; -import { setCookie } from '../cookies'; +import { getCookie, setCookie } from '../cookies'; // Mock getConfig function jest.mock('@edx/frontend-platform', () => ({ @@ -50,3 +50,15 @@ describe('setCookie function', () => { expect(Cookies).not.toHaveBeenCalled(); }); }); + +describe('getCookie function', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should return null if cookieName is undefined', () => { + const result = getCookie(undefined); + expect(result).toBeNull(); + expect(Cookies).not.toHaveBeenCalled(); + }); +}); diff --git a/src/data/tests/utils.test.js b/src/data/tests/utils.test.js index 5ecde9e3..a79ce331 100644 --- a/src/data/tests/utils.test.js +++ b/src/data/tests/utils.test.js @@ -2,7 +2,7 @@ import { getConfig, mergeConfig } from '@edx/frontend-platform'; import Cookies from 'universal-cookie'; import { getCountryCookieValue } from '../cookies'; -import getAllPossibleQueryParams from '../utils'; +import getAllPossibleQueryParams, { moveScrollToTop } from '../utils'; describe('getAllPossibleQueryParams', () => { beforeEach(() => { @@ -50,3 +50,25 @@ describe('getCountryCookieValue', () => { expect(countryCode).toEqual(undefined); }); }); +describe('moveScrollToTop', () => { + it('should call scrollIntoView on the provided ref', () => { + const scrollIntoViewMock = jest.fn(); + const ref = { + current: { + scrollIntoView: scrollIntoViewMock, + }, + }; + + moveScrollToTop(ref, 'end'); + + expect(scrollIntoViewMock).toHaveBeenCalledWith({ behavior: 'smooth', block: 'end' }); + }); + + it('should not throw an error if ref.current is undefined', () => { + const ref = { + current: null, + }; + + expect(() => moveScrollToTop(ref, 'end')).not.toThrow(); + }); +}); diff --git a/src/forms/common-components/tests/AuthenticatedRedirection.test.jsx b/src/forms/common-components/tests/AuthenticatedRedirection.test.jsx index 50ce2431..1792d8c1 100644 --- a/src/forms/common-components/tests/AuthenticatedRedirection.test.jsx +++ b/src/forms/common-components/tests/AuthenticatedRedirection.test.jsx @@ -7,6 +7,7 @@ import { MemoryRouter } from 'react-router-dom'; import configureStore from 'redux-mock-store'; import { PROGRESSIVE_PROFILING_FORM } from '../../../data/constants'; +import { LINK_TIMEOUT } from '../../../data/segment/utils'; import { OnboardingComponentContext } from '../../../data/storeHooks'; import { setCurrentOpenedForm } from '../../../onboarding-component/data/reducers'; import AuthenticatedRedirection from '../AuthenticatedRedirection'; @@ -48,10 +49,12 @@ describe('AuthenticatedRedirection', () => { store = mockStore(initialState); delete window.location; window.location = { href: '' }; + jest.useFakeTimers(); }); afterEach(() => { jest.clearAllMocks(); + jest.useRealTimers(); }); it('should not redirect if success is false', () => { @@ -103,4 +106,21 @@ describe('AuthenticatedRedirection', () => { expect(store.dispatch).toHaveBeenCalledWith(setCurrentOpenedForm(PROGRESSIVE_PROFILING_FORM)); }); + + it('should redirect after a delay if isLinkTracked is true', () => { + render(reduxWrapper( + , + )); + + expect(window.location.href).toBe(''); // Shouldn't redirect immediately + + jest.advanceTimersByTime(LINK_TIMEOUT); // Fast-forward time by LINK_TIMEOUT + + expect(window.location.href).toBe(mockRedirectUrl); // Should have redirected after timeout + }); }); diff --git a/src/forms/enterprise-sso-popup/data/utils.test.js b/src/forms/enterprise-sso-popup/data/utils.test.js new file mode 100644 index 00000000..659296fc --- /dev/null +++ b/src/forms/enterprise-sso-popup/data/utils.test.js @@ -0,0 +1,69 @@ +import QueryString from 'query-string'; + +import { getTpaHint, getTpaProvider } from './utils'; + +// Mocking the query-string library +jest.mock('query-string'); + +describe('Utility Functions', () => { + describe('getTpaProvider', () => { + it('should return the provider from primaryProviders', () => { + const tpaHintProvider = 'google-oauth2'; + const primaryProviders = [{ id: 'google-oauth2' }, { id: 'facebook' }]; + const secondaryProviders = [{ id: 'twitter' }]; + + const result = getTpaProvider(tpaHintProvider, primaryProviders, secondaryProviders); + expect(result.provider).toEqual({ id: 'google-oauth2' }); + }); + + it('should return the provider from secondaryProviders', () => { + const tpaHintProvider = 'twitter'; + const primaryProviders = [{ id: 'google-oauth2' }, { id: 'facebook' }]; + const secondaryProviders = [{ id: 'twitter' }]; + + const result = getTpaProvider(tpaHintProvider, primaryProviders, secondaryProviders); + expect(result.provider).toEqual({ id: 'twitter' }); + }); + + it('should return null if provider is not found', () => { + const tpaHintProvider = 'linkedin'; + const primaryProviders = [{ id: 'google-oauth2' }, { id: 'facebook' }]; + const secondaryProviders = [{ id: 'twitter' }]; + + const result = getTpaProvider(tpaHintProvider, primaryProviders, secondaryProviders); + expect(result.provider).toBeNull(); + }); + + it('should return null if tpaHintProvider is not provided', () => { + const tpaHintProvider = null; + const primaryProviders = [{ id: 'google-oauth2' }, { id: 'facebook' }]; + const secondaryProviders = [{ id: 'twitter' }]; + + const result = getTpaProvider(tpaHintProvider, primaryProviders, secondaryProviders); + expect(result.provider).toBeNull(); + }); + }); + + describe('getTpaHint', () => { + it('should return tpa_hint from the query string', () => { + QueryString.parse.mockReturnValue({ tpa_hint: 'google-oauth2' }); + + const result = getTpaHint(); + expect(result).toBe('google-oauth2'); + }); + + it('should return tpa_hint from the "next" parameter in the query string', () => { + QueryString.parse.mockReturnValue({ next: 'some-path?tpa_hint=facebook' }); + + const result = getTpaHint(); + expect(result).toBe('facebook'); + }); + + it('should return undefined if tpa_hint is not found', () => { + QueryString.parse.mockReturnValue({ next: 'some-path' }); + + const result = getTpaHint(); + expect(result).toBeUndefined(); + }); + }); +}); diff --git a/src/forms/enterprise-sso-popup/index.test.jsx b/src/forms/enterprise-sso-popup/index.test.jsx index 4f65bb59..e804a690 100644 --- a/src/forms/enterprise-sso-popup/index.test.jsx +++ b/src/forms/enterprise-sso-popup/index.test.jsx @@ -88,4 +88,15 @@ describe('EnterpriseSSO', () => { expect(store.dispatch).toHaveBeenCalledWith(setCurrentOpenedForm(LOGIN_FORM)); }); + + it('should prevent default behavior on mouse down for "Show me other ways to sign in or register" button', () => { + const { container } = render(reduxWrapper()); + + const button = container.querySelector('#other-ways-to-sign-in'); + + const preventDefaultMock = jest.fn(); + button.onmousedown = preventDefaultMock; + fireEvent.mouseDown(button); + expect(preventDefaultMock).toHaveBeenCalledTimes(1); + }); }); diff --git a/src/forms/login-popup/components/AccountActivationMessage.test.jsx b/src/forms/login-popup/components/AccountActivationMessage.test.jsx index 017a7912..088c2551 100644 --- a/src/forms/login-popup/components/AccountActivationMessage.test.jsx +++ b/src/forms/login-popup/components/AccountActivationMessage.test.jsx @@ -2,16 +2,14 @@ import React from 'react'; import { mergeConfig } from '@edx/frontend-platform'; import { injectIntl, IntlProvider } from '@edx/frontend-platform/i18n'; -import { - render, screen, -} from '@testing-library/react'; +import { render, screen } from '@testing-library/react'; import AccountActivationMessage from './AccountActivationMessage'; import { ACCOUNT_ACTIVATION_MESSAGE } from '../data/constants'; const IntlAccountActivationMessage = injectIntl(AccountActivationMessage); -describe('EmailConfirmationMessage', () => { +describe('AccountActivationMessage', () => { beforeEach(() => { mergeConfig({ MARKETING_EMAILS_OPT_IN: 'true', @@ -64,4 +62,14 @@ describe('EmailConfirmationMessage', () => { { selector: '#account-activation-message' }, ).textContent).toBe(expectedMessage); }); + + it('should render nothing for unknown messageType', () => { + render( + + + , + ); + // Expect nothing to be rendered + expect(screen.queryByText('', { selector: '#account-activation-message' })).toBeNull(); + }); }); diff --git a/src/forms/login-popup/data/tests/reducer.test.js b/src/forms/login-popup/data/tests/reducer.test.js index 12b218a4..f495b021 100644 --- a/src/forms/login-popup/data/tests/reducer.test.js +++ b/src/forms/login-popup/data/tests/reducer.test.js @@ -1,8 +1,18 @@ import { - COMPLETE_STATE, FAILURE_STATE, PENDING_STATE, + COMPLETE_STATE, + DEFAULT_STATE, + FAILURE_STATE, + PENDING_STATE, } from '../../../../data/constants'; import loginReducer, { - loginInitialState, loginUser, loginUserFailed, loginUserSuccess, + backupLoginForm, + loginErrorClear, + loginInitialState, + loginUser, + loginUserFailed, + loginUserSuccess, + setLoginSSOIntent, + setShowPasswordResetBanner, } from '../reducers'; describe('loginSlice reducer', () => { @@ -47,4 +57,46 @@ describe('loginSlice reducer', () => { }); expect(nextState.loginResult).toEqual({}); }); + + it('should handle setShowPasswordResetBanner action', () => { + const nextState = loginReducer(loginInitialState, setShowPasswordResetBanner()); + + expect(nextState.showResetPasswordSuccessBanner).toEqual(true); + }); + + it('should handle loginErrorClear action', () => { + const stateWithErrors = { + ...loginInitialState, + loginError: { errorCode: 'SOME_ERROR_CODE', errorContext: {} }, + submitState: FAILURE_STATE, + }; + + const nextState = loginReducer(stateWithErrors, loginErrorClear()); + + expect(nextState.loginError).toEqual({}); + expect(nextState.submitState).toEqual(DEFAULT_STATE); + }); + + it('should handle setLoginSSOIntent action', () => { + const nextState = loginReducer(loginInitialState, setLoginSSOIntent()); + + expect(nextState.isLoginSSOIntent).toEqual(true); + }); + + it('should handle backupLoginForm action', () => { + const mockPayload = { + formFields: { + emailOrUsername: 'john_doe@example.com', + password: 'password123', + }, + errors: { + emailOrUsername: '', + password: '', + }, + }; + + const nextState = loginReducer(loginInitialState, backupLoginForm(mockPayload)); + + expect(nextState.loginFormData).toEqual(mockPayload); + }); }); 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); + }); }); }); diff --git a/src/onboarding-component/data/tests/utils.test.js b/src/onboarding-component/data/tests/utils.test.js index 6e70963d..6a9392e6 100644 --- a/src/onboarding-component/data/tests/utils.test.js +++ b/src/onboarding-component/data/tests/utils.test.js @@ -1,4 +1,4 @@ -import validateContextData from '../utils'; +import validateContextData, { objectToQueryString } from '../utils'; describe('validateContextData', () => { it('should filter context data based on VALID_AUTH_PARAMS', () => { @@ -37,3 +37,36 @@ describe('validateContextData', () => { expect(validateContextData(context)).toEqual({}); }); }); + +describe('objectToQueryString', () => { + it('should convert an object to a query string', () => { + const obj = { + key1: 'value1', + key2: 'value2', + }; + const queryString = objectToQueryString(obj); + expect(queryString).toBe('key1=value1&key2=value2'); + }); + + it('should handle special characters in keys and values', () => { + const obj = { + 'key with spaces': 'value with spaces', + 'key&special': 'value?special', + }; + const queryString = objectToQueryString(obj); + expect(queryString).toBe('key%20with%20spaces=value%20with%20spaces&key%26special=value%3Fspecial'); + }); + + it('should return an empty string for an empty object', () => { + expect(objectToQueryString({})).toBe(''); + }); + + it('should handle undefined and null values', () => { + const obj = { + key1: undefined, + key2: null, + }; + const queryString = objectToQueryString(obj); + expect(queryString).toBe('key1=undefined&key2=null'); + }); +}); diff --git a/src/onboarding-component/tests/index.test.jsx b/src/onboarding-component/tests/index.test.jsx index 208a8c83..309cb4c1 100644 --- a/src/onboarding-component/tests/index.test.jsx +++ b/src/onboarding-component/tests/index.test.jsx @@ -4,8 +4,7 @@ import { Provider } from 'react-redux'; import { mergeConfig } from '@edx/frontend-platform'; import { fetchAuthenticatedUser, getAuthenticatedUser } from '@edx/frontend-platform/auth'; import { getLocale, injectIntl, IntlProvider } from '@edx/frontend-platform/i18n'; -import { render } from '@testing-library/react'; -import { act } from 'react-dom/test-utils'; +import { act, render } from '@testing-library/react'; import { MemoryRouter } from 'react-router-dom'; import configureStore from 'redux-mock-store'; @@ -18,8 +17,11 @@ import { PENDING_STATE, PROGRESSIVE_PROFILING_FORM, REGISTRATION_FORM, + RESET_PASSWORD_FORM, } from '../../data/constants'; import { OnboardingComponentContext } from '../../data/storeHooks'; +import { REQUIRE_PASSWORD_CHANGE } from '../../forms/login-popup/data/constants'; +import { TOKEN_STATE } from '../../forms/reset-password-popup/reset-password/data/constants'; import { getThirdPartyAuthContext, setCurrentOpenedForm } from '../data/reducers'; import { OnBoardingComponent, SignInComponent, SignUpComponent } from '../index'; @@ -130,12 +132,7 @@ describe('OnBoardingComponent Test', () => { }); }); - afterEach(() => { - jest.clearAllMocks(); - }); - it('renders login form when rendering SignInComponent', () => { - // It also tests that component is rendered only when isOpen is true const { getByTestId } = render(reduxWrapper( {}} />, )); @@ -163,12 +160,7 @@ describe('OnBoardingComponent Test', () => { }); }); - afterEach(() => { - jest.clearAllMocks(); - }); - it('renders registration form when rendering SignUpComponent', () => { - // It also tests that component is rendered only when isOpen is true const { getByTestId } = render(reduxWrapper( {}} />, )); @@ -356,4 +348,66 @@ describe('OnBoardingComponent Test', () => { expect(fetchAuthenticatedUser).toBeCalledWith({ forceRefresh: false }); }); + + it('sets hasCloseButton to false for FORGOT_PASSWORD_FORM with REQUIRE_PASSWORD_CHANGE error', () => { + store = mockStore({ + ...initialState, + login: { + ...initialState.login, + loginError: { errorCode: REQUIRE_PASSWORD_CHANGE }, + }, + commonData: { + ...initialState.commonData, + currentForm: FORGOT_PASSWORD_FORM, + }, + }); + + const { container } = render(reduxWrapper( {}} />)); + + // Assuming the close button is rendered conditionally based on the hasCloseButton attribute + const closeButton = container.querySelector('.close-button-selector'); // Update selector as per actual implementation + expect(closeButton).toBeNull(); + }); + + it('sets hasCloseButton to false for RESET_PASSWORD_FORM with pending status', () => { + store = mockStore({ + ...initialState, + resetPassword: { + ...initialState.resetPassword, + status: TOKEN_STATE.PENDING, + }, + commonData: { + ...initialState.commonData, + currentForm: RESET_PASSWORD_FORM, + }, + }); + + const { container } = render(reduxWrapper( + {}} />, + )); + + const component = container.querySelector('BaseContainer'); + expect(component).toBeNull(); + }); + + it('sets hasCloseButton to true for RESET_PASSWORD_FORM with non-pending status', () => { + store = mockStore({ + ...initialState, + resetPassword: { + ...initialState.resetPassword, + status: TOKEN_STATE.COMPLETE, + }, + commonData: { + ...initialState.commonData, + currentForm: RESET_PASSWORD_FORM, + }, + }); + + const { container } = render(reduxWrapper( + {}} />, + )); + + const component = container.querySelector('BaseContainer'); + expect(component).toBeNull(); + }); });