-
Notifications
You must be signed in to change notification settings - Fork 0
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
Test Login page #87
Closed
Closed
Test Login page #87
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react' | ||
import { act } from 'react-dom/test-utils' | ||
import { MemoryRouter } from 'react-router-dom' | ||
import axios from 'axios' | ||
import Login from '../src/pages/Login' | ||
|
||
jest.mock('../src/assets/logo.png', () => './__mocks__/logoMock.js') | ||
jest.mock('axios') | ||
|
||
describe('Login Component', () => { | ||
// TEST #1 | ||
test('renders Login component', () => { | ||
render(<MemoryRouter><Login /></MemoryRouter>) | ||
}) | ||
|
||
// TEST #2 | ||
test('renders Login component with logo', () => { | ||
render(<MemoryRouter><Login /></MemoryRouter>) | ||
const logoElement = screen.getByRole('img', { name: '' }) | ||
expect(logoElement).toBeInTheDocument() | ||
expect(logoElement).toHaveClass('size-1/2 object-contain') | ||
expect(logoElement).toHaveAttribute('src', './__mocks__/logoMock.js') | ||
}) | ||
|
||
// TEST #3 | ||
test('renders login form with input fields', () => { | ||
render(<MemoryRouter><Login /></MemoryRouter>) | ||
const usernameInput = screen.getByPlaceholderText('Username') | ||
const passwordInput = screen.getByPlaceholderText('Password') | ||
const loginButton = screen.getByRole('button', { name: 'Login' }) | ||
|
||
expect(usernameInput).toBeInTheDocument() | ||
expect(passwordInput).toBeInTheDocument() | ||
expect(loginButton).toBeInTheDocument() | ||
}) | ||
|
||
// TEST #4 | ||
test('renders link to sign up page', () => { | ||
render(<MemoryRouter><Login /></MemoryRouter>) | ||
const signUpLink = screen.getByRole('link', { name: /Sign up here/i }) | ||
|
||
expect(signUpLink).toBeInTheDocument() | ||
expect(signUpLink).toHaveAttribute('href', '/signup') | ||
}) | ||
|
||
// TEST #5 | ||
// test('form submission with valid data and successful response', async () => { | ||
// axios.post.mockResolvedValue({ | ||
// status: 200, | ||
// data: { token: 'dummyToken' }, | ||
// }) | ||
|
||
// render(<MemoryRouter><Login /></MemoryRouter>) | ||
|
||
// // Simulate user input | ||
// const usernameInput = screen.getByPlaceholderText('Username') | ||
// const passwordInput = screen.getByPlaceholderText('Password') | ||
|
||
// fireEvent.change(usernameInput, { target: { value: 'username' } }) | ||
// fireEvent.change(passwordInput, { target: { value: 'password' } }) | ||
|
||
// // Simulate form submission | ||
// fireEvent.click(screen.getByRole('button', { name: 'Login' })) | ||
|
||
// // Wait for axios post call to be made | ||
// await waitFor(() => expect(axios.post).toHaveBeenCalledWith( | ||
// 'http://localhost:8420/v1/login', | ||
// { username: 'username', password: 'password' } | ||
// )) | ||
|
||
// // Wait for the navigation to complete | ||
// await waitFor(() => { | ||
// // Try finding the element by data-testid | ||
// const homeComponent = screen.queryByTestId('home-component') | ||
// expect(homeComponent).toBeInTheDocument() | ||
// }, { timeout: 5000 }) | ||
// }) | ||
|
||
// TEST #7 | ||
// test('form submission with valid data and unsuccessful response', async () => { | ||
// // Mock axios.post to reject with an unsuccessful response | ||
// axios.post.mockRejectedValue({ | ||
// response: { | ||
// status: 401, | ||
// data: { error: 'Authentication failed' }, // Add an error message similar to a real response | ||
// }, | ||
// }) | ||
|
||
// render(<MemoryRouter><Login /></MemoryRouter>) | ||
|
||
// // Simulate user input | ||
// const usernameInput = screen.getByPlaceholderText('Username') | ||
// const passwordInput = screen.getByPlaceholderText('Password') | ||
|
||
// fireEvent.change(usernameInput, { target: { value: 'username' } }) | ||
// fireEvent.change(passwordInput, { target: { value: 'password' } }) | ||
|
||
// // Simulate form submission | ||
// fireEvent.click(screen.getByRole('button', { name: 'Login' })) | ||
|
||
// // Wait for axios post call to be made | ||
// await waitFor(() => expect(axios.post).toHaveBeenCalledWith( | ||
// 'http://localhost:8420/v1/login', | ||
// { username: 'username', password: 'password' } | ||
// )) | ||
|
||
// // Wait for the error message to be displayed | ||
// waitFor(() => { | ||
// const errorMessage = screen.getByText('Authentication failed') | ||
// expect(errorMessage).toBeInTheDocument() | ||
// }, { timeout: 10000 }) | ||
// }) | ||
}) |
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
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.
I am not sure about these assertions, its testing the rendering of the login form component which already has its own render tests. A better test could be rending the Login page with the image, LoginForm component mocked, and
then assert that all of those elements are present in the rendered Login page
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.
Also adding I think it should all be in 1 test. So a "good" Login page has to be rendered with all three- the Logo, , and and the para. This should be 1 test that asserts that all 3 are present.