From 8cf122f7a293ed811afafffc953e45e871338c4d Mon Sep 17 00:00:00 2001 From: Akhilender Date: Thu, 16 Nov 2023 23:41:28 +0530 Subject: [PATCH] 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 --- .../UserPasswordUpdate.test.tsx | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx b/src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx index 7b07db0cc7..e16272a770 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 = [ { @@ -50,6 +57,7 @@ describe('Testing User Password Update', () => { const formData = { previousPassword: 'anshgoyal', newPassword: 'anshgoyalansh', + wrongPassword: 'akhil', confirmNewPassword: 'anshgoyalansh', }; @@ -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.' + ); + }); });