From d7d63dd245fc808a6a3a79eaabfd6f9735fe265a Mon Sep 17 00:00:00 2001 From: Raymond Debasa Peralta Date: Mon, 29 Apr 2024 03:40:17 +0200 Subject: [PATCH 1/2] game test v2 --- webapp/src/components/Game.js | 7 +++- webapp/src/components/Game.test.js | 59 ++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/webapp/src/components/Game.js b/webapp/src/components/Game.js index 05a1f8c6..feab3620 100644 --- a/webapp/src/components/Game.js +++ b/webapp/src/components/Game.js @@ -59,6 +59,9 @@ const Game = () => { const [openDialog, setOpenDialog] = useState(false); + const [gameData, setGameData] = useState(null); // Variable local para almacenar los datos de la partida + + const handleDialogOpen = () => { setIsTimerActive(false); setOpenDialog(true); @@ -319,10 +322,10 @@ const getQuestions = () => { //console.log("Número Total de Preguntas:", newGame.totalQuestions); //console.log("Número de Respuestas Correctas:", newGame.correctAnswers); //console.log("Número de Respuestas Incorrectas:", newGame.incorrectAnswers); - + setGameData(newGame); - axios.post(`${apiEndpoint}/addgame`, newGame) + axios.post(`${apiEndpoint}/addgame`, gameData) .then(response => { console.log("Respuesta del servidor:", response.data); }) diff --git a/webapp/src/components/Game.test.js b/webapp/src/components/Game.test.js index 536032c7..28a55bdf 100644 --- a/webapp/src/components/Game.test.js +++ b/webapp/src/components/Game.test.js @@ -3,7 +3,7 @@ import { render, fireEvent, screen, waitFor } from '@testing-library/react'; import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import Game from './Game'; -import { MemoryRouter } from 'react-router-dom'; +import { MemoryRouter } from 'react-router-dom'; // Importa MemoryRouter const mockAxios = new MockAdapter(axios); @@ -25,6 +25,20 @@ describe('Game component', () => { beforeEach(() => { mockAxios.reset(); jest.useFakeTimers(); + jest.clearAllMocks(); + localStorage.clear(); + }); + + it('should display initial elements correctly', () => { + renderGameComponent(); + + // Verificar que elementos iniciales se muestren correctamente + expect(screen.getByText('Saber y Ganar Juego')).toBeInTheDocument(); + expect(screen.getByText('Preguntas restantes: 5')).toBeInTheDocument(); // Depende de la configuración del juego + expect(screen.getByText('Correctas: 0')).toBeInTheDocument(); + expect(screen.getByText('Incorrectas: 0')).toBeInTheDocument(); + expect(screen.getByText('Pregunta 0:')).toBeInTheDocument(); // La primera pregunta + expect(screen.getByText('Volver al menú principal')).toBeInTheDocument(); }); it('should display question and answer options', async () => { @@ -45,6 +59,7 @@ describe('Game component', () => { } }); }); + it('should handle correct answer click', async () => { renderGameComponent(); @@ -56,14 +71,11 @@ describe('Game component', () => { // Una vez que se muestre la pregunta, hacer clic en la respuesta correcta fireEvent.click(screen.getByText('4')); // Verificar si afectó correctamente al conteo de respuestas correctas - - console.log(screen.getByText(/Correctas: 1/i).textContent); expect(screen.getByText(/Correctas: 1/i)).toBeInTheDocument(); - + // Verificar si el color del botón cambió a verde + expect(screen.getByText('4')).toHaveStyle({ backgroundColor: 'rgba(79, 141, 18, 0.726)' }); } }); - - }); it('should handle incorrect answer click', async () => { @@ -76,13 +88,11 @@ describe('Game component', () => { if (screen.queryByText(/Pregunta 1:/i)) { // Una vez que se muestre la pregunta, hacer clic en una respuesta incorrecta fireEvent.click(screen.getByText('2')); - - console.log(screen.getByText(/Incorrectas: 1/i).textContent); expect(screen.getByText(/Incorrectas: 1/i)).toBeInTheDocument(); + // Verificar si el color del botón cambió a rojo + expect(screen.getByText('2')).toHaveStyle({ backgroundColor: 'rgba(208, 22, 22, 0.952)' }); } }); - - }); it('should close confirmation dialog on cancel', async () => { @@ -113,12 +123,11 @@ describe('Game component', () => { await waitFor(() => { expect(screen.queryByText('Game')).not.toBeInTheDocument(); }); - }); it('should display end game message and enable "Back to Main Menu" button when game is finished', async () => { renderGameComponent(); - + for (let i = 1; i <= 5; i++) { mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData }); @@ -142,6 +151,32 @@ describe('Game component', () => { expect(screen.getByText(/Volver al menú principal/i)).toBeInTheDocument(); } }); + + + }); + + + + + it('should disable buttons when time runs out', async () => { + renderGameComponent(); + + mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData }); + + // Esperar hasta que se muestre "Pregunta 1" + await waitFor(() => { + if (screen.queryByText(/Pregunta 1:/i)) { + jest.advanceTimersByTime(10000); // Avanzar el temporizador por 10 segundos + + // Verificar que los botones estén deshabilitados + const buttons = screen.getAllByRole('button', { name: /answer/i }); + buttons.forEach(button => { + expect(button).toBeDisabled(); + }); + } + }); + }); + }); From 62cdf58596c88d48ada839580dc8f9a6838f1c14 Mon Sep 17 00:00:00 2001 From: Raymond Debasa Peralta Date: Mon, 29 Apr 2024 04:17:02 +0200 Subject: [PATCH 2/2] game test v3 --- webapp/src/components/Game.test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/webapp/src/components/Game.test.js b/webapp/src/components/Game.test.js index 28a55bdf..5c8d04ed 100644 --- a/webapp/src/components/Game.test.js +++ b/webapp/src/components/Game.test.js @@ -178,5 +178,31 @@ describe('Game component', () => { }); }); + + it('should handle timeout correctly', async () => { + // Define una función simulada para handleShowQuestion + const handleShowQuestion = jest.fn(); + + renderGameComponent({ handleShowQuestion }); // Pasa la función simulada como propiedad + + await waitFor(() => { + mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData }); + }); + + await waitFor(() => { + if (screen.queryByText(/Pregunta 1:/i)) { + jest.advanceTimersByTime(10000); + expect(screen.getByText(correctOption)).toHaveStyle({ backgroundColor: 'rgba(79, 141, 18, 0.726)' }); + expect(incrementIncorrect).toHaveBeenCalledTimes(1); + expect(decrementQuestionsToAnswer).toHaveBeenCalledTimes(1); + } + }); + + await waitFor(() => { + expect(handleShowQuestion).toHaveBeenCalledTimes(0); // Verifica que handleShowQuestion se llame una vez + }); + }); + + });