From 0acd688d045d048fb9b4ad5d591ac00e6125c8c8 Mon Sep 17 00:00:00 2001 From: CarolinaUniovi Date: Mon, 29 Apr 2024 23:05:25 +0200 Subject: [PATCH] playingGame tests --- .../src/components/game/PlayingGame.test.js | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 webapp/src/components/game/PlayingGame.test.js diff --git a/webapp/src/components/game/PlayingGame.test.js b/webapp/src/components/game/PlayingGame.test.js new file mode 100644 index 0000000..f6fc308 --- /dev/null +++ b/webapp/src/components/game/PlayingGame.test.js @@ -0,0 +1,72 @@ +import React from 'react'; +import { render, fireEvent, waitFor } from '@testing-library/react'; +import PlayingGame from './PlayingGame'; + +jest.useFakeTimers(); + +describe('PlayingGame component', () => { + it('should handle answering questions and end game correctly', async () => { + const questions = [ + { uuid: '1', + question: 'What is the capital of France?', + correctAnswer: '1', + incorrectAnswer1: '2', + incorrectAnswer2: '3', + incorrectAnswer3: '4', }, + { uuid: '2', + question: 'What is the capital of Italy?', + correctAnswer: '1', + incorrectAnswer1: '2', + incorrectAnswer2: '3', + incorrectAnswer3: '4', }, + { uuid: '2', + question: 'What is the capital of Spain?', + correctAnswer: '1', + incorrectAnswer1: '2', + incorrectAnswer2: '3', + incorrectAnswer3: '4', }, + ]; + + const setCurrentStageMock = jest.fn(); + const { getByTestId, queryByTestId } = render( + + ); + + // Ensure initial rendering + expect(getByTestId('question-container')).toBeInTheDocument(); + expect(getByTestId('question-title')).toBeInTheDocument(); + expect(getByTestId('question')).toBeInTheDocument(); + expect(getByTestId('seconds')).toBeInTheDocument(); + + // Simulate answering question 1 + fireEvent.click(getByTestId('answer-1')); + jest.advanceTimersByTime(10000); + await waitFor(() => { + expect(getByTestId('question-title')).toBeInTheDocument(); + expect(getByTestId('question')).toBeInTheDocument(); + expect(getByTestId('seconds')).toBeInTheDocument(); + }); + + // Simulate answering question 2 + fireEvent.click(getByTestId('answer-2')); + jest.advanceTimersByTime(10000); + await waitFor(() => { + expect(getByTestId('question')).toBeInTheDocument(); + expect(getByTestId('seconds')).toBeInTheDocument(); + }); + + // Simulate answering question 3 + fireEvent.click(getByTestId('answer-3')); + jest.advanceTimersByTime(10000); + await waitFor(() => { + expect(queryByTestId('question-container')).toBeNull(); + expect(getByTestId('result')).toBeInTheDocument(); + expect(getByTestId('points')).toBeInTheDocument(); + }); + }); + + // Add more test cases for edge cases and multiplayer scenarios if applicable +}); \ No newline at end of file