Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287747 committed Mar 4, 2024
1 parent ae3a8ad commit 5563b40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions webapp/src/components/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const Question = ({ goTo, setGameFinished }) => {
if (isCorrect(option)) return 'green';
};

// NOSONAR
const shuffleOptions = (options) => {
return options.sort(() => Math.random() - 0.5);
};
Expand Down
23 changes: 22 additions & 1 deletion webapp/src/test/Game.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, act } from '@testing-library/react';
import { Game } from '../components/Game';

const MAX_TIME = 600;

// Mock de preguntas predefinidas
const mockQuestions = [
{
Expand All @@ -19,6 +21,16 @@ const mockQuestions = [
// Mock de la función goTo
const mockGoTo = jest.fn();

const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
clear: jest.fn(),
};
global.localStorage = localStorageMock;

// Mock de setInterval y clearInterval
jest.useFakeTimers();

describe('Game component', () => {
beforeEach(() => {
jest.spyOn(global, 'fetch').mockResolvedValue({
Expand Down Expand Up @@ -58,4 +70,13 @@ describe('Game component', () => {
//fireEvent.click(nextButton);
//expect(global.fetch).toHaveBeenCalledTimes(2); // Verifica que se hizo una segunda llamada a fetch para obtener la siguiente pregunta
});

// Test para verificar que el juego finaliza cuando se alcanza el número máximo de preguntas
test('El juego finaliza correctamente cuando se alcanza el número máximo de preguntas', async () => {

render(<Game goTo={() => {}} setGameFinished={() => {}} />);
act(() => {
jest.advanceTimersByTime(MAX_TIME * 1000);
});
});
});
6 changes: 3 additions & 3 deletions webapp/src/test/PostGame.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('PostGame component', () => {

test('formatTiempo devuelve el formato de tiempo correcto', () => {
// Ejemplo de datos de entrada y salida esperada
const segundos = 600; // 10 minutos en segundos
const tiempoEsperado = '10:00';
const segundos = 0; // 0 segundos
const tiempoEsperado = '00:00';

// Renderizar el componente PostGame que contiene la función formatTiempo
render(<PostGame />);
Expand All @@ -38,6 +38,6 @@ describe('PostGame component', () => {
const tiempoUsadoCell = screen.getByText('Tiempo usado').closest('tr').querySelector('td:last-child');

// Verificar si el texto del componente coincide con el tiempo esperado
//expect(tiempoUsadoCell.textContent).toBe(tiempoEsperado);
expect(tiempoUsadoCell.textContent).toBe(tiempoEsperado);
});
});

0 comments on commit 5563b40

Please sign in to comment.