From d9bea9c21940ecc60ddc7c681decc9fb471f1c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Fern=C3=A1ndez?= Date: Sat, 6 Apr 2024 21:16:18 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20prueba=20saveStatistics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/components/QuizGame.test.js | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/webapp/src/components/QuizGame.test.js b/webapp/src/components/QuizGame.test.js index 74854fba..61831ab9 100644 --- a/webapp/src/components/QuizGame.test.js +++ b/webapp/src/components/QuizGame.test.js @@ -46,4 +46,32 @@ test('Pregunta incorrecta', async () => { // Wait for the state updates to propagate await waitFor(() => expect(screen.queryByText(/Loading questions.../i)).not.toBeInTheDocument()); +}); + +test('Final del juego y llamada a saveStatistics', async () => { + mockAxios.onGet('http://localhost:8000/generate-question').reply(200, { + question: "pregunta", + correctAnswer: "correcta", + allAnswers: ["correcta", "no1", "no2", "no3"] + }); + + // Intercepta la llamada a saveStatistics + mockAxios.onPost('http://localhost:8000/save-statistics').reply(config => { + // Verifica que saveStatistics se haya llamado con los datos correctos + const { username, rightAnswers, wrongAnswers, time } = JSON.parse(config.data); + expect(username).toBe('username'); + expect(rightAnswers).toBeGreaterThanOrEqual(0); + expect(wrongAnswers).toBeGreaterThanOrEqual(0); + expect(time).toBeGreaterThanOrEqual(0); + return [200]; + }); + + render(); + expect(screen.getByText(/Loading questions.../i)).toBeInTheDocument(); + + // Simula la respuesta a todas las preguntas + while (screen.queryByRole('button', { name: /correcta/i })) { + const answerButton = await screen.findByRole('button', { name: /correcta/i }); + fireEvent.click(answerButton); + } }); \ No newline at end of file