lotr-sut/sut/frontend/test/components/TriviaGame.test.tsx
Fellowship Scholar f6a5823439 init commit
2026-03-29 20:07:56 +00:00

82 lines
3.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import { TRIVIA_QUESTION_POOL, TRIVIA_REQUIRED_CORRECT, TRIVIA_SESSION_SIZE, TriviaGame } from '../../src/components/minigames/TriviaGame';
describe('TriviaGame', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.spyOn(Math, 'random').mockReturnValue(0);
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it('renders a 4-question session and keeps next disabled until answering', () => {
render(<TriviaGame onSuccess={vi.fn()} onFail={vi.fn()} />);
expect(screen.getByText(/Gandalf's Trivia Challenge/i)).toBeTruthy();
expect(screen.getByText(new RegExp(`Question 1 of ${TRIVIA_SESSION_SIZE}`))).toBeTruthy();
expect(screen.getByText(new RegExp(`Pool: ${TRIVIA_QUESTION_POOL.length} questions`))).toBeTruthy();
const nextButton = screen.getByRole('button', { name: /Next Question/i });
expect(nextButton).toHaveProperty('disabled', true);
expect(screen.getAllByRole('button').length).toBeGreaterThanOrEqual(5);
});
it('succeeds when the player answers at least 3 of 4 correctly', () => {
const onSuccess = vi.fn();
const onFail = vi.fn();
render(<TriviaGame onSuccess={onSuccess} onFail={onFail} />);
fireEvent.click(screen.getByRole('button', { name: 'Samwise' }));
fireEvent.click(screen.getByRole('button', { name: /Next Question/i }));
fireEvent.click(screen.getByRole('button', { name: 'Hobbiton' }));
fireEvent.click(screen.getByRole('button', { name: /Next Question/i }));
fireEvent.click(screen.getByRole('button', { name: 'Mithril is only known in Dwarvish' }));
fireEvent.click(screen.getByRole('button', { name: /Next Question/i }));
fireEvent.click(screen.getByRole('button', { name: 'Sauron' }));
fireEvent.click(screen.getByRole('button', { name: /See Result/i }));
vi.runAllTimers();
expect(onSuccess).toHaveBeenCalledTimes(1);
expect(onFail).not.toHaveBeenCalled();
});
it('fails when the player finishes below the 3-correct threshold', () => {
const onSuccess = vi.fn();
const onFail = vi.fn();
render(<TriviaGame onSuccess={onSuccess} onFail={onFail} />);
fireEvent.click(screen.getByRole('button', { name: 'Gandalf' }));
fireEvent.click(screen.getByRole('button', { name: /Next Question/i }));
fireEvent.click(screen.getByRole('button', { name: 'Bree' }));
fireEvent.click(screen.getByRole('button', { name: /Next Question/i }));
fireEvent.click(screen.getByRole('button', { name: 'Adamant' }));
fireEvent.click(screen.getByRole('button', { name: /Next Question/i }));
fireEvent.click(screen.getByRole('button', { name: 'Sauron' }));
fireEvent.click(screen.getByRole('button', { name: /See Result/i }));
vi.runAllTimers();
expect(onFail).toHaveBeenCalledTimes(1);
expect(onSuccess).not.toHaveBeenCalled();
});
it('documents the required score target in the UI', () => {
render(<TriviaGame onSuccess={vi.fn()} onFail={vi.fn()} />);
expect(screen.getByText(new RegExp(`Score: 0/${TRIVIA_REQUIRED_CORRECT} needed`))).toBeTruthy();
});
});