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

game test v4 #197

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
131 changes: 129 additions & 2 deletions webapp/src/components/Game.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,133 @@ describe('Game component', () => {
});
});



it('should handle dialog open and close', async () => {
renderGameComponent();

fireEvent.click(screen.getByText('Volver al menú principal'));

expect(screen.getByText('Confirmación')).toBeInTheDocument();

fireEvent.click(screen.getByText('Cancelar'));

await waitFor(() => {
expect(screen.queryByText('Confirmación')).not.toBeInTheDocument();
});
});

it('should stop and start timer when dialog opens and closes', async () => {
renderGameComponent();

fireEvent.click(screen.getByText('Volver al menú principal'));

expect(screen.getByText('Cancelar')).toBeInTheDocument();

fireEvent.click(screen.getByText('Cancelar'));

await waitFor(() => {
expect(screen.getByText('Preguntas restantes: 5')).toBeInTheDocument();
});
});
it('should handle timer countdown correctly', async () => {
renderGameComponent();

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

await waitFor(() => {
if (screen.queryByText(/Pregunta 1:/i)) {
jest.advanceTimersByTime(10000);
expect(screen.getByText('Preguntas restantes: 4')).toBeInTheDocument();
}
});
});

it('should handle game finish correctly', async () => {
renderGameComponent();

for (let i = 1; i <= 5; i++) {
mockAxios.onGet('http://localhost:8000/createquestion').reply(200, { data: mockQuestionData });

await waitFor(() => {
if (screen.queryByText(`Pregunta ${i}:`)) {
fireEvent.click(screen.getByText('4'));
expect(screen.getByText(`Correctas: ${i}`)).toBeInTheDocument();
expect(screen.getByText(`Preguntas restantes: ${5 - i}`)).toBeInTheDocument();
}
});
}

await waitFor(() => {
if (screen.queryByText('Partida finalizada')) {
expect(screen.getByText('Partida finalizada')).toBeInTheDocument();
expect(screen.getByText('Gracias por jugar')).toBeInTheDocument();
expect(screen.getByText('Puntuación')).toBeInTheDocument();
expect(screen.getByText('Volver al menú principal')).toBeInTheDocument();
}
});
});

it('should disable buttons when time runs out', async () => {
renderGameComponent();

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

await waitFor(() => {
if (screen.queryByText(/Pregunta 1:/i)) {
jest.advanceTimersByTime(10000);
const buttons = screen.getAllByRole('button', { name: /answer/i });
buttons.forEach(button => {
expect(button).toBeDisabled();
});
}
});
});

it('should handle timeout correctly', async () => {
renderGameComponent();

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

await waitFor(() => {
if (screen.queryByText(/Pregunta 1:/i)) {
jest.advanceTimersByTime(10000);
expect(screen.getByText('4')).toHaveStyle({ backgroundColor: 'rgba(79, 141, 18, 0.726)' });
}
});
});


it('should display the timer correctly', async () => {
renderGameComponent();


expect(screen.getByText('10')).toBeInTheDocument(); // Comprobar el valor inicial del temporizador

// Esperar a que el temporizador llegue a cero
await waitFor(() => {
if (screen.queryByText('0')) {
expect(screen.queryByText('0')).toBeInTheDocument(); // Comprobar que el temporizador llega a cero
}
});
});

it('should countdown correctly', async () => {
renderGameComponent();

// Verificar que el temporizador comience con el valor correcto
expect(screen.getByText('10')).toBeInTheDocument();

for(let i = 9; i >= 0; i--){
await waitFor(() => {
if (screen.queryByText(String(i))) {
expect(screen.queryByText(String(i))).toBeInTheDocument();
}
});
}

// Verificar que el temporizador no sea negativo
expect(screen.queryByText('-1')).not.toBeInTheDocument();
});

});