Skip to content

Commit

Permalink
Agregado mas tests en gatewayeservice, historial y jugar
Browse files Browse the repository at this point in the history
  • Loading branch information
Verzidee committed Mar 31, 2024
1 parent 1ec334c commit f58d4a4
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 13 deletions.
63 changes: 63 additions & 0 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,67 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
});
// Test for /getquestions endpoint
it('should forward get questions request to generate service', async () => {
axios.get.mockResolvedValue({ data: [{ question: 'What is 2+2?' }] });
const response = await request(app)
.get('/getquestions');

expect(response.statusCode).toBe(200);
expect(response.body).toEqual([{ question: 'What is 2+2?' }]);
});

// Test for /createquestion endpoint
it('should forward create question request to generate service', async () => {
axios.post.mockResolvedValue({ data: { success: true, id: 'questionId' } });
const response = await request(app)
.post('/createquestion')
.send({ question: 'What is 2+2?', answer: '4' });

expect(response.statusCode).toBe(201);
expect(response.body).toEqual({ success: true, id: 'questionId' });
});

// Test for /updatequestion/:id endpoint
it('should forward update question request to generate service', async () => {
axios.put.mockResolvedValue({ status: 200 });
const response = await request(app)
.put('/updatequestion/questionId')
.send({ question: 'Updated Question?', answer: 'Updated Answer' });

expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('OK');
});

// Test for /deletequestion/:id endpoint
it('should forward delete question request to generate service', async () => {
axios.delete.mockResolvedValue({ status: 200 });
const response = await request(app)
.put('/deletequestion/questionId');

expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('OK');
});

// Test for /savehistory endpoint
it('should forward save history request to history service', async () => {
axios.post.mockResolvedValue({ data: { success: true } });
const response = await request(app)
.post('/savehistory')
.send({ username: 'testuser', action: 'test action' });

expect(response.statusCode).toBe(200);
expect(response.body.success).toBe(true);
});

// Test for /gethistory endpoint
it('should forward get history request to history service', async () => {
axios.get.mockResolvedValue({ data: [{ action: 'test action', date: '2024-03-31' }] });
const response = await request(app)
.get('/gethistory?username=testuser');

expect(response.statusCode).toBe(200);
expect(response.body).toEqual([{ action: 'test action', date: '2024-03-31' }]);
});

});
48 changes: 39 additions & 9 deletions webapp/src/components/pages/Historial.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import axios from 'axios';
import Historial from './Historial';
import { AuthContext } from '../../AuthContext'; // Asegúrate de que la ruta sea correcta
import { AuthContext } from '../../AuthContext';
import { BrowserRouter } from 'react-router-dom';

// Mock de useNavigate
Expand All @@ -13,17 +14,46 @@ jest.mock('react-router-dom', () => ({
useNavigate: () => mockedNavigate,
}));

// Mock de axios
jest.mock('axios');

describe('Historial Component Tests', () => {
test('redirects to login if not logged in', () => {
render(
<BrowserRouter>
<AuthContext.Provider value={{ isLoggedIn: false }}>
<Historial />
</AuthContext.Provider>
</BrowserRouter>
<BrowserRouter>
<AuthContext.Provider value={{ isLoggedIn: false }}>
<Historial />
</AuthContext.Provider>
</BrowserRouter>
);

// Verifica que se llamó a navigate con '/login'
expect(mockedNavigate).toHaveBeenCalledWith('/login');
});

test('fetches and displays historial data', async () => {
const fakeHistorialData = {
username: "userTest",
NumJugadas: 10,
NumPreguntasJugadas: 40,
NumAcertadas: 25,
NumFalladas: 15
};

axios.get.mockResolvedValue({ data: fakeHistorialData });

render(
<BrowserRouter>
<AuthContext.Provider value={{ isLoggedIn: true, username: "userTest" }}>
<Historial />
</AuthContext.Provider>
</BrowserRouter>
);

// Espera a que el texto esperado aparezca en el documento
await waitFor(() => {
expect(screen.getByText(/Número de Partidas: 10/i)).toBeInTheDocument();
expect(screen.getByText(/Número de Preguntas Jugadas: 40/i)).toBeInTheDocument();
expect(screen.getByText(/Número de acertadas: 25/i)).toBeInTheDocument();
expect(screen.getByText(/Número de falladas: 15/i)).toBeInTheDocument();
});
});
});
40 changes: 36 additions & 4 deletions webapp/src/components/pages/Jugar.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { render } from '@testing-library/react';
import {render, screen, waitFor} from '@testing-library/react';
import '@testing-library/jest-dom';
import Jugar from './Jugar';
import { AuthContext } from '../../AuthContext';
import { BrowserRouter } from 'react-router-dom';
import axios from "axios";

// Mock de useNavigate
const mockedNavigate = jest.fn();
Expand All @@ -12,7 +13,8 @@ jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigate,
}));

// Mock de axios
jest.mock('axios');
describe('Jugar Component Tests', () => {
test('redirects to login if not logged in', () => {
render(
Expand All @@ -26,7 +28,37 @@ describe('Jugar Component Tests', () => {
// Verifica que se haya llamado a navigate con '/login'
expect(mockedNavigate).toHaveBeenCalledWith('/login');
});
test('check the questions', async () => {
const questions = [
{
question: "¿Cuál es la capital de Alemania?",
answers: [
{ answer: "Berlín", correct: true },
{ answer: "Múnich", correct: false },
{ answer: "Hamburgo", correct: false },
{ answer: "Colonia", correct: false },
],
}
];


// Aquí podrías añadir más pruebas relacionadas con el comportamiento del componente al estar logueado,
// como verificar la carga inicial de preguntas o el comportamiento del temporizador.
axios.get.mockResolvedValue({ data: questions });

render(
<BrowserRouter>
<AuthContext.Provider value={{ isLoggedIn: true }}>
<Jugar />
</AuthContext.Provider>
</BrowserRouter>
);

// Espera a que el texto esperado aparezca en el documento
await waitFor(() => {
expect(screen.getByText(/¿Cuál es la capital de Alemania?/i)).toBeInTheDocument();
expect(screen.getByText(/Berlín/i)).toBeInTheDocument();
expect(screen.getByText(/Múnich/i)).toBeInTheDocument();
expect(screen.getByText(/Hamburgo/i)).toBeInTheDocument();
expect(screen.getByText(/Colonia/i)).toBeInTheDocument();
});
});
});

0 comments on commit f58d4a4

Please sign in to comment.