From 8d56b1d028053dee83f5da7df1d29afab0fa35a9 Mon Sep 17 00:00:00 2001 From: Marco Quintana Date: Sun, 7 Apr 2024 12:53:59 +0200 Subject: [PATCH 1/2] Added storeQuestion tests --- webapp/src/storeQuestion/App.jsx | 7 +- webapp/src/storeQuestion/test/App.test.jsx | 100 +++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 webapp/src/storeQuestion/test/App.test.jsx diff --git a/webapp/src/storeQuestion/App.jsx b/webapp/src/storeQuestion/App.jsx index d6d7c7eb..2e1fa609 100644 --- a/webapp/src/storeQuestion/App.jsx +++ b/webapp/src/storeQuestion/App.jsx @@ -18,7 +18,10 @@ function App() { const response = await axios.get(`${apiEndpoint}/history/questions`); setPreguntas(response.data); } catch (error) { - console.error('Error al obtener las preguntas:', error.response.data.error); + if('response' in error && 'data' in error.response && 'error' in error.response.data) + console.error('Error al obtener las preguntas:', error.response.data.error); + else + console.error('Error al obtener las preguntas:', error) } }; @@ -66,7 +69,7 @@ function App() { diff --git a/webapp/src/storeQuestion/test/App.test.jsx b/webapp/src/storeQuestion/test/App.test.jsx new file mode 100644 index 00000000..838b0dcb --- /dev/null +++ b/webapp/src/storeQuestion/test/App.test.jsx @@ -0,0 +1,100 @@ +import userEvent from '@testing-library/user-event'; +import axios from 'axios'; +import App from '../App'; +import {MemoryRouter} from 'react-router-dom'; +import { render, screen, waitFor,} from '@testing-library/react'; + + +jest.mock("axios") +//const mockAxios = new MockAdapter(axios); + +describe('App', () => { + + beforeEach(async() => { + const mockQuestions = Array.from({ length: 20 }, (_, index) => ({ + _id: String(index + 1), + pregunta: `Question ${index + 1}`, + respuesta_correcta: 'Answer 1', + respuestas_incorrectas: ['Answer 2', 'Answer 3', 'Answer 4'], + createdAt: '05/04/2024', + })); + + // Simula la respuesta de la petición GET + axios.get.mockResolvedValueOnce({ data: mockQuestions }); + }); + + test('renders the app title', async() => { + await waitFor( () => render()); + expect(screen.getByText('Almacén de preguntas')).toBeInTheDocument(); + }); + + test('fetches and displays questions', async () => { + const { getByText } = render(); + + await waitFor(() => { + // Verifica que las preguntas se muestren en el componente + expect(getByText('Question 1')).toBeInTheDocument(); + expect(getByText('Question 2')).toBeInTheDocument(); + }); + + // Verifica que se haya llamado axios.get con el endpoint correcto + expect(axios.get).toHaveBeenCalledWith('http://localhost:8000/history/questions'); + }); + + test('navigates to the next page', async () => { + const { getByText } = render(); + + await waitFor(() => { + expect(getByText('Question 1')).toBeInTheDocument(); + expect(getByText('Question 10')).toBeInTheDocument(); + }); + + userEvent.click(getByText('Siguiente')); + + await waitFor(() => { + expect(getByText('Question 11')).toBeInTheDocument(); + expect(getByText('Question 20')).toBeInTheDocument(); + }); + + userEvent.click(getByText('Anterior')); + + await waitFor(() => { + expect(getByText('Question 1')).toBeInTheDocument(); + expect(getByText('Question 10')).toBeInTheDocument(); + }); + + userEvent.click(getByText('Última')); + + await waitFor(() => { + expect(getByText('Question 11')).toBeInTheDocument(); + expect(getByText('Question 20')).toBeInTheDocument(); + }); + + userEvent.click(getByText('Primera')); + + await waitFor(() => { + expect(getByText('Question 1')).toBeInTheDocument(); + expect(getByText('Question 10')).toBeInTheDocument(); + }); + + }); + +}); + +test('handles unknown error when fetching questions', async () => { + const errorMessage = 'Error fetching questions'; + axios.get.mockRejectedValueOnce(new Error(errorMessage)); + + render(); + + expect(axios.get).toHaveBeenCalledWith('http://localhost:8000/history/questions'); +}); + +test('handles error with response.data.error when fetching questions', async () => { + const errorMessage = 'Error fetching questions'; + axios.get.mockRejectedValueOnce({ response: { data: { error: errorMessage } } }); + + render(); + + expect(axios.get).toHaveBeenCalledWith('http://localhost:8000/history/questions'); +}); \ No newline at end of file From 1d925fb4e3dec3264935c7eec09b2296ce790a7f Mon Sep 17 00:00:00 2001 From: Marco Quintana Date: Sun, 7 Apr 2024 14:31:31 +0200 Subject: [PATCH 2/2] Added userStats tests --- webapp/src/userStats/App.jsx | 2 +- webapp/src/userStats/test/App.test.jsx | 124 +++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 webapp/src/userStats/test/App.test.jsx diff --git a/webapp/src/userStats/App.jsx b/webapp/src/userStats/App.jsx index 16f7f8e4..44974733 100644 --- a/webapp/src/userStats/App.jsx +++ b/webapp/src/userStats/App.jsx @@ -72,7 +72,7 @@ function App() {
diff --git a/webapp/src/userStats/test/App.test.jsx b/webapp/src/userStats/test/App.test.jsx new file mode 100644 index 00000000..bc131aa5 --- /dev/null +++ b/webapp/src/userStats/test/App.test.jsx @@ -0,0 +1,124 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import axios from 'axios'; +import App from '../App'; +import {MemoryRouter} from 'react-router-dom'; + +jest.mock('axios'); + +describe('App', () => { + beforeEach(() => { + localStorage.setItem('username', 'testuser'); + }) + + afterEach(() => { + localStorage.removeItem('username'); + }) + + it('should fetch and display games', async () => { + const games = [ + { id: 1, username: 'testuser', points: 100, questions: [{ title: 'Question 1', answers: ['Answer 1', 'Answer 2', 'Answer 3'], ansIndex: [1,1] }], createdAt: "07/04/2024"}, + { id: 2, username: 'testuser', points: 0, questions: [{ title: 'Question 2', answers: ['Answer 1', 'Answer 2', 'Answer 3'], ansIndex: [0,1] }], createdAt: "07/04/2024"} + ]; + + axios.get.mockResolvedValueOnce({ data: games }); + + render(); + + await waitFor(() => { + const elementos = screen.getAllByText('▼'); + + // Haz clic en cada elemento encontrado + elementos.forEach(elemento => { + userEvent.click(elemento); + }); + }) + + await waitFor(() => { + expect(screen.getByText('Historial de testuser')).toBeInTheDocument(); + expect(screen.getByText('Question 1')).toBeInTheDocument(); + expect(screen.getByText('Question 2')).toBeInTheDocument(); + }); + }); + + it('should navigate to next and previous pages', async () => { + const games = Array.from({ length: 20 }, (_, index) => ({ + _id: String(index + 1), + username: 'testuser', + points: 100, + questions: [ + { + title: `Question ${index+1}`, + answers: ['Answer 1', 'Answer 2', 'Answer 3'], + ansIndex: [1,1] + } + ], + createdAt: "07/04/2024" + })); + + axios.get.mockResolvedValueOnce({ data: games }); + + render(); + + await waitFor(() => { + const elementos = screen.getAllByText('▼'); + + // Haz clic en cada elemento encontrado + elementos.forEach(elemento => { + userEvent.click(elemento); + }); + }) + + await waitFor(() => { + expect(screen.getByText('Historial de testuser')).toBeInTheDocument(); + expect(screen.getByText('Question 1')).toBeInTheDocument(); + expect(screen.getByText('Question 10')).toBeInTheDocument(); + }); + + const nextPageButton = screen.getByText('Siguiente'); + const prevPageButton = screen.getByText('Anterior'); + const firstPageButton = screen.getByText('Primera'); + const lastPageButton = screen.getByText('Última'); + + userEvent.click(nextPageButton); + + await waitFor(() => { + expect(screen.getByText('Historial de testuser')).toBeInTheDocument(); + expect(screen.getByText('Question 11')).toBeInTheDocument(); + expect(screen.getByText('Question 20')).toBeInTheDocument(); + }); + + userEvent.click(prevPageButton); + + await waitFor(() => { + expect(screen.getByText('Historial de testuser')).toBeInTheDocument(); + expect(screen.getByText('Question 1')).toBeInTheDocument(); + expect(screen.getByText('Question 10')).toBeInTheDocument(); + }); + + userEvent.click(lastPageButton); + + await waitFor(() => { + expect(screen.getByText('Historial de testuser')).toBeInTheDocument(); + expect(screen.getByText('Question 11')).toBeInTheDocument(); + expect(screen.getByText('Question 20')).toBeInTheDocument(); + }); + + userEvent.click(firstPageButton); + + await waitFor(() => { + expect(screen.getByText('Historial de testuser')).toBeInTheDocument(); + expect(screen.getByText('Question 1')).toBeInTheDocument(); + expect(screen.getByText('Question 10')).toBeInTheDocument(); + }); + }); + + test('handles unknown error when fetching games', async () => { + const errorMessage = 'Error fetching games'; + axios.get.mockRejectedValueOnce(new Error(errorMessage)); + + render(); + + expect(axios.get).toHaveBeenCalledWith('http://localhost:8000/history/games/testuser'); + }); +}); \ No newline at end of file