Skip to content

Commit

Permalink
Tests añadidos
Browse files Browse the repository at this point in the history
  • Loading branch information
CANCI0 committed Apr 11, 2024
1 parent 0d599b2 commit 52c0a71
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 20 deletions.
2 changes: 1 addition & 1 deletion webapp/src/components/Profile/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Perfil = (username) => {
{t('components.profile.recentGames')}
</Heading>
<Box overflowX={"scroll"} width={'100%'}>
{userData.games.length > 0 ? (
{ userData.games && userData.games.length > 0 ? (
<Table variant="simple">
<Thead>
<Tr>
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"name": "Nombre de usuario",
"dateLabel": "Fecha de creación de la cuenta:",
"ngame": "Partida",
"error": "Ha habido un error al intentar cargar las preguntas. Inténtalo más tarde"
"error": "Ha habido un error al intentar cargar el perfil del usuario. Inténtalo más tarde"
},
"ranking": {
"loading": "Cargando ...",
Expand Down
24 changes: 14 additions & 10 deletions webapp/src/pages/Calculadora/Calculadora.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("renders the game screen", () => {
expect(screen.getByText(/puntuación/i)).toBeInTheDocument();
});

test("handles correct answer", () => {
test("handles correct answer", async () => {
render(
<I18nextProvider i18n={i18n}>
<MemoryRouter>
Expand Down Expand Up @@ -78,6 +78,10 @@ test("handles correct answer", () => {
.trim()
);
expect(updatedScore).toBe(initialScore + 2);

await setTimeout(() => {

}, 1000);
});

test("handles incorrect answer", () => {
Expand All @@ -102,7 +106,7 @@ test("handles incorrect answer", () => {
const submitButton = screen.getByRole("button", { name: /enviar/i });

// Enter an incorrect answer and submit
fireEvent.change(inputField, { target: { value: 0 } });
fireEvent.change(inputField, { target: { value: "a" } });
fireEvent.click(submitButton);

// Check if the score remains the same
Expand All @@ -115,7 +119,7 @@ test("handles incorrect answer", () => {
expect(updatedScore).toBe(initialScore);
});

test("handles game over", () => {
test("handles game over", async () => {
render(
<I18nextProvider i18n={i18n}>
<MemoryRouter>
Expand All @@ -131,7 +135,7 @@ test("handles game over", () => {
.textContent.split(":")[1]
.trim()
);

// Get the input field and submit button
const inputField = screen.getByTitle(/number/i);
const submitButton = screen.getByRole("button", { name: /enviar/i });
Expand All @@ -153,10 +157,10 @@ test("handles game over", () => {
.textContent.split(":")[1]
.trim()
);

screen.getByText(/jugar de nuevo/i).click();

waitFor(() => {
expect(screen.getByText(/¿/i)).toBeInTheDocument();
});
expect(updatedScore).toBe(initialScore);

await setTimeout(() => {
screen.getByText(/jugar de nuevo/i).click();
}, 1000);

});
2 changes: 1 addition & 1 deletion webapp/src/pages/History/History.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Divider,
useColorMode,
} from "@chakra-ui/react";
import React, { useEffect, useState, getMemo } from "react";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import Nav from "../../components/Nav/Nav";
import Footer from "../../components/Footer/Footer";
Expand Down
79 changes: 79 additions & 0 deletions webapp/src/pages/History/History.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { render, screen, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import History from "./History";
import { I18nextProvider } from "react-i18next";
import i18n from "../../i18n.js";

describe("History Component", () => {
test("renders user profile data after loading", async () => {
const mockUserData = {
username: "testUser",
createdAt: new Date(),
games: [
{
_id: "game1",
questions: [
{
pregunta: "Question 1",
respuestas: ["Answer 1", "Answer 2"],
correcta: "Answer 1",
respuesta: "Answer 2",
},
{
pregunta: "Question 2",
respuestas: ["Answer 3", "Answer 4"],
correcta: "Answer 3",
respuesta: "Answer 3",
},
],
},
],
};

jest.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockUserData),
});

render(
<I18nextProvider i18n={i18n}>
<MemoryRouter>
<History />
</MemoryRouter>
</I18nextProvider>
);

await waitFor(() => {
expect(screen.getByText("testUser")).toBeInTheDocument();
expect(
screen.getByText(/Fecha de creación de la cuenta/i)
).toBeInTheDocument();
expect(screen.getByText("Question 1")).toBeInTheDocument();
expect(screen.getByText("Question 2")).toBeInTheDocument();
expect(screen.getByText("Answer 1")).toBeInTheDocument();
expect(screen.getByText("Answer 2")).toBeInTheDocument();
expect(screen.getByText("Answer 3")).toBeInTheDocument();
expect(screen.getByText("Answer 4")).toBeInTheDocument();
});
});

test("displays an error message when profile loading fails", async () => {
jest
.spyOn(global, "fetch")
.mockRejectedValueOnce(new Error("Failed to fetch"));

render(
<I18nextProvider i18n={i18n}>
<MemoryRouter>
<History />
</MemoryRouter>
</I18nextProvider>
);

await waitFor(() => {
expect(
screen.getByText("Ha habido un error al intentar cargar el perfil del usuario. Inténtalo más tarde")
).toBeInTheDocument();
});
});
});
17 changes: 10 additions & 7 deletions webapp/src/pages/Perfil/Perfil.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Perfil Component", () => {
],
};

jest.spyOn(global, "fetch").mockResolvedValueOnce({
jest.spyOn(global, "fetch").mockResolvedValue({
json: jest.fn().mockResolvedValueOnce(mockUserData),
});

Expand All @@ -52,9 +52,10 @@ describe("Perfil Component", () => {
});

test("displays an error message when profile loading fails", async () => {
jest
.spyOn(global, "fetch")
.mockRejectedValueOnce(new Error("Failed to fetch"));
jest.spyOn(global, "fetch").mockRejectedValueOnce({
ok: false,
json: () => Promise.resolve(new Error("Error")),
});

render(
<I18nextProvider i18n={i18n}>
Expand All @@ -65,7 +66,7 @@ describe("Perfil Component", () => {
);

await waitFor(() => {
expect(screen.getByText("Error: Failed to fetch")).toBeInTheDocument();
expect(screen.getByText("Ha habido un error al intentar cargar la información del usuario. Inténtalo más tarde.")).toBeInTheDocument();
});
});

Expand All @@ -77,7 +78,8 @@ describe("Perfil Component", () => {
};

jest.spyOn(global, "fetch").mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce(mockUserData),
ok: true,
json: () => Promise.resolve(mockUserData),
});

render(
Expand All @@ -104,7 +106,8 @@ test("displays recent calculator game data", async () => {
};

jest.spyOn(global, "fetch").mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce(mockUserData),
ok: true,
json: () => Promise.resolve(mockUserData),
});

render(
Expand Down

0 comments on commit 52c0a71

Please sign in to comment.