Skip to content

Commit

Permalink
Quitado código duplicado en los test de register
Browse files Browse the repository at this point in the history
  • Loading branch information
CANCI0 committed Apr 2, 2024
1 parent ea55fba commit 5fbd4c4
Showing 1 changed file with 28 additions and 56 deletions.
84 changes: 28 additions & 56 deletions webapp/src/components/Register/Register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,42 @@ 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(
<Router>
<Register />
</Router>
);

// 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(
Expand All @@ -38,40 +52,17 @@ describe("Register Component", () => {
</Router>
);

// 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",
});
});

test("handles user registration failure", async () => {
const errorMessage = "Error al registrar usuario";
const mockErrorResponse = { response: { data: { error: errorMessage } } };
axios.post.mockRejectedValueOnce(mockErrorResponse);

render(
Expand All @@ -80,29 +71,10 @@ describe("Register Component", () => {
</Router>
);

// 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",
Expand Down

0 comments on commit 5fbd4c4

Please sign in to comment.