From 816e7615d1b28857b6fba467db367234e62dd3d1 Mon Sep 17 00:00:00 2001 From: Akhilender Bongirwar <112749383+akhilender-bongirwar@users.noreply.github.com> Date: Sun, 19 Nov 2023 00:17:44 +0530 Subject: [PATCH] test: Achieved 100% test coverage and fixed uncovered lines (#1068) * test: Achieved 100% test coverage and fixed uncovered lines - Improved the test coverage for the User-Password-Update component, addressing the previously uncovered lines and ensuring that all tests pass successfully. - Added two new tests 1. Empty Password Field Test: - The first test ensures that an error is displayed when attempting to save changes with an empty password field. 2. Mismatched New and Confirm Passwords Test - The second test covers the scenario where the new and confirm password fields do not match. With these new tests, I now have 100% test coverage, and there are no more uncovered lines. Signed-off-by: Akhilender * Altered the formData - Altered the formData to make sure all are related to the organization name. Signed-off-by: Akhilender --------- Signed-off-by: Akhilender --- .../UserPasswordUpdate.test.tsx | 66 +++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx b/src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx index 7b07db0cc7..285b430c59 100644 --- a/src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx +++ b/src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx @@ -3,11 +3,18 @@ import { act, render, screen } from '@testing-library/react'; import { MockedProvider } from '@apollo/react-testing'; import userEvent from '@testing-library/user-event'; import { I18nextProvider } from 'react-i18next'; - import { UPDATE_USER_PASSWORD_MUTATION } from 'GraphQl/Mutations/mutations'; import i18nForTest from 'utils/i18nForTest'; import UserPasswordUpdate from './UserPasswordUpdate'; import { StaticMockLink } from 'utils/StaticMockLink'; +import { toast as mockToast } from 'react-toastify'; + +jest.mock('react-toastify', () => ({ + toast: { + error: jest.fn(), + success: jest.fn(), + }, +})); const MOCKS = [ { @@ -48,9 +55,10 @@ describe('Testing User Password Update', () => { }; const formData = { - previousPassword: 'anshgoyal', - newPassword: 'anshgoyalansh', - confirmNewPassword: 'anshgoyalansh', + previousPassword: 'Palisadoes', + newPassword: 'ThePalisadoesFoundation', + wrongPassword: 'This is wrong passoword', + confirmNewPassword: 'ThePalisadoesFoundation', }; global.alert = jest.fn(); @@ -89,4 +97,54 @@ describe('Testing User Password Update', () => { screen.getByPlaceholderText(/Confirm New Password/i) ).toBeInTheDocument(); }); + + test('displays an error when the password field is empty', async () => { + render( + + + + + + ); + + userEvent.click(screen.getByText(/Save Changes/i)); + + await wait(); + expect(mockToast.error).toHaveBeenCalledWith( + 'The password field cannot be empty.' + ); + }); + + test('displays an error when new and confirm password field does not match', async () => { + render( + + + + + + ); + + await wait(); + + userEvent.type( + screen.getByPlaceholderText(/Previous Password/i), + formData.previousPassword + ); + userEvent.type( + screen.getAllByPlaceholderText(/New Password/i)[0], + formData.wrongPassword + ); + userEvent.type( + screen.getByPlaceholderText(/Confirm New Password/i), + formData.confirmNewPassword + ); + + userEvent.click(screen.getByText(/Save Changes/i)); + + expect(screen.getByText(/Cancel/i)).toBeTruthy(); + await wait(); + expect(mockToast.error).toHaveBeenCalledWith( + 'New and Confirm password do not match.' + ); + }); });