|
| 1 | +import { |
| 2 | + initializeMocks, render, screen, waitFor, |
| 3 | +} from '@src/testUtils'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import { useCourseItemData } from '@src/course-outline/data/apiHooks'; |
| 6 | +import { VisibilityTypes } from '@src/data/constants'; |
| 7 | +import { VisibilitySection } from './VisibilitySection'; |
| 8 | + |
| 9 | +jest.mock('@src/course-outline/data/apiHooks', () => ({ |
| 10 | + ...jest.requireActual('@src/course-outline/data/apiHooks'), |
| 11 | + useCourseItemData: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +const mockUseCourseItemData = useCourseItemData as jest.Mock; |
| 15 | + |
| 16 | +const defaultProps = { |
| 17 | + itemId: 'block-v1:course+type@sequential+block@test', |
| 18 | + isSubsection: true, |
| 19 | + onChange: jest.fn(), |
| 20 | +}; |
| 21 | + |
| 22 | +describe('VisibilitySection component', () => { |
| 23 | + beforeEach(() => { |
| 24 | + initializeMocks(); |
| 25 | + mockUseCourseItemData.mockReturnValue({ data: undefined }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('renders title and buttons', async () => { |
| 29 | + render(<VisibilitySection {...defaultProps} />); |
| 30 | + expect(await screen.findByText('Visibility')).toBeInTheDocument(); |
| 31 | + expect(await screen.findByRole('button', { name: 'Student Visible' })).toBeInTheDocument(); |
| 32 | + expect(await screen.findByRole('button', { name: 'Staff Only' })).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('clicking staff only calls onChange with staff and hideAfterDue false', async () => { |
| 36 | + const user = userEvent.setup(); |
| 37 | + const onChange = jest.fn(); |
| 38 | + render(<VisibilitySection {...defaultProps} onChange={onChange} />); |
| 39 | + |
| 40 | + await user.click(await screen.findByRole('button', { name: 'Staff Only' })); |
| 41 | + await waitFor(async () => { |
| 42 | + expect(onChange).toHaveBeenCalledWith({ isVisibleToStaffOnly: true, hideAfterDue: false }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('clicking student visible calls onChange with isVisibleToStaffOnly false', async () => { |
| 47 | + const user = userEvent.setup(); |
| 48 | + const onChange = jest.fn(); |
| 49 | + mockUseCourseItemData.mockReturnValue({ data: { visibilityState: VisibilityTypes.STAFF_ONLY } }); |
| 50 | + render(<VisibilitySection {...defaultProps} onChange={onChange} />); |
| 51 | + |
| 52 | + await user.click(await screen.findByRole('button', { name: 'Student Visible' })); |
| 53 | + await waitFor(async () => { |
| 54 | + expect(onChange).toHaveBeenCalledWith({ isVisibleToStaffOnly: false }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('shows checkbox when subsection and not staff only, and toggling it calls onChange', async () => { |
| 59 | + const user = userEvent.setup(); |
| 60 | + const onChange = jest.fn(); |
| 61 | + // initial data not staff only |
| 62 | + mockUseCourseItemData.mockReturnValue({ data: { visibilityState: undefined, hideAfterDue: false } }); |
| 63 | + render(<VisibilitySection {...defaultProps} onChange={onChange} />); |
| 64 | + |
| 65 | + const checkbox = await screen.findByRole('checkbox'); |
| 66 | + await user.click(checkbox); |
| 67 | + await waitFor(async () => { |
| 68 | + expect(onChange).toHaveBeenCalledWith({ hideAfterDue: true, isVisibleToStaffOnly: false }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('hides checkbox when staff visible', async () => { |
| 73 | + const onChange = jest.fn(); |
| 74 | + // when item is staff only, checkbox should not be present |
| 75 | + mockUseCourseItemData.mockReturnValue({ data: { visibilityState: VisibilityTypes.STAFF_ONLY } }); |
| 76 | + render(<VisibilitySection {...defaultProps} onChange={onChange} />); |
| 77 | + expect(screen.queryByRole('checkbox')).not.toBeInTheDocument(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments