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