From 42c7cb335e47cfeb873e345b9f108235a0127ec4 Mon Sep 17 00:00:00 2001
From: sergiorodriguezgarcia
<113514397+sergiorodriguezgarcia@users.noreply.github.com>
Date: Tue, 23 Apr 2024 16:20:59 +0200
Subject: [PATCH] feat: Adding more tests for the Signup page
---
webapp/src/tests/Signup.test.js | 43 ++++++++++++++++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)
diff --git a/webapp/src/tests/Signup.test.js b/webapp/src/tests/Signup.test.js
index 9b2112b6..550fe67a 100644
--- a/webapp/src/tests/Signup.test.js
+++ b/webapp/src/tests/Signup.test.js
@@ -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();
@@ -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();
+ const emailInput = getByPlaceholderText('session.email');
+ const passwordInput = getByPlaceholderText('session.password');
+ const loginButton = getByTestId('Sign up');
+
+ fireEvent.change(emailInput, { target: { value: 'test@example.com' } });
+ fireEvent.change(passwordInput, { target: { value: 'password123' } });
+ fireEvent.click(loginButton);
+
+ await waitFor(() => {
+ expect(getByTestId('error-message')).toBeInTheDocument();
+ });
+ });
+
+ it('renders button "GoBack"', () => {
+ const { getByTestId } = render(
+
+
+
+
+
+ );
+ const goBackButton = getByTestId('GoBack');
+ expect(goBackButton).toBeInTheDocument();
+ });
});
\ No newline at end of file