-
-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve code coverage in `src/components/UserPortal/Register/Register…
….tsx` (#3248) * Improve code coverage in register.tsx * Achieved 100% Code coverage
- Loading branch information
Showing
2 changed files
with
66 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import { vi } from 'vitest'; | |
* GraphQL mock data is used for testing user registration functionality. | ||
*/ | ||
|
||
// GraphQL Mock Data | ||
const MOCKS = [ | ||
{ | ||
request: { | ||
|
@@ -54,6 +55,7 @@ const MOCKS = [ | |
}, | ||
]; | ||
|
||
// Form Data | ||
const formData = { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
|
@@ -62,16 +64,26 @@ const formData = { | |
confirmPassword: 'johnDoe', | ||
}; | ||
|
||
const link = new StaticMockLink(MOCKS, true); | ||
// Additional GraphQL Mock Data for Error Handling | ||
const ERROR_MOCKS = [ | ||
{ | ||
request: { | ||
query: SIGNUP_MUTATION, | ||
variables: { | ||
firstName: 'Error', | ||
lastName: 'Test', | ||
email: '[email protected]', | ||
password: 'password', | ||
}, | ||
}, | ||
error: new Error('GraphQL error occurred'), | ||
}, | ||
]; | ||
|
||
async function wait(ms = 100): Promise<void> { | ||
await act(() => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
}); | ||
} | ||
// Static Mock Link | ||
const link = new StaticMockLink(MOCKS, true); | ||
|
||
// Mock toast | ||
vi.mock('react-toastify', () => ({ | ||
toast: { | ||
success: vi.fn(), | ||
|
@@ -80,12 +92,18 @@ vi.mock('react-toastify', () => ({ | |
}, | ||
})); | ||
|
||
// Mock setCurrentMode function | ||
const setCurrentMode: React.Dispatch<SetStateAction<string>> = vi.fn(); | ||
|
||
// Test setup props | ||
const props = { | ||
setCurrentMode, | ||
}; | ||
|
||
async function waitForAsync(): Promise<void> { | ||
await act(() => new Promise((resolve) => setTimeout(resolve, 100))); | ||
} | ||
|
||
describe('Testing Register Component [User Portal]', () => { | ||
it('Component should be rendered properly', async () => { | ||
render( | ||
|
@@ -100,7 +118,7 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
}); | ||
|
||
it('Expect the mode to be changed to Login', async () => { | ||
|
@@ -116,7 +134,7 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.click(screen.getByTestId('setLoginBtn')); | ||
|
||
|
@@ -136,7 +154,7 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
||
|
@@ -156,7 +174,7 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.type(screen.getByTestId('emailInput'), formData.email); | ||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
@@ -177,12 +195,10 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.type(screen.getByTestId('passwordInput'), formData.password); | ||
|
||
userEvent.type(screen.getByTestId('emailInput'), formData.email); | ||
|
||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
||
expect(toast.error).toHaveBeenCalledWith('Please enter valid details.'); | ||
|
@@ -201,20 +217,17 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.type(screen.getByTestId('passwordInput'), formData.password); | ||
|
||
userEvent.type(screen.getByTestId('emailInput'), formData.email); | ||
|
||
userEvent.type(screen.getByTestId('firstNameInput'), formData.firstName); | ||
|
||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
||
expect(toast.error).toHaveBeenCalledWith('Please enter valid details.'); | ||
}); | ||
|
||
test("Expect toast.error to be called if confirmPassword doesn't match with password", async () => { | ||
it("Expect toast.error to be called if confirmPassword doesn't match with password", async () => { | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
|
@@ -227,16 +240,12 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.type(screen.getByTestId('passwordInput'), formData.password); | ||
|
||
userEvent.type(screen.getByTestId('emailInput'), formData.email); | ||
|
||
userEvent.type(screen.getByTestId('firstNameInput'), formData.firstName); | ||
|
||
userEvent.type(screen.getByTestId('lastNameInput'), formData.lastName); | ||
|
||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
||
expect(toast.error).toHaveBeenCalledWith( | ||
|
@@ -257,27 +266,52 @@ describe('Testing Register Component [User Portal]', () => { | |
</MockedProvider>, | ||
); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
userEvent.type(screen.getByTestId('passwordInput'), formData.password); | ||
|
||
userEvent.type( | ||
screen.getByTestId('confirmPasswordInput'), | ||
formData.confirmPassword, | ||
); | ||
|
||
userEvent.type(screen.getByTestId('emailInput'), formData.email); | ||
|
||
userEvent.type(screen.getByTestId('firstNameInput'), formData.firstName); | ||
|
||
userEvent.type(screen.getByTestId('lastNameInput'), formData.lastName); | ||
|
||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
||
await wait(); | ||
await waitForAsync(); | ||
|
||
expect(toast.success).toHaveBeenCalledWith( | ||
'Successfully registered. Please wait for admin to approve your request.', | ||
); | ||
}); | ||
|
||
// Error Test Case | ||
it('Expect toast.error to be called if GraphQL mutation fails', async () => { | ||
render( | ||
<MockedProvider addTypename={false} mocks={ERROR_MOCKS}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Register {...props} /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider>, | ||
); | ||
|
||
await waitForAsync(); | ||
|
||
// Fill out the form with error-triggering values | ||
userEvent.type(screen.getByTestId('passwordInput'), 'password'); | ||
userEvent.type(screen.getByTestId('confirmPasswordInput'), 'password'); | ||
userEvent.type(screen.getByTestId('emailInput'), '[email protected]'); | ||
userEvent.type(screen.getByTestId('firstNameInput'), 'Error'); | ||
userEvent.type(screen.getByTestId('lastNameInput'), 'Test'); | ||
userEvent.click(screen.getByTestId('registerBtn')); | ||
|
||
await waitForAsync(); | ||
|
||
// Assert that toast.error is called with the error message | ||
expect(toast.error).toHaveBeenCalledWith('GraphQL error occurred'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters