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

71 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { FIND_THE_RING_MAX_WRONG_ATTEMPTS, FindTheRingGame } from '../../src/components/minigames/FindTheRingGame';
describe('FindTheRingGame', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.spyOn(Math, 'random').mockReturnValue(0);
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it('shows start flow and reveals cups after shuffling', async () => {
render(<FindTheRingGame onSuccess={vi.fn()} onFail={vi.fn()} />);
fireEvent.click(screen.getByRole('button', { name: /Start Shuffling/i }));
await act(async () => {
vi.advanceTimersByTime(1300);
});
expect(screen.getAllByRole('button').length).toBe(3);
expect(screen.getByText(new RegExp(`Wrong guesses: 0/${FIND_THE_RING_MAX_WRONG_ATTEMPTS}`))).toBeTruthy();
});
it('fails on the third wrong guess', async () => {
const onFail = vi.fn();
render(<FindTheRingGame onSuccess={vi.fn()} onFail={onFail} />);
fireEvent.click(screen.getByRole('button', { name: /Start Shuffling/i }));
await act(async () => {
vi.advanceTimersByTime(1300);
});
const clickWrongCup = async (name: string) => {
fireEvent.click(screen.getByRole('button', { name }));
await act(async () => {
vi.advanceTimersByTime(850);
});
};
await clickWrongCup('Cup 2');
await clickWrongCup('Cup 3');
await clickWrongCup('Cup 2');
await act(async () => {
vi.runOnlyPendingTimers();
});
expect(onFail).toHaveBeenCalledTimes(1);
});
it('succeeds when the player picks the correct cup', async () => {
const onSuccess = vi.fn();
render(<FindTheRingGame onSuccess={onSuccess} onFail={vi.fn()} />);
fireEvent.click(screen.getByRole('button', { name: /Start Shuffling/i }));
await act(async () => {
vi.advanceTimersByTime(1300);
});
fireEvent.click(screen.getByRole('button', { name: 'Cup 1' }));
await act(async () => {
vi.runOnlyPendingTimers();
});
expect(onSuccess).toHaveBeenCalledTimes(1);
});
});