|
| 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: [{ id: 1, title: 'Assignments' }], |
| 13 | + assessments: [{ id: 10, title: 'Assignment 1', tabId: 1, maxGrade: 100 }], |
| 14 | + students: [ |
| 15 | + { |
| 16 | + id: 1, |
| 17 | + name: 'Alice Smith', |
| 18 | + grades: { '10': 80 }, |
| 19 | + totalGrade: 80, |
| 20 | + totalMaxGrade: 100, |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 2, |
| 24 | + name: 'Bob Jones', |
| 25 | + grades: { '10': 60 }, |
| 26 | + totalGrade: 60, |
| 27 | + totalMaxGrade: 100, |
| 28 | + }, |
| 29 | + ], |
| 30 | +}; |
| 31 | + |
| 32 | +describe('<GradebookIndex />', () => { |
| 33 | + it('renders all students initially', async () => { |
| 34 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 35 | + expect(await screen.findByText('Alice Smith')).toBeInTheDocument(); |
| 36 | + expect(screen.getByText('Bob Jones')).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('filters students by name on search input', async () => { |
| 40 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 41 | + |
| 42 | + const input = await screen.findByPlaceholderText('Search by student name'); |
| 43 | + fireEvent.change(input, { target: { value: 'alice' } }); |
| 44 | + |
| 45 | + expect(screen.getByText('Alice Smith')).toBeInTheDocument(); |
| 46 | + expect(screen.queryByText('Bob Jones')).not.toBeInTheDocument(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders a raw score toggle', async () => { |
| 50 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 51 | + expect(await screen.findByLabelText('Show raw score')).toBeInTheDocument(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('switches existing rows from percentage to raw score when the toggle is clicked', async () => { |
| 55 | + render(<GradebookIndex />, { state: { gradebook: gradebookState } }); |
| 56 | + |
| 57 | + // Wait for table to render — Alice's grade shows as percentage by default. |
| 58 | + expect(await screen.findAllByText('80.00%')).not.toHaveLength(0); |
| 59 | + |
| 60 | + // Click the toggle. |
| 61 | + fireEvent.click(screen.getByLabelText('Show raw score')); |
| 62 | + |
| 63 | + // Existing rows must re-render — raw score replaces the percentage. |
| 64 | + expect(await screen.findAllByText('80 / 100')).not.toHaveLength(0); |
| 65 | + expect(screen.queryByText('80.00%')).not.toBeInTheDocument(); |
| 66 | + }); |
| 67 | +}); |
0 commit comments