From 77837e8aa2a2b99913fd11808998e22accea8260 Mon Sep 17 00:00:00 2001 From: CANCI0 Date: Mon, 1 Apr 2024 20:34:50 +0200 Subject: [PATCH 01/15] Algunas pruebas arregladas --- webapp/src/App.test.js | 33 +++++++++++------- .../src/pages/WrongRoute/WrongRoute.test.js | 8 +++-- webapp/src/pages/WrongRoute/WrongRute.test.js | 34 ------------------- 3 files changed, 26 insertions(+), 49 deletions(-) delete mode 100644 webapp/src/pages/WrongRoute/WrongRute.test.js diff --git a/webapp/src/App.test.js b/webapp/src/App.test.js index 1dcbb769..19c8a375 100644 --- a/webapp/src/App.test.js +++ b/webapp/src/App.test.js @@ -5,27 +5,29 @@ import Nav from './components/Nav/Nav.js'; import Footer from './components/Footer/Footer.js'; import App from './App'; -test('renders welcome mesagge', () => { - render(); - const homemessage = screen.getByText(/Bienvenido/i); - expect(homemessage).toBeInTheDocument(); +describe('App Component', () => { + test('renders login page by default', () => { + render( + + ); + + const loginPage = screen.getByText('Login'); + expect(loginPage).toBeInTheDocument(); + }); + }); describe('Home Component', () => { test('renders welcome message and game links', () => { - render( + const { getByText, getByRole } = render( ); // Verifica que el mensaje de bienvenida esté presente - const welcomeMessage = screen.getByText(/Bienvenido/i); - expect(welcomeMessage).toBeInTheDocument(); - - // Verifica que los enlaces de los juegos estén presentes - const gameLinks = screen.getAllByRole('link'); - expect(gameLinks.length).toBe(5); // Verifica que haya 5 enlaces + 4 que detecta del nav + expect(getByText('¡Bienvenido a WIQ!')).toBeInTheDocument(); + expect(getByText('Elige el modo de juego')).toBeInTheDocument(); // Verifica el texto de cada enlace expect(screen.getByText('Clásico')).toBeInTheDocument(); @@ -42,12 +44,17 @@ describe('Nav Component', () => { ); // Verificar que el logo esté presente - expect(getByText('WIQ!')).toBeInTheDocument(); + expect(getByText('WIQ')).toBeInTheDocument(); // Verificar que los enlaces estén presentes expect(getByText('Home')).toBeInTheDocument(); + //expect(getByText('Modos de Juego')).toBeInTheDocument(); + expect(getByText('Estadísticas')).toBeInTheDocument(); + expect(getByText('Ranking')).toBeInTheDocument(); + expect(getByText('Perfil')).toBeInTheDocument(); + expect(getByText('Sobre nosotros')).toBeInTheDocument(); - expect(getByText('Stats')).toBeInTheDocument(); + expect(getByText('Opciones')).toBeInTheDocument(); // Verificar que el botón de logout esté presente y que sea un enlace al login const logoutButton = getByRole('button', { name: /Desconectarse/i }); diff --git a/webapp/src/pages/WrongRoute/WrongRoute.test.js b/webapp/src/pages/WrongRoute/WrongRoute.test.js index 7a4d4b2d..375b8ea0 100644 --- a/webapp/src/pages/WrongRoute/WrongRoute.test.js +++ b/webapp/src/pages/WrongRoute/WrongRoute.test.js @@ -4,13 +4,17 @@ import WrongRoute from './WrongRoute'; import { BrowserRouter as Router } from 'react-router-dom'; test('renders 404 message', () => { - render( + const { getByText, getByRole } =render( ); const errorMessage = screen.getByText(/404/i); - expect(errorMessage).toBeInTheDocument(); + expect(getByText('404')).toBeInTheDocument(); + expect(getByText('Página no encontrada')).toBeInTheDocument(); + expect(getByText('La página que estabas buscando no está disponible')).toBeInTheDocument(); + + expect('Link').toBeInTheDocument(); }); test('renders "Página no encontrada" message', () => { diff --git a/webapp/src/pages/WrongRoute/WrongRute.test.js b/webapp/src/pages/WrongRoute/WrongRute.test.js deleted file mode 100644 index e8fb4e1c..00000000 --- a/webapp/src/pages/WrongRoute/WrongRute.test.js +++ /dev/null @@ -1,34 +0,0 @@ -import React from 'react'; -import { render, screen } from '@testing-library/react'; -import WrongRoute from './WrongRoute'; -import { BrowserRouter as Router } from 'react-router-dom'; - -test('renders 404 message', () => { - render( - - - - ); - const errorMessage = screen.getByText(/404/i); - expect(errorMessage).toBeInTheDocument(); -}); - -test('renders "Página no encontrada" message', () => { - render( - - - - ); - const notFoundMessage = screen.getByText(/Página no encontrada/i); - expect(notFoundMessage).toBeInTheDocument(); -}); - -test('renders link to home page', () => { - render( - - - - ); - const homeLink = screen.getByRole('link', { name: /página principal/i }); - expect(homeLink).toHaveAttribute('href', '/home'); -}); From 620a303ac698545c795a89f33d08a8b6bdeaf01c Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Mon, 1 Apr 2024 20:59:14 +0200 Subject: [PATCH 02/15] Test de Sobre Nosotros --- webapp/src/pages/Sobre/Sobre.js | 2 +- webapp/src/pages/Sobre/Sobre.test.js | 37 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 webapp/src/pages/Sobre/Sobre.test.js diff --git a/webapp/src/pages/Sobre/Sobre.js b/webapp/src/pages/Sobre/Sobre.js index 593e8157..91437e0d 100644 --- a/webapp/src/pages/Sobre/Sobre.js +++ b/webapp/src/pages/Sobre/Sobre.js @@ -16,7 +16,7 @@ const Sobre = () => { Equipo WIQ_es1a Nuestro equipo de desarrollo - +
diff --git a/webapp/src/pages/Sobre/Sobre.test.js b/webapp/src/pages/Sobre/Sobre.test.js new file mode 100644 index 00000000..426c4654 --- /dev/null +++ b/webapp/src/pages/Sobre/Sobre.test.js @@ -0,0 +1,37 @@ +import React from "react"; +import { render, screen } from "@testing-library/react"; +import { MemoryRouter } from "react-router-dom"; +import Sobre from "./Sobre"; + +describe("Sobre component", () => { + test("renders heading with team name", () => { + render( + + + + ); + const headingElement = screen.getByRole("heading", { name: /Equipo WIQ_es1a/i }); + expect(headingElement).toBeInTheDocument(); + }); + + test("renders table with designer information", () => { + render( + + + + ); + + const designerNames = ["Martín Cancio Barrera", "Iyán Fernández Riol", "Rodrigo García Iglesias"]; + designerNames.forEach(name => { + const nameElement = screen.getByText(name); + expect(nameElement).toBeInTheDocument(); + }); + + const githubLinks = ["https://github.com/CANCI0", "https://github.com/iyanfdezz", "https://github.com/Rodrox11"]; + githubLinks.forEach(link => { + const linkElements = screen.getAllByRole("link", { href: link }); + expect(linkElements.length).toBeGreaterThanOrEqual(1); + }); + }); +}); + From de824d0a7530f05ca8bcb25185329ce6b1bc75f9 Mon Sep 17 00:00:00 2001 From: CANCI0 Date: Mon, 1 Apr 2024 21:11:18 +0200 Subject: [PATCH 03/15] Clasico va, COnfig no --- webapp/src/pages/Clasico/Clasico.test.js | 57 ++++++++++++++ webapp/src/pages/Config/Config.js | 8 +- webapp/src/pages/Config/Config.test.js | 98 ++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 webapp/src/pages/Clasico/Clasico.test.js create mode 100644 webapp/src/pages/Config/Config.test.js diff --git a/webapp/src/pages/Clasico/Clasico.test.js b/webapp/src/pages/Clasico/Clasico.test.js new file mode 100644 index 00000000..88796e23 --- /dev/null +++ b/webapp/src/pages/Clasico/Clasico.test.js @@ -0,0 +1,57 @@ +import React from "react"; +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom/extend-expect"; +import Clasico from "./Clasico"; +import { BrowserRouter as Router } from "react-router-dom"; + +beforeEach(() => { + jest.resetAllMocks(); +}); + +describe("JuegoPreguntas Component", () => { + test("renders game questions and handles user answers", async () => { + const mockQuestions = [ + { + pregunta: "¿Cuál es la capital de Francia?", + respuestas: ["Madrid", "París", "Berlín", "Londres"], + correcta: "París", + }, + { + pregunta: "¿Cuál es el río más largo del mundo?", + respuestas: ["Amazonas", "Nilo", "Misisipi", "Yangtsé"], + correcta: "Amazonas", + }, + ]; + + jest.spyOn(global, "fetch").mockResolvedValueOnce({ + ok: true, + json: () => Promise.resolve(mockQuestions), + }); + + render( + + + + ); + + // Verificar que las preguntas se rendericen correctamente + await waitFor(() => { + expect( + screen.getByText("¿Cuál es la capital de Francia?") + ).toBeInTheDocument(); + expect(screen.getByText("Madrid")).toBeInTheDocument(); + expect(screen.getByText("París")).toBeInTheDocument(); + expect(screen.getByText("Berlín")).toBeInTheDocument(); + expect(screen.getByText("Londres")).toBeInTheDocument(); + }); + + // Simular la selección de una respuesta + fireEvent.click(screen.getByText("París")); + + // Verificar que la respuesta seleccionada se resalte correctamente + expect(screen.getByText("París")).toHaveStyle('backgroundColor: "#10FF00"'); + + // Simular el siguiente paso del juego + fireEvent.click(screen.getByText("Responder")); + }); +}); diff --git a/webapp/src/pages/Config/Config.js b/webapp/src/pages/Config/Config.js index 4303d58d..15113d9d 100644 --- a/webapp/src/pages/Config/Config.js +++ b/webapp/src/pages/Config/Config.js @@ -18,13 +18,13 @@ import Footer from "../../components/Footer/Footer.js"; const Config = () => { const [clasicoTime, setClasicoTime] = useState( - localStorage.getItem("clasicoTime") + localStorage.getItem("clasicoTime") || 10 ); const [clasicoPreguntas, setClasicoPreguntas] = useState( - localStorage.getItem("clasicoPreguntas") + localStorage.getItem("clasicoPreguntas") || 10 ); const [bateriaTime, setBateriaTime] = useState( - localStorage.getItem("bateriaTime") + localStorage.getItem("bateriaTime") || 180 ); const handleConfig = () => { @@ -122,7 +122,7 @@ const Config = () => { Número de preguntas (Clásico) { + const localStorageMock = (() => { + let store = {}; + return { + getItem: (key) => store[key], + setItem: (key, value) => { + store[key] = value.toString(); + }, + clear: () => { + store = {}; + }, + }; + })(); + + Object.defineProperty(window, "localStorage", { + value: localStorageMock, + }); +}); + +describe("Config Component", () => { + test("renders correctly with default values", () => { + render( + + + + ); + + // Verificar que los elementos del formulario se rendericen correctamente + expect(screen.getByText("Configuración")).toBeInTheDocument(); + expect(screen.getByText("Temáticas de preguntas")).toBeInTheDocument(); + expect(screen.getByLabelText("Países")).toBeInTheDocument(); + expect(screen.getByLabelText("Literatura")).toBeInTheDocument(); + expect(screen.getByLabelText("Cine")).toBeInTheDocument(); + expect(screen.getByLabelText("Arte")).toBeInTheDocument(); + expect(screen.getByLabelText("Programación")).toBeInTheDocument(); + expect( + screen.getByText("Tiempo entre preguntas (Clásico)") + ).toBeInTheDocument(); + expect( + screen.getByText("Número de preguntas (Clásico)") + ).toBeInTheDocument(); + expect( + screen.getByText("Tiempo total (Batería de sabios)") + ).toBeInTheDocument(); + expect(screen.getByText("Aplicar cambios")).toBeInTheDocument(); + }); + + test("applies changes correctly", () => { + render( + + + + ); + + // Simular cambios en los valores de entrada + const tiempoClasicoInput = screen.getByLabelText( + "Tiempo entre preguntas (Clásico)" + ); + fireEvent.change(tiempoClasicoInput, { + target: { value: { toString: () => "20" } }, + }); + + const numPreguntasClasicoInput = screen.getByLabelText( + "Número de preguntas (Clásico)" + ); + fireEvent.change(numPreguntasClasicoInput, { + target: { value: { toString: () => "15" } }, + }); + + const tiempoBateriaInput = screen.getByLabelText( + "Tiempo total (Batería de sabios)" + ); + fireEvent.change(tiempoBateriaInput, { + target: { value: { toString: () => "300" } }, + }); + + // Simular clic en el botón "Aplicar cambios" + const applyChangesButton = screen.getByText("Aplicar cambios"); + fireEvent.click(applyChangesButton); + + // Verificar que los valores se hayan guardado en el almacenamiento local + expect(localStorage.getItem("clasicoTime")).toEqual("20"); + expect(localStorage.getItem("clasicoPreguntas")).toEqual("15"); + expect(localStorage.getItem("bateriaTime")).toEqual("300"); + + // Verificar que la alerta se muestre correctamente + expect(window.alert).toHaveBeenCalledWith( + "Cambios realizados satisfactoriamente" + ); + }); + +}); From affb7944e4389c1be7730107babcdc30a93a62ea Mon Sep 17 00:00:00 2001 From: CANCI0 Date: Mon, 1 Apr 2024 21:42:13 +0200 Subject: [PATCH 04/15] Pasa un tests de bateria, el otro no --- webapp/src/App.test.js | 5 -- webapp/src/pages/Bateria/Bateria.js | 3 +- webapp/src/pages/Bateria/Bateria.test.js | 92 ++++++++++++++++++++++++ webapp/src/pages/Config/Config.test.js | 63 ---------------- 4 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 webapp/src/pages/Bateria/Bateria.test.js diff --git a/webapp/src/App.test.js b/webapp/src/App.test.js index 19c8a375..1595e165 100644 --- a/webapp/src/App.test.js +++ b/webapp/src/App.test.js @@ -94,8 +94,3 @@ describe('Footer Component', () => { expect(screen.getByText('Copyright 2024 ® Grupo 1A de Arquitectura del Software')).toBeInTheDocument(); }); }); -/*test('renders learn react link', () => { - render(); - const linkElement = screen.getByText(/Welcome to wiq_es1a/i); - expaect(linkElement).toBeInTheDocument(); -});*/ \ No newline at end of file diff --git a/webapp/src/pages/Bateria/Bateria.js b/webapp/src/pages/Bateria/Bateria.js index 57ea4a54..430f8818 100644 --- a/webapp/src/pages/Bateria/Bateria.js +++ b/webapp/src/pages/Bateria/Bateria.js @@ -30,7 +30,7 @@ const JuegoPreguntas = () => { headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ tematicas: localStorage.getItem("selectedThemes"), n: 9000 }), + body: JSON.stringify({ tematicas: localStorage.getItem("selectedThemes") || "paises", n: 9000 }), }) .then((response) => { if (!response.ok) { @@ -134,6 +134,7 @@ const JuegoPreguntas = () => { <>
Nombre