diff --git a/webapp/src/components/tests/AddUser.test.js b/webapp/src/components/tests/AddUser.test.js index 2613dd5..fe76461 100644 --- a/webapp/src/components/tests/AddUser.test.js +++ b/webapp/src/components/tests/AddUser.test.js @@ -4,91 +4,79 @@ import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import AddUser from '../AddUser'; - const mockAxios = new MockAdapter(axios); - const mockedUsernames = ['existingUser']; - mockAxios.onGet(`http://localhost:8000/getUsernames`).reply(200, { usernames: mockedUsernames }); - - const usernameInput = screen.getByLabelText(/Usuario/i); - const passwordInput = screen.getByLabelText(/Contraseña/i); - const addUserButton = screen.getByRole('button', { name: /Crear/i }); +const mockAxios = new MockAdapter(axios); describe('AddUser component', () => { beforeEach(() => { mockAxios.reset(); }); - it('should add user successfully', async () => { - render(); - // Mock the axios.post request to simulate a successful response + const renderComponent = () => render(); + + const setupMockedUsernames = (usernames) => { + mockAxios.onGet(`http://localhost:8000/getUsernames`).reply(200, { usernames }); + }; + + const setupSuccessfulResponse = () => { mockAxios.onPost('http://localhost:8000/adduser').reply(200); + }; - // Simulate user input - fireEvent.change(usernameInput, { target: { value: 'testUser' } }); - fireEvent.change(passwordInput, { target: { value: 'testPassword' } }); + const setupErrorResponse = (statusCode, errorMessage) => { + mockAxios.onPost('http://localhost:8000/adduser').reply(statusCode, { error: errorMessage }); + }; - // Trigger the add user button click - fireEvent.click(addUserButton); + const typeUsernameAndPassword = (username, password) => { + const usernameInput = screen.getByLabelText(/Usuario/i); + const passwordInput = screen.getByLabelText(/Contraseña/i); + fireEvent.change(usernameInput, { target: { value: username } }); + fireEvent.change(passwordInput, { target: { value: password } }); + }; + + const clickCreateUserButton = () => { + const createUserButton = screen.getByRole('button', { name: /Crear/i }); + fireEvent.click(createUserButton); + }; - // Wait for the Snackbar to be open + it('should add user successfully', async () => { + renderComponent(); + setupSuccessfulResponse(); + typeUsernameAndPassword('testUser', 'testPassword'); + clickCreateUserButton(); await waitFor(() => { expect(screen.getByText(/Usuario creado correctamente/i)).toBeInTheDocument(); }); }); it('should handle error when adding user', async () => { - render(); - // Mock the axios.post request to simulate an error response - mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' }); - - // Simulate user input - fireEvent.change(usernameInput, { target: { value: 'testUser' } }); - fireEvent.change(passwordInput, { target: { value: 'testPassword' } }); - - // Trigger the add user button click - fireEvent.click(addUserButton); - - // Wait for the error Snackbar to be open + renderComponent(); + setupErrorResponse(500, 'Internal Server Error'); + typeUsernameAndPassword('testUser', 'testPassword'); + clickCreateUserButton(); await waitFor(() => { expect(screen.getByText(/Error: Internal Server Error/i)).toBeInTheDocument(); }); }); - it('should show error of password too short', async () => { - render(); - // Mock the axios.post request to simulate a successful response - mockAxios.onPost('http://localhost:8000/adduser').reply(200); - - // Simulate user input - fireEvent.change(usernameInput, { target: { value: 'testUser' } }); - fireEvent.change(passwordInput, { target: { value: 'sh' } }); - - // Trigger the add user button click - fireEvent.click(addUserButton); - - // Wait for the Snackbar to be open + it('should show error message for short password', async () => { + renderComponent(); + setupMockedUsernames([]); + setupSuccessfulResponse(); + typeUsernameAndPassword('testUser', 'sh'); + clickCreateUserButton(); await waitFor(() => { expect(screen.getByText(/Error: Credenciales incorrectas. La contraseña debe contener al menos 8 caracteres/i)).toBeInTheDocument(); }); }); - it('should show error of repeated user', async () => { - render(); - // Mock the axios.post request to simulate a successful response - mockAxios.onPost('http://localhost:8000/adduser').reply(200); - - // Simulate user input - fireEvent.change(usernameInput, { target: { value: 'existingUser' } }); - fireEvent.change(passwordInput, { target: { value: 'passwordCorrect' } }); - - // Trigger the add user button click - fireEvent.click(addUserButton); - - // Wait for the Snackbar to be open + it('should show error message for repeated username', async () => { + renderComponent(); + setupMockedUsernames(['existingUser']); + setupSuccessfulResponse(); + typeUsernameAndPassword('existingUser', 'passwordCorrect'); + clickCreateUserButton(); await waitFor(() => { expect(screen.getByText(/Error: Credenciales incorrectas. El nombre de usuario esta en uso/i)).toBeInTheDocument(); }); }); - }); -