-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
193 additions
and
21 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
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
frontend/src/__tests__/integration-test/signin/client-utils.tsx
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { render } from '@testing-library/react'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import Signin from '@/pages/signin-page'; | ||
import { | ||
PASSWORDINPUT_PLACEHOLDER, | ||
SIGNINBUTTON_TEXT, | ||
SIGNIN_EMAIL_PLACEHOLDER, | ||
} from '@/constants/images'; | ||
|
||
export const formSetup = () => { | ||
const form = render( | ||
<BrowserRouter> | ||
<Signin /> | ||
</BrowserRouter> | ||
); | ||
|
||
const usernameInput = form.getByPlaceholderText(SIGNIN_EMAIL_PLACEHOLDER); | ||
|
||
const passwordInput = form.getByPlaceholderText(PASSWORDINPUT_PLACEHOLDER); | ||
|
||
const signinbuttonText = form.getByText(SIGNINBUTTON_TEXT); | ||
|
||
if ( | ||
!(usernameInput instanceof HTMLInputElement) || | ||
!(passwordInput instanceof HTMLInputElement) || | ||
!(signinbuttonText instanceof HTMLButtonElement) | ||
) { | ||
throw new Error('Issue during test setup, some input elemnts are not rendered'); | ||
} | ||
|
||
return { form, usernameInput, passwordInput, signinbuttonText }; | ||
}; |
70 changes: 70 additions & 0 deletions
70
frontend/src/__tests__/integration-test/signin/signin.test.tsx
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { waitFor } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { formSetup } from './client-utils'; | ||
import { toast } from 'react-toastify'; | ||
|
||
interface sucessdata { | ||
status: number; | ||
message: string; | ||
} | ||
|
||
interface errordata { | ||
status: number; | ||
message: string; | ||
} | ||
|
||
interface Messages { | ||
success: { | ||
render: (data: { data: sucessdata }) => void; | ||
}; | ||
error: { | ||
render: (error: { error: errordata }) => void; | ||
}; | ||
} | ||
|
||
const mockedUseNavigate = vi.fn(); | ||
|
||
// Mocking the useNavigate hook from React Router DOM | ||
vi.mock('react-router-dom', async () => { | ||
const mod = await vi.importActual<typeof import('react-router-dom')>('react-router-dom'); | ||
return { | ||
...mod, | ||
useNavigate: () => mockedUseNavigate, | ||
}; | ||
}); | ||
|
||
// Mock react-toastify's toast.promise | ||
vi.mock('react-toastify', () => ({ | ||
toast: { | ||
promise: vi.fn((promise: Promise<sucessdata>, messages: Messages) => { | ||
return promise.then( | ||
(data: sucessdata) => { | ||
messages.success.render({ data }); | ||
return data; | ||
}, | ||
(error: errordata) => { | ||
messages.error.render({ error }); | ||
throw error; | ||
} | ||
); | ||
}), | ||
}, | ||
})); | ||
|
||
describe('Integration Tests : Signin Component', async () => { | ||
test('Signin : Sucess - Sucessfully sign in', async () => { | ||
const mockedToastPromise = toast.promise; | ||
const userActions = userEvent.setup(); | ||
const { usernameInput, passwordInput, signinbuttonText } = await formSetup(); | ||
|
||
await userActions.type(usernameInput, '[email protected]'); | ||
await userActions.type(passwordInput, 'Test@1234'); | ||
await userActions.click(signinbuttonText); | ||
|
||
await waitFor(() => { | ||
expect(mockedToastPromise).toHaveBeenCalled(); | ||
expect(mockedUseNavigate).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
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
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 |
---|---|---|
|
@@ -7,10 +7,30 @@ import { | |
EMAIL_EMPTY_ERRORMESSAGE, | ||
INVALID_CONFIRMPWD_ERRORMESSAGE, | ||
INVALID_PWD_ERRORMESSAGE, | ||
INVALID_USERNAME_ERRORMESSAGE, | ||
NAME_EMPTY_ERRORMESSAGE, | ||
PASSWORD_EMPTY_ERRORMESSAGE, | ||
USERNAME_EMPTY_ERRORMESSAGE, | ||
} from '@/constants/images'; | ||
import { toast } from 'react-toastify'; | ||
|
||
interface sucessdata { | ||
status: number; | ||
message: string; | ||
} | ||
|
||
interface errordata { | ||
status: number; | ||
message: string; | ||
} | ||
|
||
interface Messages { | ||
success: { | ||
render: (data: { data: sucessdata }) => void; | ||
}; | ||
error: { | ||
render: (error: { error: errordata }) => void; | ||
}; | ||
} | ||
const mockedUseNavigate = vi.fn(); | ||
// Mocking the useNavigate hook from React Router DOM | ||
vi.mock('react-router-dom', async () => { | ||
|
@@ -21,6 +41,24 @@ vi.mock('react-router-dom', async () => { | |
}; | ||
}); | ||
|
||
// Mock react-toastify's toast.promise | ||
vi.mock('react-toastify', () => ({ | ||
toast: { | ||
promise: vi.fn((promise: Promise<sucessdata>, messages: Messages) => { | ||
return promise.then( | ||
(data: sucessdata) => { | ||
messages.success.render({ data }); | ||
return data; | ||
}, | ||
(error: errordata) => { | ||
messages.error.render({ error }); | ||
throw error; | ||
} | ||
); | ||
}), | ||
}, | ||
})); | ||
|
||
describe('Unit Tests : Signup Component', async () => { | ||
test('Signup : Failure - Invalid Email Address', async () => { | ||
const userActions = userEvent.setup(); | ||
|
@@ -42,17 +80,19 @@ describe('Unit Tests : Signup Component', async () => { | |
passwordInput, | ||
confirmpasswordInput, | ||
signupbuttonText, | ||
nameInput, | ||
} = await formSetup(); | ||
await userActions.type(usernameInput, 'aryastark'); | ||
await userActions.type(nameInput, 'arya'); | ||
await userActions.type(emailInput, '[email protected]'); | ||
await userActions.type(passwordInput, '12345678'); | ||
await userActions.type(confirmpasswordInput, '1234'); | ||
await userActions.type(passwordInput, 'Test@1234'); | ||
await userActions.type(confirmpasswordInput, 'Tesr@1897'); | ||
await userActions.click(signupbuttonText); | ||
await waitFor(() => { | ||
expect(form.getByText(INVALID_CONFIRMPWD_ERRORMESSAGE)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
/* | ||
test('Signup : Failure - Invalid Username', async () => { | ||
const userActions = userEvent.setup(); | ||
const { | ||
|
@@ -62,23 +102,27 @@ describe('Unit Tests : Signup Component', async () => { | |
passwordInput, | ||
confirmpasswordInput, | ||
signupbuttonText, | ||
nameInput | ||
} = await formSetup(); | ||
await userActions.type(usernameInput, 'ary'); | ||
await userActions.type(nameInput,'arya') | ||
await userActions.type(emailInput, '[email protected]'); | ||
await userActions.type(passwordInput, '12345678'); | ||
await userActions.type(confirmpasswordInput, '12345678'); | ||
await userActions.type(passwordInput, 'Test@1234'); | ||
await userActions.type(confirmpasswordInput, 'Test@1234'); | ||
await userActions.click(signupbuttonText); | ||
await waitFor(() => { | ||
expect(form.getByText(INVALID_USERNAME_ERRORMESSAGE)).toBeInTheDocument(); | ||
}); | ||
}); | ||
*/ | ||
|
||
test('Signup : Failure - Form is submitted without any values', async () => { | ||
const userActions = userEvent.setup(); | ||
const { form, signupbuttonText } = await formSetup(); | ||
await userActions.click(signupbuttonText); | ||
await waitFor(() => { | ||
expect(form.getByText(USERNAME_EMPTY_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(NAME_EMPTY_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(EMAIL_EMPTY_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(PASSWORD_EMPTY_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(CONFIRMPASSWORD_EMPTY_ERRORMESSAGE)).toBeInTheDocument(); | ||
|
@@ -94,32 +138,43 @@ describe('Unit Tests : Signup Component', async () => { | |
passwordInput, | ||
confirmpasswordInput, | ||
signupbuttonText, | ||
nameInput, | ||
} = await formSetup(); | ||
await userActions.type(usernameInput, 'abcd'); | ||
await userActions.type(nameInput, 'ab'); | ||
await userActions.type(emailInput, '[email protected]'); | ||
await userActions.type(passwordInput, '123'); | ||
await userActions.type(confirmpasswordInput, '1234'); | ||
await userActions.click(signupbuttonText); | ||
await waitFor(() => { | ||
expect(form.getByText(INVALID_USERNAME_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(NAME_EMPTY_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(INVALID_PWD_ERRORMESSAGE)).toBeInTheDocument(); | ||
expect(form.getByText(INVALID_CONFIRMPWD_ERRORMESSAGE)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('should call the signup api when all the input values are valid and should redirect to home page', async () => { | ||
const userActions = userEvent.setup(); | ||
const mockedToastPromise = toast.promise; | ||
|
||
const { usernameInput, emailInput, passwordInput, confirmpasswordInput, signupbuttonText } = | ||
await formSetup(); | ||
const { | ||
usernameInput, | ||
emailInput, | ||
passwordInput, | ||
confirmpasswordInput, | ||
signupbuttonText, | ||
nameInput, | ||
} = await formSetup(); | ||
await userActions.type(nameInput, 'arya'); | ||
await userActions.type(usernameInput, 'aryastark'); | ||
await userActions.type(emailInput, '[email protected]'); | ||
await userActions.type(passwordInput, '123456789'); | ||
await userActions.type(confirmpasswordInput, '123456789'); | ||
await userActions.type(passwordInput, 'Test@1234'); | ||
await userActions.type(confirmpasswordInput, 'Test@1234'); | ||
await userActions.click(signupbuttonText); | ||
|
||
await waitFor(() => { | ||
expect(mockedUseNavigate).toHaveBeenCalledTimes(1); | ||
expect(mockedToastPromise).toHaveBeenCalled(); | ||
expect(mockedUseNavigate).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
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