diff --git a/webapp/src/components/loginAndRegistration/AddUser.test.js b/webapp/src/components/loginAndRegistration/AddUser.test.js index 8cf04089..56cbfb5f 100644 --- a/webapp/src/components/loginAndRegistration/AddUser.test.js +++ b/webapp/src/components/loginAndRegistration/AddUser.test.js @@ -1,14 +1,19 @@ import React from 'react'; -import { render, screen } from '@testing-library/react'; +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import AddUser from './AddUser'; -import { BrowserRouter as Router } from 'react-router-dom'; +import axios from 'axios'; +import { BrowserRouter as Router } from 'react-router-dom'; // Mocking useTranslation hook jest.mock('react-i18next', () => ({ useTranslation: () => ({ t: key => key }), })); +// Mocking axios to simulate an error response +jest.mock('axios'); + describe('', () => { + test('renders the AddUser component', () => { render( @@ -24,6 +29,150 @@ describe('', () => { expect(screen.getByText('addUser.login_link')).toBeInTheDocument(); }); + + test('displays error message when passwords do not match', () => { + render( + + + + ); + + const usernameInput = screen.getByPlaceholderText('addUser.username_placeholder'); + fireEvent.change(usernameInput, { target: { value: 'username' } }); + + const passwordInput = screen.getByPlaceholderText('addUser.password_placeholder'); + fireEvent.change(passwordInput, { target: { value: '12345678' } }); + + const repeatPasswordInput = screen.getByPlaceholderText('addUser.repeat_password_placeholder'); + fireEvent.change(repeatPasswordInput, { target: { value: '123456789' } }); + + const submitButton = screen.getByText('addUser.register_button'); + fireEvent.click(submitButton); + + expect(screen.getByText('addUser.error_passwords_no_match')).toBeInTheDocument(); + }); + + test('displays error message when spaces in password', () => { + render( + + + + ); + + const usernameInput = screen.getByPlaceholderText('addUser.username_placeholder'); + fireEvent.change(usernameInput, { target: { value: 'username' } }); + + const passwordInput = screen.getByPlaceholderText('addUser.password_placeholder'); + fireEvent.change(passwordInput, { target: { value: '1234 5678' } }); + + const repeatPasswordInput = screen.getByPlaceholderText('addUser.repeat_password_placeholder'); + fireEvent.change(repeatPasswordInput, { target: { value: '1234 5678' } }); + + const submitButton = screen.getByText('addUser.register_button'); + fireEvent.click(submitButton); + + expect(screen.getByText('addUser.error_password_spaces')).toBeInTheDocument(); + }); + + test('displays error message when passwords too short', () => { + render( + + + + ); + + const usernameInput = screen.getByPlaceholderText('addUser.username_placeholder'); + fireEvent.change(usernameInput, { target: { value: 'username' } }); + + const passwordInput = screen.getByPlaceholderText('addUser.password_placeholder'); + fireEvent.change(passwordInput, { target: { value: '1234567' } }); + + const repeatPasswordInput = screen.getByPlaceholderText('addUser.repeat_password_placeholder'); + fireEvent.change(repeatPasswordInput, { target: { value: '1234567' } }); + + const submitButton = screen.getByText('addUser.register_button'); + fireEvent.click(submitButton); + + expect(screen.getByText('addUser.error_password_minimum_length')).toBeInTheDocument(); + }); + + test('displays error message when passwords too long', () => { + render( + + + + ); + + const usernameInput = screen.getByPlaceholderText('addUser.username_placeholder'); + fireEvent.change(usernameInput, { target: { value: 'username' } }); + + const passwordInput = screen.getByPlaceholderText('addUser.password_placeholder'); + fireEvent.change(passwordInput, { target: { value: '01234567890123456789012345678901234567890123456789012345678901234' } }); + + const repeatPasswordInput = screen.getByPlaceholderText('addUser.repeat_password_placeholder'); + fireEvent.change(repeatPasswordInput, { target: { value: '01234567890123456789012345678901234567890123456789012345678901234' } }); + + const submitButton = screen.getByText('addUser.register_button'); + fireEvent.click(submitButton); + + expect(screen.getByText('addUser.error_password_maximum_length')).toBeInTheDocument(); + }); + + test('displays error message when spaces in username', () => { + render( + + + + ); + + const usernameInput = screen.getByPlaceholderText('addUser.username_placeholder'); + fireEvent.change(usernameInput, { target: { value: 'user name' } }); + + const passwordInput = screen.getByPlaceholderText('addUser.password_placeholder'); + fireEvent.change(passwordInput, { target: { value: '12345678' } }); + + const repeatPasswordInput = screen.getByPlaceholderText('addUser.repeat_password_placeholder'); + fireEvent.change(repeatPasswordInput, { target: { value: '12345678' } }); + + const submitButton = screen.getByText('addUser.register_button'); + fireEvent.click(submitButton); + + expect(screen.getByText('addUser.error_username_spaces')).toBeInTheDocument(); + }); + + test('displays error message when username is already in use', async () => { + + // Mock axios post method to simulate response for username already in use + axios.post.mockRejectedValue({ response: { data: { error: 'Username already in use' } } }); + + render( + + + + ); + + const usernameInput = screen.getByPlaceholderText('addUser.username_placeholder'); + fireEvent.change(usernameInput, { target: { value: 'existing_user' } }); + + const passwordInput = screen.getByPlaceholderText('addUser.password_placeholder'); + fireEvent.change(passwordInput, { target: { value: '12345678' } }); + + const repeatPasswordInput = screen.getByPlaceholderText('addUser.repeat_password_placeholder'); + fireEvent.change(repeatPasswordInput, { target: { value: '12345678' } }); + + const submitButton = screen.getByText('addUser.register_button'); + fireEvent.click(submitButton); + + // Wait for the asynchronous axios call to be completed + await waitFor(() => { + expect(screen.getByText('addUser.error_username_in_use')).toBeInTheDocument(); + }); + + // Ensure axios.post is called with the correct data + expect(axios.post).toHaveBeenCalledWith(expect.any(String), { username: 'existing_user', password: '12345678' }); + + }); + });