|
| 1 | +import { fireEvent, initializeMocks, render, screen } from '@src/testUtils'; |
| 2 | +import { useCourseUserPermissions } from '@src/authz/hooks'; |
| 3 | +import { CourseAuthoringProvider } from '@src/CourseAuthoringContext'; |
| 4 | +import { mockContentTaxonomyTagsData } from '@src/content-tags-drawer/data/api.mocks'; |
| 5 | +import { CourseInfoSidebar } from './CourseInfoSidebar'; |
| 6 | + |
| 7 | +const courseId = mockContentTaxonomyTagsData.otherTagsId; |
| 8 | + |
| 9 | +jest.mock('@src/authz/hooks', () => ({ |
| 10 | + ...jest.requireActual('@src/authz/hooks'), |
| 11 | + useCourseUserPermissions: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('@src/course-outline/data/apiHooks', () => ({ |
| 15 | + ...jest.requireActual('@src/course-outline/data/apiHooks'), |
| 16 | + useCourseDetails: () => ({ data: { title: 'Test Course' } }), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('@src/search-manager', () => ({ |
| 20 | + useGetBlockTypes: () => ({ data: [] }), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('../OutlineSidebarContext', () => ({ |
| 24 | + ...jest.requireActual('../OutlineSidebarContext'), |
| 25 | + useOutlineSidebarContext: () => ({ |
| 26 | + currentTabKey: 'info', |
| 27 | + setCurrentTabKey: jest.fn(), |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +mockContentTaxonomyTagsData.applyMock(); |
| 32 | + |
| 33 | +const renderComponent = () => |
| 34 | + render(<CourseInfoSidebar />, { |
| 35 | + extraWrapper: ({ children }) => ( |
| 36 | + <CourseAuthoringProvider courseId={courseId}> |
| 37 | + {children} |
| 38 | + </CourseAuthoringProvider> |
| 39 | + ), |
| 40 | + }); |
| 41 | + |
| 42 | +describe('<CourseInfoSidebar />', () => { |
| 43 | + beforeEach(() => { |
| 44 | + initializeMocks(); |
| 45 | + jest.mocked(useCourseUserPermissions).mockReturnValue({ |
| 46 | + canManageTags: true, |
| 47 | + isLoading: false, |
| 48 | + isAuthzEnabled: false, |
| 49 | + } as ReturnType<typeof useCourseUserPermissions>); |
| 50 | + }); |
| 51 | + |
| 52 | + it('shows the Manage tags action when user can manage tags', async () => { |
| 53 | + renderComponent(); |
| 54 | + expect(await screen.findByText('Taxonomy Alignments')).toBeInTheDocument(); |
| 55 | + |
| 56 | + const taxonomySection = screen.getByText('Taxonomy Alignments').closest('.pgn__hstack') as HTMLElement; |
| 57 | + const toggle = taxonomySection.querySelector('.dropdown button') as HTMLButtonElement; |
| 58 | + expect(toggle).not.toBeNull(); |
| 59 | + fireEvent.click(toggle); |
| 60 | + |
| 61 | + expect(await screen.findByText('Manage tags')).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('hides the Manage tags action when user cannot manage tags', async () => { |
| 65 | + jest.mocked(useCourseUserPermissions).mockReturnValue({ |
| 66 | + canManageTags: false, |
| 67 | + isLoading: false, |
| 68 | + isAuthzEnabled: false, |
| 69 | + } as ReturnType<typeof useCourseUserPermissions>); |
| 70 | + renderComponent(); |
| 71 | + expect(await screen.findByText('Taxonomy Alignments')).toBeInTheDocument(); |
| 72 | + |
| 73 | + const taxonomySection = screen.getByText('Taxonomy Alignments').closest('.pgn__hstack') as HTMLElement; |
| 74 | + expect(taxonomySection.querySelector('.dropdown')).toBeNull(); |
| 75 | + expect(screen.queryByText('Manage tags')).not.toBeInTheDocument(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments