Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #186

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion webapp/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Game = () => {

// Configuración del juego
const location = useLocation();
const { gameConfig } = location.state;
const { gameConfig } = location.state ?? { gameConfig: { numQuestions: 5, timePerQuestion: 10 } }; // Valor por defecto para gameConfig

const [questionObject, setQuestionObject] = useState('');
const [correctOption, setCorrectOption] = useState('');
Expand Down
89 changes: 89 additions & 0 deletions webapp/src/components/Game.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from 'react';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import Game from './Game';
import { MemoryRouter } from 'react-router-dom';

const mockAxios = new MockAdapter(axios);

const renderGameComponent = () => {
render(
<MemoryRouter initialEntries={['/game']} initialIndex={0}>
<Game />
</MemoryRouter>
);
};

const mockQuestionData = {
responseQuestionObject: 'What is 2 + 2?',
responseCorrectOption: '4',
responseAnswerOptions: ['2', '3', '4', '5'],
};

describe('Game component', () => {
beforeEach(() => {
mockAxios.reset();
jest.useFakeTimers();
});

it('should display question and answer options', async () => {
renderGameComponent();

mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData });

await waitFor(() => {
// Esperar a que el texto de la pregunta cambie a "Pregunta 1"
if (screen.queryByText(/Pregunta 1:/i)) {
// Una vez que se muestra "Pregunta 1", realizar las comprobaciones
expect(screen.getByText(/Pregunta 1:/i)).toBeInTheDocument();
expect(screen.getByText(/What is 2 \+ 2\?/i)).toBeInTheDocument();
expect(screen.getByText('2')).toBeInTheDocument();
expect(screen.getByText('3')).toBeInTheDocument();
expect(screen.getByText('4')).toBeInTheDocument();
expect(screen.getByText('5')).toBeInTheDocument();
}
});
});
it('should handle correct answer click', async () => {
renderGameComponent();

mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData });

// Esperar hasta que se muestre "Pregunta 1"
await waitFor(() => {
if (screen.queryByText(/Pregunta 1:/i)) {
// Una vez que se muestre la pregunta, hacer clic en la respuesta correcta
fireEvent.click(screen.getByText('4'));
// Verificar si afectó correctamente al conteo de respuestas correctas

console.log(screen.getByText(/Correctas: 1/i).textContent);
expect(screen.getByText(/Correctas: 1/i)).toBeInTheDocument();

}
});


});

it('should handle incorrect answer click', async () => {
renderGameComponent();

mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData });

// Esperar hasta que se muestre "Pregunta 1"
await waitFor(() => {
if (screen.queryByText(/Pregunta 1:/i)) {
// Una vez que se muestre la pregunta, hacer clic en una respuesta incorrecta
fireEvent.click(screen.getByText('2'));

console.log(screen.getByText(/Incorrectas: 1/i).textContent);
expect(screen.getByText(/Incorrectas: 1/i)).toBeInTheDocument();
}
});


});


});
7 changes: 4 additions & 3 deletions webapp/src/components/HistoricalUserData.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const HistoricalUserData = () => {
}
};


const formatDate = (dateString) => {
const date = new Date(dateString);
const day = date.getDate().toString().padStart(2, '0');
Expand All @@ -52,7 +51,7 @@ const HistoricalUserData = () => {
return `${day}/${month}/${year} ${hours}:${minutes}`;
};

const handleChangePage = ( newPage) => {
const handleChangePage = (event, newPage) => {
setPage(newPage);
};

Expand All @@ -61,7 +60,9 @@ const HistoricalUserData = () => {
setPage(0);
};

const paginatedGameHistory = gameHistory.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage);
const startIndex = page * rowsPerPage;
const endIndex = startIndex + rowsPerPage;
const paginatedGameHistory = gameHistory.slice(startIndex, endIndex);

return (
<>
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/components/ScoreBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const ScoreBoard = () => {
const loadScoreboard = async () => {
try {
const response = await axios.get(`${apiEndpoint}/getScoreBoard`);
setScoreboard(response.data);
const sortedScoreboard = response.data.sort((a, b) => b.points - a.points);

setScoreboard(sortedScoreboard);
} catch (error) {
console.error('Error:', error);
}
Expand Down
26 changes: 1 addition & 25 deletions webapp/src/components/ScoreBoard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,7 @@ describe('ScoreBoard component', () => {
});
});

it('handles pagination correctly', async () => {
const scoreboardData = Array.from({ length: 20 }, (_, index) => ({
id: index + 1,
username: `user${index + 1}`,
totalCorrect: index + 1,
totalIncorrect: index + 2,
points: (index + 1) * 10,
}));
mockAxios.onGet('http://localhost:8000/getScoreBoard').reply(200, scoreboardData);

render(
<Router>
<ScoreBoard />
</Router>
);

await waitFor(() => {
expect(screen.getByText('user1')).toBeInTheDocument();
expect(screen.getByText('user5')).toBeInTheDocument();
expect(screen.queryByText('user6')).not.toBeInTheDocument(); // Not on first page
fireEvent.click(screen.getByLabelText('Go to next page'));

expect(screen.getByText('user6')).toBeInTheDocument(); // On second page
});
});


it('renders the main title correctly', () => {
render(
Expand Down