Skip to content

Commit

Permalink
Another game test
Browse files Browse the repository at this point in the history
  • Loading branch information
lauracc97 committed Apr 26, 2024
1 parent 0a02c1a commit f545b7d
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions webapp/src/components/Game.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen,cleanup } from '@testing-library/react';
import { render, screen,cleanup, waitFor, fireEvent } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import Game from './Game';
Expand Down Expand Up @@ -29,21 +29,49 @@ describe('Game', () => {

it('should mock the question and show it on the screen', async () => {
const mockResponse = {
pregunta: 'What is the capital of France?',
correcta: 'Paris',
incorrectas: ['London', 'Berlin', 'Madrid'],
pregunta: '¿Cual es la capital de Italia?',
correcta: 'Roma',
incorrectas: ['Seul', 'Berlin', 'Madrid'],
};

mockAxios.onPost('http://localhost:8000/questions').reply(200, mockResponse);

render(
<BrowserRouter>
<Game />
</BrowserRouter>);

await screen.findByText('What is the capital of France?');
expect(screen.getByText('Paris')).toBeInTheDocument();
expect(screen.getByText('London')).toBeInTheDocument();
await screen.findByText('¿Cual es la capital de Italia?');
expect(screen.getByText('Roma')).toBeInTheDocument();
expect(screen.getByText('Seul')).toBeInTheDocument();
expect(screen.getByText('Berlin')).toBeInTheDocument();
expect(screen.getByText('Madrid')).toBeInTheDocument();
});

it('should select the correct answer and add 1 to the total', async () => {

const mockResponse = {
pregunta: '¿Cual es la capital de Italia?',
correcta: 'Roma',
incorrectas: ['Seul', 'Berlin', 'Madrid'],
};

mockAxios.onPost(`http://localhost:8000/questions`).reply(200, mockResponse);

const { getByText } = render(
<BrowserRouter>
<Game />
</BrowserRouter>);

// Esperar a que se cargue la pregunta
await waitFor(() => expect(getByText(mockResponse.pregunta)).toBeInTheDocument());

// Seleccionamos la respuesta correcta
const correctOption = screen.getAllByRole('radio').find(option => option.parentElement.getAttribute('data-iscorrect') === 'true');
fireEvent.click(correctOption);

// Verificar que se incrementa la puntuación
expect(getByText('Preguntas acertadas: 1')).toBeInTheDocument();
});

});

0 comments on commit f545b7d

Please sign in to comment.