|
| 1 | +import { courseOutlineIndexMock } from '@src/course-outline/__mocks__'; |
| 2 | +import { initializeMocks, render, screen } from '@src/testUtils'; |
| 3 | +import { userEvent } from '@testing-library/user-event'; |
| 4 | +import mockResult from '@src/library-authoring/__mocks__/library-search.json'; |
| 5 | +import { mockContentSearchConfig, mockSearchResult } from '@src/search-manager/data/api.mock'; |
| 6 | +import { |
| 7 | + mockContentLibrary, |
| 8 | + mockGetCollectionMetadata, |
| 9 | + mockGetContainerMetadata, |
| 10 | + mockGetContentLibraryV2List, |
| 11 | + mockLibraryBlockMetadata, |
| 12 | +} from '@src/library-authoring/data/api.mocks'; |
| 13 | +import { AddSidebar } from './AddSidebar'; |
| 14 | + |
| 15 | +const handleNewSectionSubmit = jest.fn(); |
| 16 | +const handleNewSubsectionSubmit = jest.fn(); |
| 17 | +const handleNewUnitSubmit = jest.fn(); |
| 18 | +const handleAddSectionFromLibrary = { mutateAsync: jest.fn() }; |
| 19 | +const handleAddSubsectionFromLibrary = { mutateAsync: jest.fn() }; |
| 20 | +const handleAddUnitFromLibrary = { mutateAsync: jest.fn() }; |
| 21 | +mockContentSearchConfig.applyMock(); |
| 22 | +mockContentLibrary.applyMock(); |
| 23 | +mockGetCollectionMetadata.applyMock(); |
| 24 | +mockGetContentLibraryV2List.applyMock(); |
| 25 | +mockLibraryBlockMetadata.applyMock(); |
| 26 | +mockGetContainerMetadata.applyMock(); |
| 27 | + |
| 28 | +jest.mock('@src/CourseAuthoringContext', () => ({ |
| 29 | + useCourseAuthoringContext: () => ({ |
| 30 | + courseId: 5, |
| 31 | + courseUsageKey: 'course-usage-key', |
| 32 | + courseDetails: { name: 'Test course' }, |
| 33 | + handleNewSubsectionSubmit, |
| 34 | + handleNewUnitSubmit, |
| 35 | + handleNewSectionSubmit, |
| 36 | + handleAddSectionFromLibrary, |
| 37 | + handleAddSubsectionFromLibrary, |
| 38 | + handleAddUnitFromLibrary, |
| 39 | + }), |
| 40 | +})); |
| 41 | + |
| 42 | +jest.mock('react-redux', () => ({ |
| 43 | + ...jest.requireActual('react-redux'), |
| 44 | + useSelector: jest.fn().mockReturnValue(courseOutlineIndexMock.courseStructure.childInfo.children), |
| 45 | +})); |
| 46 | + |
| 47 | +jest.mock('@src/studio-home/hooks', () => ({ |
| 48 | + useStudioHome: () => ({ |
| 49 | + isLoadingPage: false, |
| 50 | + isFailedLoadingPage: false, |
| 51 | + librariesV2Enabled: true, |
| 52 | + }), |
| 53 | +})); |
| 54 | + |
| 55 | +const renderComponent = () => render(<AddSidebar />); |
| 56 | +const searchResult = { |
| 57 | + ...mockResult, |
| 58 | + results: [ |
| 59 | + { |
| 60 | + ...mockResult.results[0], |
| 61 | + hits: [ |
| 62 | + ...mockResult.results[0].hits.slice(16, 19), |
| 63 | + ], |
| 64 | + }, |
| 65 | + { |
| 66 | + ...mockResult.results[1], |
| 67 | + }, |
| 68 | + ], |
| 69 | +}; |
| 70 | + |
| 71 | +describe('AddSidebar component', () => { |
| 72 | + beforeEach(() => { |
| 73 | + initializeMocks(); |
| 74 | + mockSearchResult({ |
| 75 | + ...searchResult, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders the AddSidebar component without any errors', async () => { |
| 80 | + const user = userEvent.setup(); |
| 81 | + renderComponent(); |
| 82 | + // Check add new tab content |
| 83 | + expect(await screen.findByRole('button', { name: 'Section' })).toBeInTheDocument(); |
| 84 | + expect(await screen.findByRole('button', { name: 'Subsection' })).toBeInTheDocument(); |
| 85 | + expect(await screen.findByRole('button', { name: 'Unit' })).toBeInTheDocument(); |
| 86 | + expect(await screen.findByRole('tab', { name: 'Add New' })).toBeInTheDocument(); |
| 87 | + const existingTab = await screen.findByRole('tab', { name: 'Add Existing' }); |
| 88 | + expect(existingTab).toBeInTheDocument(); |
| 89 | + |
| 90 | + // Check existing tab content |
| 91 | + await user.click(existingTab); |
| 92 | + expect(await screen.findByRole('button', { name: 'All libraries' })).toBeInTheDocument(); |
| 93 | + expect(await screen.findByRole('button', { name: 'See more' })).toBeInTheDocument(); |
| 94 | + expect(await screen.findByRole('search')).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('show hide extra filters', async () => { |
| 98 | + const user = userEvent.setup(); |
| 99 | + renderComponent(); |
| 100 | + const existingTab = await screen.findByRole('tab', { name: 'Add Existing' }); |
| 101 | + expect(existingTab).toBeInTheDocument(); |
| 102 | + await user.click(existingTab); |
| 103 | + const toggleBtn = await screen.findByRole('button', { name: 'See more' }); |
| 104 | + expect(toggleBtn).toBeInTheDocument(); |
| 105 | + // hidden by default |
| 106 | + expect(screen.queryByRole('button', { name: 'Type' })).not.toBeInTheDocument(); |
| 107 | + // show when clicked |
| 108 | + await user.click(toggleBtn); |
| 109 | + expect(await screen.findByRole('button', { name: 'Type' })).toBeInTheDocument(); |
| 110 | + expect(await screen.findByRole('button', { name: 'Tags' })).toBeInTheDocument(); |
| 111 | + expect(await screen.findByTitle('Sort search results')).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('calls appropriate handlers on new button click', async () => { |
| 115 | + const user = userEvent.setup(); |
| 116 | + renderComponent(); |
| 117 | + |
| 118 | + // Validate handler for adding section, subsection and unit |
| 119 | + const section = await screen.findByRole('button', { name: 'Section' }); |
| 120 | + const subsection = await screen.findByRole('button', { name: 'Subsection' }); |
| 121 | + const unit = await screen.findByRole('button', { name: 'Unit' }); |
| 122 | + await user.click(section); |
| 123 | + expect(handleNewSectionSubmit).toHaveBeenCalled(); |
| 124 | + await user.click(subsection); |
| 125 | + expect(handleNewSubsectionSubmit).toHaveBeenCalled(); |
| 126 | + await user.click(unit); |
| 127 | + expect(handleNewUnitSubmit).toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('calls appropriate handlers on existing button click', async () => { |
| 131 | + const user = userEvent.setup(); |
| 132 | + const sectionList = courseOutlineIndexMock.courseStructure.childInfo.children; |
| 133 | + const lastSection = sectionList[3]; |
| 134 | + const lastSubsection = lastSection.childInfo.children[0]; |
| 135 | + renderComponent(); |
| 136 | + // Check existing tab content |
| 137 | + await user.click(await screen.findByRole('tab', { name: 'Add Existing' })); |
| 138 | + |
| 139 | + // Validate handler for adding section, subsection and unit |
| 140 | + const addBtns = await screen.findAllByRole('button', { name: 'Add' }); |
| 141 | + // first one is unit as per mock |
| 142 | + await user.click(addBtns[0]); |
| 143 | + expect(handleAddUnitFromLibrary.mutateAsync).toHaveBeenCalledWith({ |
| 144 | + type: 'library_v2', |
| 145 | + category: 'vertical', |
| 146 | + parentLocator: lastSubsection.id, |
| 147 | + libraryContentKey: searchResult.results[0].hits[0].usage_key, |
| 148 | + }); |
| 149 | + // second one is subsection as per mock |
| 150 | + await user.click(addBtns[1]); |
| 151 | + expect(handleAddSubsectionFromLibrary.mutateAsync).toHaveBeenCalledWith({ |
| 152 | + type: 'library_v2', |
| 153 | + category: 'sequential', |
| 154 | + parentLocator: lastSection.id, |
| 155 | + libraryContentKey: searchResult.results[0].hits[1].usage_key, |
| 156 | + }); |
| 157 | + // third one is section as per mock |
| 158 | + await user.click(addBtns[2]); |
| 159 | + expect(handleAddSectionFromLibrary.mutateAsync).toHaveBeenCalledWith({ |
| 160 | + type: 'library_v2', |
| 161 | + category: 'chapter', |
| 162 | + parentLocator: 'course-usage-key', |
| 163 | + libraryContentKey: searchResult.results[0].hits[2].usage_key, |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments