generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from Arquisoft/feat/webapp/changes
More descriptive errors for the login and signup
- Loading branch information
Showing
7 changed files
with
122 additions
and
29 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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, act } from '@testing-library/react'; | ||
import { render, fireEvent, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { MemoryRouter } from 'react-router'; | ||
import Login from '../pages/Login'; | ||
|
@@ -8,7 +8,6 @@ import MockAdapter from 'axios-mock-adapter'; | |
import { HttpStatusCode } from 'axios'; | ||
import { ChakraProvider } from '@chakra-ui/react'; | ||
import theme from '../styles/theme'; | ||
import Signup from 'pages/Signup'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
|
@@ -87,4 +86,48 @@ describe('Login Component', () => { | |
expect(getByTestId('error-message')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('renders button "GoBack"', () => { | ||
const { getByTestId } = render( | ||
<ChakraProvider theme={theme}> | ||
<MemoryRouter initialEntries={['/signup']}> | ||
<Login /> | ||
</MemoryRouter> | ||
</ChakraProvider> | ||
); | ||
const goBackButton = getByTestId('GoBack'); | ||
expect(goBackButton).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays error message on failed format login attempt', async () => { | ||
mockAxios.onPost().replyOnce(HttpStatusCode.BadRequest); | ||
const { getByPlaceholderText, getByTestId } = render(<ChakraProvider theme={theme}><MemoryRouter><Login /></MemoryRouter></ChakraProvider>); | ||
const emailInput = getByPlaceholderText('session.email'); | ||
const passwordInput = getByPlaceholderText('session.password'); | ||
const loginButton = getByTestId('Login'); | ||
|
||
fireEvent.change(emailInput, { target: { value: 'test' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'test' } }); | ||
fireEvent.click(loginButton); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('error-message')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays error message on unauthorized login attempt', async () => { | ||
mockAxios.onPost().replyOnce(HttpStatusCode.Unauthorized); | ||
const { getByPlaceholderText, getByTestId } = render(<ChakraProvider theme={theme}><MemoryRouter><Login /></MemoryRouter></ChakraProvider>); | ||
const emailInput = getByPlaceholderText('session.email'); | ||
const passwordInput = getByPlaceholderText('session.password'); | ||
const loginButton = getByTestId('Login'); | ||
|
||
fireEvent.change(emailInput, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'test' } }); | ||
fireEvent.click(loginButton); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('error-message')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -4,9 +4,14 @@ import { MemoryRouter } from 'react-router'; | |
import Signup from '../pages/Signup'; | ||
import { ChakraProvider } from '@chakra-ui/react'; | ||
import theme from '../styles/theme'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import AuthManager from 'components/auth/AuthManager'; | ||
import { HttpStatusCode } from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useNavigate: jest.fn(), | ||
})); | ||
|
||
jest.mock('react-i18next', () => ({ | ||
useTranslation: () => { | ||
|
@@ -19,8 +24,17 @@ jest.mock('react-i18next', () => ({ | |
}, | ||
})); | ||
|
||
const authManager = new AuthManager(); | ||
let mockAxios = new MockAdapter(authManager.getAxiosInstance()); | ||
|
||
describe('Signup Component', () => { | ||
|
||
beforeEach(() => { | ||
authManager.reset(); | ||
jest.clearAllMocks(); | ||
mockAxios = new MockAdapter(authManager.getAxiosInstance()); | ||
}); | ||
|
||
it('renders form elements correctly', () => { | ||
const { getByPlaceholderText } = render(<ChakraProvider theme={theme}><MemoryRouter><Signup /></MemoryRouter></ChakraProvider>); | ||
|
||
|
@@ -74,4 +88,35 @@ describe('Signup Component', () => { | |
expect(confirmPasswordInput.value).toBe('newPassword'); | ||
}); | ||
|
||
it('displays error message on failed register attempt', async () => { | ||
mockAxios.onPost().replyOnce(HttpStatusCode.BadRequest); | ||
const { getByPlaceholderText, getByTestId } = render(<ChakraProvider theme={theme}><MemoryRouter><Signup /></MemoryRouter></ChakraProvider>); | ||
const emailInput = getByPlaceholderText('session.email'); | ||
const passwordInput = getByPlaceholderText('session.password'); | ||
const registerButton = getByTestId('Sign up'); | ||
|
||
fireEvent.change(emailInput, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'password123' } }); | ||
fireEvent.click(registerButton); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('error-message')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays error message on unauthorized register attempt', async () => { | ||
mockAxios.onPost().replyOnce(HttpStatusCode.Unauthorized); | ||
const { getByPlaceholderText, getByTestId } = render(<ChakraProvider theme={theme}><MemoryRouter><Signup /></MemoryRouter></ChakraProvider>); | ||
const emailInput = getByPlaceholderText('session.email'); | ||
const passwordInput = getByPlaceholderText('session.password'); | ||
const registerButton = getByTestId('Sign up'); | ||
|
||
fireEvent.change(emailInput, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'test' } }); | ||
fireEvent.click(registerButton); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('error-message')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |