|
| 1 | +import { fireEvent, render, screen } from 'test-utils'; |
| 2 | + |
| 3 | +import GradebookIndex from '../pages/GradebookIndex'; |
| 4 | +import { GradebookState } from '../types'; |
| 5 | + |
| 6 | +jest.mock('../operations', () => ({ |
| 7 | + __esModule: true, |
| 8 | + default: () => (): Promise<void> => Promise.resolve(), |
| 9 | +})); |
| 10 | + |
| 11 | +const gradebookState: GradebookState = { |
| 12 | + tabs: [ |
| 13 | + { id: 1, title: 'Assignments', categoryId: 10, categoryTitle: 'Missions' }, |
| 14 | + ], |
| 15 | + assessments: [{ id: 10, title: 'Assignment 1', tabId: 1, maxGrade: 100 }], |
| 16 | + students: [ |
| 17 | + { |
| 18 | + id: 1, |
| 19 | + name: 'Alice Smith', |
| 20 | + grades: { '10': 80 }, |
| 21 | + totalGrade: 80, |
| 22 | + totalMaxGrade: 100, |
| 23 | + }, |
| 24 | + { |
| 25 | + id: 2, |
| 26 | + name: 'Bob Jones', |
| 27 | + grades: { '10': 60 }, |
| 28 | + totalGrade: 60, |
| 29 | + totalMaxGrade: 100, |
| 30 | + }, |
| 31 | + ], |
| 32 | +}; |
| 33 | + |
| 34 | +describe('<GradebookIndex />', () => { |
| 35 | + it('renders all students initially', async () => { |
| 36 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 37 | + expect(await screen.findByText('Alice Smith')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText('Bob Jones')).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('filters students by name on search input', async () => { |
| 42 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 43 | + |
| 44 | + const input = await screen.findByPlaceholderText('Search by student name'); |
| 45 | + fireEvent.change(input, { target: { value: 'alice' } }); |
| 46 | + |
| 47 | + expect(screen.getByText('Alice Smith')).toBeInTheDocument(); |
| 48 | + expect(screen.queryByText('Bob Jones')).not.toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('renders a show percentage toggle', async () => { |
| 52 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 53 | + expect(await screen.findByLabelText('Show percentage')).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('shows raw scores by default', async () => { |
| 57 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 58 | + |
| 59 | + // Raw score is the default — Alice's grade shows as "80 / 100" |
| 60 | + expect(await screen.findAllByText('80 / 100')).not.toHaveLength(0); |
| 61 | + expect(screen.queryByText('80.00%')).not.toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('switches from raw score to percentage when the toggle is clicked', async () => { |
| 65 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 66 | + |
| 67 | + // Wait for default raw score rendering |
| 68 | + await screen.findAllByText('80 / 100'); |
| 69 | + |
| 70 | + // Click the "Show percentage" toggle |
| 71 | + fireEvent.click(screen.getByLabelText('Show percentage')); |
| 72 | + |
| 73 | + // Percentage replaces the raw score |
| 74 | + expect(await screen.findAllByText('80.00%')).not.toHaveLength(0); |
| 75 | + expect(screen.queryByText('80 / 100')).not.toBeInTheDocument(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments