Skip to content

Commit

Permalink
test: Achieved 100% test coverage and fixed uncovered lines (#1068)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Altered the formData

- Altered the formData to make sure all are related to the organization name.

Signed-off-by: Akhilender <[email protected]>

---------

Signed-off-by: Akhilender <[email protected]>
  • Loading branch information
akhilender-bongirwar authored Nov 18, 2023
1 parent 4e9265c commit 816e761
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions src/components/UserPasswordUpdate/UserPasswordUpdate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<UserPasswordUpdate {...props} />
</I18nextProvider>
</MockedProvider>
);

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(
<MockedProvider addTypename={false} link={link}>
<I18nextProvider i18n={i18nForTest}>
<UserPasswordUpdate {...props} />
</I18nextProvider>
</MockedProvider>
);

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.'
);
});
});

0 comments on commit 816e761

Please sign in to comment.