|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { render, screen } from '@testing-library/react' |
| 3 | +import { HostQuestionAnswers } from '@/components/host/HostQuestionAnswers' |
| 4 | +import type { Question } from '@/db' |
| 5 | + |
| 6 | +const BASE: Question = { |
| 7 | + id: 'q1', |
| 8 | + title: 'Q', |
| 9 | + type: 'multiple_choice', |
| 10 | + options: ['Paris', 'Lyon', 'Marseille', 'Nice'], |
| 11 | + answer: 'Paris', |
| 12 | + description: '', |
| 13 | + difficulty: null, |
| 14 | + tags: [], |
| 15 | + media: null, |
| 16 | + mediaType: null, |
| 17 | + createdAt: 0, |
| 18 | + updatedAt: 0, |
| 19 | +} |
| 20 | +function q(o: Partial<Question>): Question { |
| 21 | + return { ...BASE, ...o } |
| 22 | +} |
| 23 | + |
| 24 | +describe('HostQuestionAnswers — multiple_choice', () => { |
| 25 | + it('renders all four options', () => { |
| 26 | + render(<HostQuestionAnswers question={q({})} />) |
| 27 | + expect(screen.getByText('Paris')).toBeInTheDocument() |
| 28 | + expect(screen.getByText('Lyon')).toBeInTheDocument() |
| 29 | + expect(screen.getByText('Marseille')).toBeInTheDocument() |
| 30 | + expect(screen.getByText('Nice')).toBeInTheDocument() |
| 31 | + }) |
| 32 | + it('highlights the correct option with green background', () => { |
| 33 | + render(<HostQuestionAnswers question={q({})} />) |
| 34 | + expect(screen.getByText('Paris').closest('li')).toHaveStyle({ background: '#f0fdf4' }) |
| 35 | + }) |
| 36 | + it('does not highlight incorrect options', () => { |
| 37 | + render(<HostQuestionAnswers question={q({})} />) |
| 38 | + expect(screen.getByText('Lyon').closest('li')).not.toHaveStyle({ background: '#f0fdf4' }) |
| 39 | + }) |
| 40 | + it('shows "✓ Correct" marker only on the correct option', () => { |
| 41 | + render(<HostQuestionAnswers question={q({})} />) |
| 42 | + expect(screen.getAllByText('✓ Correct')).toHaveLength(1) |
| 43 | + }) |
| 44 | + it('renders alphabetic labels A–D', () => { |
| 45 | + render(<HostQuestionAnswers question={q({})} />) |
| 46 | + expect(screen.getByText('A')).toBeInTheDocument() |
| 47 | + expect(screen.getByText('B')).toBeInTheDocument() |
| 48 | + expect(screen.getByText('C')).toBeInTheDocument() |
| 49 | + expect(screen.getByText('D')).toBeInTheDocument() |
| 50 | + }) |
| 51 | +}) |
| 52 | + |
| 53 | +describe('HostQuestionAnswers — true_false', () => { |
| 54 | + const tf = q({ type: 'true_false', options: ['True', 'False'], answer: 'True' }) |
| 55 | + it('renders True and False options', () => { |
| 56 | + render(<HostQuestionAnswers question={tf} />) |
| 57 | + expect(screen.getByText('True')).toBeInTheDocument() |
| 58 | + expect(screen.getByText('False')).toBeInTheDocument() |
| 59 | + }) |
| 60 | + it('highlights True when answer is True', () => { |
| 61 | + render(<HostQuestionAnswers question={tf} />) |
| 62 | + expect(screen.getByText('True').closest('div')).toHaveStyle({ background: '#f0fdf4' }) |
| 63 | + expect(screen.getByText('False').closest('div')).not.toHaveStyle({ background: '#f0fdf4' }) |
| 64 | + }) |
| 65 | + it('highlights False when answer is False', () => { |
| 66 | + render( |
| 67 | + <HostQuestionAnswers |
| 68 | + question={q({ type: 'true_false', options: ['True', 'False'], answer: 'False' })} |
| 69 | + /> |
| 70 | + ) |
| 71 | + expect(screen.getByText('False').closest('div')).toHaveStyle({ background: '#f0fdf4' }) |
| 72 | + expect(screen.getByText('True').closest('div')).not.toHaveStyle({ background: '#f0fdf4' }) |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +describe('HostQuestionAnswers — open_ended', () => { |
| 77 | + const oe = q({ type: 'open_ended', options: [], answer: 'The Eiffel Tower' }) |
| 78 | + it('renders the expected answer text', () => { |
| 79 | + render(<HostQuestionAnswers question={oe} />) |
| 80 | + expect(screen.getByText('The Eiffel Tower')).toBeInTheDocument() |
| 81 | + }) |
| 82 | + it('renders the "Expected answer" label', () => { |
| 83 | + render(<HostQuestionAnswers question={oe} />) |
| 84 | + expect(screen.getByText('Expected answer')).toBeInTheDocument() |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +describe('HostQuestionAnswers — section label', () => { |
| 89 | + it('renders "Answers" heading', () => { |
| 90 | + render(<HostQuestionAnswers question={q({})} />) |
| 91 | + expect(screen.getByText('Answers')).toBeInTheDocument() |
| 92 | + }) |
| 93 | +}) |
0 commit comments