|
| 1 | +import { initializeMocks, render, screen } from '@src/testUtils'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { UnitSidebar } from './UnitInfoSidebar'; |
| 4 | + |
| 5 | +// Mocks |
| 6 | +jest.mock('@src/course-outline/data/apiHooks', () => ({ |
| 7 | + useCourseItemData: jest.fn(), |
| 8 | + courseOutlineQueryKeys: { courseItemId: (id: string) => ['courseItem', id] }, |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('../OutlineSidebarContext', () => ({ |
| 12 | + useOutlineSidebarContext: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('@src/CourseAuthoringContext', () => ({ |
| 16 | + useCourseAuthoringContext: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('./PublishButon', () => ({ PublishButon: ({ onClick }: any) => <button type="button" onClick={onClick}>Publish</button> })); |
| 20 | +jest.mock('./InfoSection', () => ({ InfoSection: ({ itemId }: any) => <div>InfoSection:{itemId}</div> })); |
| 21 | +jest.mock('@src/course-unit/unit-sidebar/unit-info/GenericUnitInfoSettings', () => ({ GenericUnitInfoSettings: () => <div>GenericUnitInfoSettings</div> })); |
| 22 | +jest.mock('@src/generic/block-type-utils', () => ({ getItemIcon: () => () => null })); |
| 23 | +jest.mock('@src/course-unit/xblock-container-iframe', () => function XBlockIframe() { |
| 24 | + return <div>XBlockIframe</div>; |
| 25 | +}); |
| 26 | +jest.mock('@src/generic/hooks/context/iFrameContext', () => ({ IframeProvider: ({ children }: any) => <div>{children}</div> })); |
| 27 | + |
| 28 | +const apiHooks = jest.requireMock('@src/course-outline/data/apiHooks') as any; |
| 29 | +const outlineContext = jest.requireMock('../OutlineSidebarContext') as any; |
| 30 | +const authoring = jest.requireMock('@src/CourseAuthoringContext') as any; |
| 31 | + |
| 32 | +describe('UnitSidebar', () => { |
| 33 | + beforeEach(() => { |
| 34 | + initializeMocks(); |
| 35 | + outlineContext.useOutlineSidebarContext.mockReturnValue({ selectedContainerState: { sectionId: 's1', subsectionId: 'ss1' }, clearSelection: jest.fn() }); |
| 36 | + authoring.useCourseAuthoringContext.mockReturnValue({ openPublishModal: jest.fn(), getUnitUrl: (id: string) => `/unit/${id}`, courseId: '5' }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('renders title and info tab by default', () => { |
| 40 | + apiHooks.useCourseItemData.mockReturnValue({ |
| 41 | + data: { |
| 42 | + displayName: 'Unit 1', hasChanges: false, category: 'vertical', id: 'unit-1', |
| 43 | + }, |
| 44 | + isPending: false, |
| 45 | + }); |
| 46 | + render(<UnitSidebar unitId="unit-1" />); |
| 47 | + expect(screen.getByText('Unit 1')).toBeInTheDocument(); |
| 48 | + expect(screen.getByText('InfoSection:unit-1')).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('shows publish button and triggers openPublishModal when unit has changes', async () => { |
| 52 | + const user = userEvent.setup(); |
| 53 | + const openPublishModal = jest.fn(); |
| 54 | + authoring.useCourseAuthoringContext.mockReturnValue({ openPublishModal, getUnitUrl: (id: string) => `/unit/${id}`, courseId: '5' }); |
| 55 | + apiHooks.useCourseItemData.mockReturnValue({ |
| 56 | + data: { |
| 57 | + displayName: 'Unit 2', hasChanges: true, category: 'vertical', id: 'unit-2', |
| 58 | + }, |
| 59 | + isPending: false, |
| 60 | + }); |
| 61 | + |
| 62 | + render(<UnitSidebar unitId="unit-2" />); |
| 63 | + expect(screen.getByText('Publish')).toBeInTheDocument(); |
| 64 | + await user.click(screen.getByText('Publish')); |
| 65 | + expect(openPublishModal).toHaveBeenCalledWith({ value: expect.any(Object), sectionId: 's1', subsectionId: 'ss1' }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('switches to preview tab and shows iframe', async () => { |
| 69 | + const user = userEvent.setup(); |
| 70 | + apiHooks.useCourseItemData.mockReturnValue({ |
| 71 | + data: { |
| 72 | + displayName: 'Unit 3', hasChanges: false, category: 'vertical', id: 'unit-3', |
| 73 | + }, |
| 74 | + isPending: false, |
| 75 | + }); |
| 76 | + render(<UnitSidebar unitId="unit-3" />); |
| 77 | + await user.click(screen.getByRole('tab', { name: /Preview/i })); |
| 78 | + expect(screen.getByText('XBlockIframe')).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('shows settings tab content when selected', async () => { |
| 82 | + const user = userEvent.setup(); |
| 83 | + apiHooks.useCourseItemData.mockReturnValue({ |
| 84 | + data: { |
| 85 | + displayName: 'Unit 4', hasChanges: false, category: 'vertical', id: 'unit-4', visibilityState: undefined, discussionEnabled: false, userPartitionInfo: null, |
| 86 | + }, |
| 87 | + isPending: false, |
| 88 | + }); |
| 89 | + render(<UnitSidebar unitId="unit-4" />); |
| 90 | + await user.click(screen.getByRole('tab', { name: /Settings/i })); |
| 91 | + expect(screen.getByText('GenericUnitInfoSettings')).toBeInTheDocument(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments