|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { renderWithIntl } from '@src/testUtils'; |
| 4 | +import GradingPage from './GradingPage'; |
| 5 | +import messages from './messages'; |
| 6 | + |
| 7 | +// Mock child components, each component should have its own test suite |
| 8 | +jest.mock('./components/GradingLearnerContent', () => { |
| 9 | + return function MockGradingLearnerContent({ toolType }: { toolType: string }) { |
| 10 | + return <div>Grading Content for: {toolType}</div>; |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +jest.mock('./components/GradingActionRow', () => { |
| 15 | + return function MockGradingActionRow() { |
| 16 | + return <div>Grading Action Row</div>; |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +jest.mock('@src/components/PendingTasks', () => { |
| 21 | + return { |
| 22 | + PendingTasks: function MockPendingTasks() { |
| 23 | + return <div>Pending Tasks</div>; |
| 24 | + } |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +describe('GradingPage', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('renders the page title correctly', () => { |
| 34 | + renderWithIntl(<GradingPage />); |
| 35 | + expect(screen.getByText(messages.pageTitle.defaultMessage)).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('renders all child components', () => { |
| 39 | + renderWithIntl(<GradingPage />); |
| 40 | + expect(screen.getByText('Grading Action Row')).toBeInTheDocument(); |
| 41 | + expect(screen.getByText('Grading Content for: single')).toBeInTheDocument(); |
| 42 | + expect(screen.getByText('Pending Tasks')).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders both button options with correct labels', () => { |
| 46 | + renderWithIntl(<GradingPage />); |
| 47 | + expect(screen.getByRole('button', { name: messages.singleLearner.defaultMessage })).toBeInTheDocument(); |
| 48 | + expect(screen.getByRole('button', { name: messages.allLearners.defaultMessage })).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('defaults to single learner tool being selected', () => { |
| 52 | + renderWithIntl(<GradingPage />); |
| 53 | + const singleLearnerButton = screen.getByRole('button', { name: messages.singleLearner.defaultMessage }); |
| 54 | + const allLearnersButton = screen.getByRole('button', { name: messages.allLearners.defaultMessage }); |
| 55 | + |
| 56 | + // Single learner should have primary variant (selected state) |
| 57 | + expect(singleLearnerButton).toHaveClass('btn-primary'); |
| 58 | + expect(allLearnersButton).toHaveClass('btn-outline-primary'); |
| 59 | + |
| 60 | + // GradingLearnerContent should receive 'single' as initial toolType |
| 61 | + expect(screen.getByText('Grading Content for: single')).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('switches to All Learners when All Learners button is clicked', async () => { |
| 65 | + renderWithIntl(<GradingPage />); |
| 66 | + const user = userEvent.setup(); |
| 67 | + |
| 68 | + const allLearnersButton = screen.getByRole('button', { name: messages.allLearners.defaultMessage }); |
| 69 | + |
| 70 | + await user.click(allLearnersButton); |
| 71 | + |
| 72 | + // All learners should now be selected |
| 73 | + expect(allLearnersButton).toHaveClass('btn-primary'); |
| 74 | + expect(screen.getByRole('button', { name: messages.singleLearner.defaultMessage })).toHaveClass('btn-outline-primary'); |
| 75 | + |
| 76 | + // GradingLearnerContent should receive 'all' as toolType |
| 77 | + expect(screen.getByText('Grading Content for: all')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('switches back to Single Learner when Single Learner button is clicked', async () => { |
| 81 | + renderWithIntl(<GradingPage />); |
| 82 | + const user = userEvent.setup(); |
| 83 | + |
| 84 | + const singleLearnerButton = screen.getByRole('button', { name: messages.singleLearner.defaultMessage }); |
| 85 | + const allLearnersButton = screen.getByRole('button', { name: messages.allLearners.defaultMessage }); |
| 86 | + |
| 87 | + // First switch to all learners |
| 88 | + await user.click(allLearnersButton); |
| 89 | + expect(screen.getByText('Grading Content for: all')).toBeInTheDocument(); |
| 90 | + |
| 91 | + // Then switch back to single learner |
| 92 | + await user.click(singleLearnerButton); |
| 93 | + |
| 94 | + // Single learner should be selected again |
| 95 | + expect(singleLearnerButton).toHaveClass('btn-primary'); |
| 96 | + expect(allLearnersButton).toHaveClass('btn-outline-primary'); |
| 97 | + expect(screen.getByText('Grading Content for: single')).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('maintains correct button states during multiple interactions', async () => { |
| 101 | + renderWithIntl(<GradingPage />); |
| 102 | + const user = userEvent.setup(); |
| 103 | + |
| 104 | + const singleLearnerButton = screen.getByRole('button', { name: messages.singleLearner.defaultMessage }); |
| 105 | + const allLearnersButton = screen.getByRole('button', { name: messages.allLearners.defaultMessage }); |
| 106 | + |
| 107 | + // Initial state - single learner selected |
| 108 | + expect(singleLearnerButton).toHaveClass('btn-primary'); |
| 109 | + expect(allLearnersButton).toHaveClass('btn-outline-primary'); |
| 110 | + |
| 111 | + // Click all learners multiple times - should remain selected |
| 112 | + await user.click(allLearnersButton); |
| 113 | + await user.click(allLearnersButton); |
| 114 | + |
| 115 | + expect(allLearnersButton).toHaveClass('btn-primary'); |
| 116 | + expect(singleLearnerButton).toHaveClass('btn-outline-primary'); |
| 117 | + expect(screen.getByText('Grading Content for: all')).toBeInTheDocument(); |
| 118 | + |
| 119 | + // Click single learner multiple times - should remain selected |
| 120 | + await user.click(singleLearnerButton); |
| 121 | + await user.click(singleLearnerButton); |
| 122 | + |
| 123 | + expect(singleLearnerButton).toHaveClass('btn-primary'); |
| 124 | + expect(allLearnersButton).toHaveClass('btn-outline-primary'); |
| 125 | + expect(screen.getByText('Grading Content for: single')).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('passes correct toolType prop to GradingLearnerContent component', () => { |
| 129 | + renderWithIntl(<GradingPage />); |
| 130 | + |
| 131 | + // Initially should pass 'single' |
| 132 | + expect(screen.getByText('Grading Content for: single')).toBeInTheDocument(); |
| 133 | + expect(screen.queryByText('Grading Content for: all')).not.toBeInTheDocument(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments