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.
feat: Adding more tests for the Signup page
- Loading branch information
1 parent
dbe67ff
commit 42c7cb3
Showing
1 changed file
with
42 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -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,31 @@ describe('Signup Component', () => { | |
expect(confirmPasswordInput.value).toBe('newPassword'); | ||
}); | ||
|
||
it('displays error message on failed login 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 loginButton = getByTestId('Sign up'); | ||
|
||
fireEvent.change(emailInput, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'password123' } }); | ||
fireEvent.click(loginButton); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('error-message')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('renders button "GoBack"', () => { | ||
const { getByTestId } = render( | ||
<ChakraProvider theme={theme}> | ||
<MemoryRouter initialEntries={['/signup']}> | ||
<Signup /> | ||
</MemoryRouter> | ||
</ChakraProvider> | ||
); | ||
const goBackButton = getByTestId('GoBack'); | ||
expect(goBackButton).toBeInTheDocument(); | ||
}); | ||
}); |