From f1dda5e5164a4c8eac7e5e121928ef585bde9eb0 Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Tue, 12 Nov 2024 15:30:02 +0530 Subject: [PATCH] test: add test for lib v2 component --- .../add-component/AddComponent.jsx | 2 +- .../add-component/AddComponent.test.jsx | 35 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/course-unit/add-component/AddComponent.jsx b/src/course-unit/add-component/AddComponent.jsx index 0c494994f1..1a0676239c 100644 --- a/src/course-unit/add-component/AddComponent.jsx +++ b/src/course-unit/add-component/AddComponent.jsx @@ -28,7 +28,7 @@ const AddComponent = ({ blockId, handleCreateNewCourseXBlock }) => { libraryContentKey: selection.usageKey, }); closeAddLibraryContentModal(); - } + }; const handleCreateNewXBlock = (type, moduleName) => { switch (type) { diff --git a/src/course-unit/add-component/AddComponent.test.jsx b/src/course-unit/add-component/AddComponent.test.jsx index 237cd77f46..9cc27acf55 100644 --- a/src/course-unit/add-component/AddComponent.test.jsx +++ b/src/course-unit/add-component/AddComponent.test.jsx @@ -23,6 +23,11 @@ let axiosMock; const blockId = '123'; const handleCreateNewCourseXBlockMock = jest.fn(); +// Mock ComponentPicker to call onComponentSelected on load +jest.mock('../../library-authoring/component-picker', () => ({ + ComponentPicker: (props) => props.onComponentSelected({ usageKey: 'test-usage-key', blockType: 'html' }), +})); + const renderComponent = (props) => render( @@ -61,7 +66,11 @@ describe('', () => { expect(getByRole('heading', { name: messages.title.defaultMessage })).toBeInTheDocument(); Object.keys(componentTemplates).forEach((component) => { const btn = getByRole('button', { - name: new RegExp(`${messages.buttonText.defaultMessage} ${componentTemplates[component].display_name}`, 'i'), + name: new RegExp( + `${componentTemplates[component].type + } ${messages.buttonText.defaultMessage} ${componentTemplates[component].display_name}`, + 'i', + ), }); expect(btn).toBeInTheDocument(); if (component.beta) { @@ -115,7 +124,11 @@ describe('', () => { } return expect(getByRole('button', { - name: new RegExp(`${messages.buttonText.defaultMessage} ${componentTemplates[component].display_name}`, 'i'), + name: new RegExp( + `${componentTemplates[component].type + } ${messages.buttonText.defaultMessage} ${componentTemplates[component].display_name}`, + 'i', + ), })).toBeInTheDocument(); }); }); @@ -180,7 +193,7 @@ describe('', () => { const { getByRole } = renderComponent(); const discussionButton = getByRole('button', { - name: new RegExp(`${messages.buttonText.defaultMessage} Problem`, 'i'), + name: new RegExp(`problem ${messages.buttonText.defaultMessage} Problem`, 'i'), }); userEvent.click(discussionButton); @@ -399,6 +412,22 @@ describe('', () => { }); }); + it('shows library picker on clicking v2 library content btn', async () => { + const { findByRole } = renderComponent(); + const libBtn = await findByRole('button', { + name: new RegExp(`${messages.buttonText.defaultMessage} Library content`, 'i'), + }); + + userEvent.click(libBtn); + expect(handleCreateNewCourseXBlockMock).toHaveBeenCalled(); + expect(handleCreateNewCourseXBlockMock).toHaveBeenCalledWith({ + type: COMPONENT_TYPES.libraryV2, + parentLocator: '123', + category: 'html', + libraryContentKey: 'test-usage-key', + }); + }); + describe('component support label', () => { it('component support label is hidden if component support legend is disabled', async () => { const supportLevels = ['fs', 'ps'];