From 5fbd4c4aceaca05d8a65fbc039d6923a8403bb9b Mon Sep 17 00:00:00 2001 From: CANCI0 Date: Tue, 2 Apr 2024 18:10:37 +0200 Subject: [PATCH] =?UTF-8?q?Quitado=20c=C3=B3digo=20duplicado=20en=20los=20?= =?UTF-8?q?test=20de=20register?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/Register/Register.test.js | 84 +++++++------------ 1 file changed, 28 insertions(+), 56 deletions(-) diff --git a/webapp/src/components/Register/Register.test.js b/webapp/src/components/Register/Register.test.js index 97977469..0279cc18 100644 --- a/webapp/src/components/Register/Register.test.js +++ b/webapp/src/components/Register/Register.test.js @@ -8,6 +8,26 @@ import { BrowserRouter as Router } from "react-router-dom"; jest.mock("axios"); describe("Register Component", () => { + const mockSuccessResponse = { data: "Usuario registrado exitosamente" }; + const mockErrorResponse = { response: { data: { error: "Error al registrar usuario" } } }; + + const fillRegistrationForm = () => { + fireEvent.change(screen.getByLabelText("Introduce tu nombre:"), { + target: { value: "testUser" }, + }); + fireEvent.change(screen.getByLabelText("Introduce tu contraseña:"), { + target: { value: "testPassword" }, + }); + fireEvent.change(screen.getByLabelText("Vuelve a introducir la contraseña:"), { + target: { value: "testPassword" }, + }); + }; + + const submitRegistrationForm = async () => { + fireEvent.click(screen.getByText("Registrarse")); + await waitFor(() => {}); + }; + test("renders registration form correctly", () => { render( @@ -15,21 +35,15 @@ describe("Register Component", () => { ); - // Verificar que los elementos del formulario se rendericen correctamente expect(screen.getByText("Regístrate")).toBeInTheDocument(); expect(screen.getByLabelText("Introduce tu nombre:")).toBeInTheDocument(); - expect( - screen.getByLabelText("Introduce tu contraseña:") - ).toBeInTheDocument(); - expect( - screen.getByLabelText("Vuelve a introducir la contraseña:") - ).toBeInTheDocument(); + expect(screen.getByLabelText("Introduce tu contraseña:")).toBeInTheDocument(); + expect(screen.getByLabelText("Vuelve a introducir la contraseña:")).toBeInTheDocument(); expect(screen.getByText("Registrarse")).toBeInTheDocument(); expect(screen.getByText("¿Ya tienes cuenta?")).toBeInTheDocument(); }); test("handles user registration successfully", async () => { - const mockSuccessResponse = { data: "Usuario registrado exitosamente" }; axios.post.mockResolvedValueOnce(mockSuccessResponse); render( @@ -38,31 +52,10 @@ describe("Register Component", () => { ); - // Simular la entrada de datos del usuario - fireEvent.change(screen.getByLabelText("Introduce tu nombre:"), { - target: { value: "testUser" }, - }); - fireEvent.change(screen.getByLabelText("Introduce tu contraseña:"), { - target: { value: "testPassword" }, - }); - fireEvent.change( - screen.getByLabelText("Vuelve a introducir la contraseña:"), - { - target: { value: "testPassword" }, - } - ); - - // Simular clic en el botón de registro - fireEvent.click(screen.getByText("Registrarse")); + fillRegistrationForm(); + await submitRegistrationForm(); - // Esperar a que se complete la solicitud y se muestre la confirmación - await waitFor(() => { - expect( - screen.getByText("Usuario registrado exitosamente") - ).toBeInTheDocument(); - }); - - // Verificar que se haya llamado correctamente a la función post de axios + expect(screen.getByText("Usuario registrado exitosamente")).toBeInTheDocument(); expect(axios.post).toHaveBeenCalledWith("http://localhost:8000/adduser", { username: "testUser", password: "testPassword", @@ -70,8 +63,6 @@ describe("Register Component", () => { }); test("handles user registration failure", async () => { - const errorMessage = "Error al registrar usuario"; - const mockErrorResponse = { response: { data: { error: errorMessage } } }; axios.post.mockRejectedValueOnce(mockErrorResponse); render( @@ -80,29 +71,10 @@ describe("Register Component", () => { ); - // Simular la entrada de datos del usuario - fireEvent.change(screen.getByLabelText("Introduce tu nombre:"), { - target: { value: "testUser" }, - }); - fireEvent.change(screen.getByLabelText("Introduce tu contraseña:"), { - target: { value: "testPassword" }, - }); - fireEvent.change( - screen.getByLabelText("Vuelve a introducir la contraseña:"), - { - target: { value: "testPassword" }, - } - ); - - // Simular clic en el botón de registro - fireEvent.click(screen.getByText("Registrarse")); - - // Esperar a que se muestre el mensaje de error - await waitFor(() => { - expect(screen.getByText(`Error: ${errorMessage}`)).toBeInTheDocument(); - }); + fillRegistrationForm(); + await submitRegistrationForm(); - // Verificar que se haya llamado correctamente a la función post de axios + expect(screen.getByText("Error: Error al registrar usuario")).toBeInTheDocument(); expect(axios.post).toHaveBeenCalledWith("http://localhost:8000/adduser", { username: "testUser", password: "testPassword",