From c40f5a781322a2fed0f716b11bd83502770b38fa Mon Sep 17 00:00:00 2001 From: Laura Cordero Date: Fri, 26 Apr 2024 10:18:58 +0200 Subject: [PATCH] Pruebas Game Test --- webapp/src/components/Game.test.js | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 webapp/src/components/Game.test.js diff --git a/webapp/src/components/Game.test.js b/webapp/src/components/Game.test.js new file mode 100644 index 00000000..8727a6fc --- /dev/null +++ b/webapp/src/components/Game.test.js @@ -0,0 +1,46 @@ +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; +import axios from 'axios'; +import MockAdapter from 'axios-mock-adapter'; +import Game from './Game'; +import { BrowserRouter} from 'react-router-dom'; + +describe('Game', () => { + let mockAxios; + + beforeEach(() => { + mockAxios = new MockAdapter(axios); + }); + + afterEach(() => { + mockAxios.reset(); + }); + + it('should render the loading text initially', () => { + render( + + + ); + expect(screen.getByText('Cargando...')).toBeInTheDocument(); + }); + + it('should render the question and answer options after loading', async () => { + const mockResponse = { + pregunta: 'What is the capital of France?', + correcta: 'Paris', + incorrectas: ['London', 'Berlin', 'Madrid'], + }; + mockAxios.onPost('http://localhost:8000/questions').reply(200, mockResponse); + + render( + + + ); + await waitFor(() => expect(screen.queryByText('Cargando...')).not.toBeInTheDocument()); + + await screen.findByText('What is the capital of France?'); + expect(screen.getByText('Paris')).toBeInTheDocument(); + expect(screen.getByText('London')).toBeInTheDocument(); + expect(screen.getByText('Berlin')).toBeInTheDocument(); + expect(screen.getByText('Madrid')).toBeInTheDocument(); + }); +}); \ No newline at end of file