-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix-#6: integration test completed for signin component #413
Merged
krishnaacharyaa
merged 1 commit into
krishnaacharyaa:main
from
Aswin2255:feat/integrationtest
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import { http, HttpResponse } from 'msw'; | ||
|
||
export const handlers = [ | ||
http.post('http://localhost:5000/api/auth/email-password/signup', () => { | ||
http.post('http://localhost:8080/api/auth/email-password/signup', () => { | ||
return new HttpResponse(null, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No hard coding please |
||
status: 200, | ||
}); | ||
|
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this used multiple places, can we have a seperate types folder? and make it reusable |
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we are doing an integration test with the real API. The jsdom is our frontend and it's default URL is localhost:3000, so if we doesn't include it we will get an cors error during integration test..