Skip to content

Commit

Permalink
feat: Adding more tests for the Signup page
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiorodriguezgarcia committed Apr 23, 2024
1 parent dbe67ff commit 42c7cb3
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion webapp/src/tests/Signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {
Expand All @@ -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>);

Expand Down Expand Up @@ -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();
});
});

0 comments on commit 42c7cb3

Please sign in to comment.