@@ -13,11 +13,11 @@ import {
1313} from '../data/api.mocks' ;
1414import {
1515 getContentLibraryApiUrl , getCreateLibraryBlockUrl , getLibraryCollectionComponentApiUrl , getLibraryPasteClipboardUrl ,
16- getXBlockFieldsApiUrl ,
16+ getXBlockFieldsApiUrl , getLibraryContainerChildrenApiUrl ,
1717} from '../data/api' ;
1818import { mockBroadcastChannel , mockClipboardEmpty , mockClipboardHtml } from '../../generic/data/api.mock' ;
1919import { LibraryProvider } from '../common/context/LibraryContext' ;
20- import AddContentContainer from './AddContentContainer ' ;
20+ import AddContent from './AddContent ' ;
2121import { ComponentEditorModal } from '../components/ComponentEditorModal' ;
2222import editorCmsApi from '../../editors/data/services/cms/api' ;
2323import { ToastActionData } from '../../generic/toast-context' ;
@@ -32,7 +32,7 @@ jest.mock('frontend-components-tinymce-advanced-plugins', () => ({ a11ycheckerCs
3232const { libraryId } = mockContentLibrary ;
3333const render = ( collectionId ?: string ) => {
3434 const params : { libraryId : string , collectionId ?: string } = { libraryId, collectionId } ;
35- return baseRender ( < AddContentContainer /> , {
35+ return baseRender ( < AddContent /> , {
3636 path : '/library/:libraryId/:collectionId?' ,
3737 params,
3838 extraWrapper : ( { children } ) => (
@@ -45,10 +45,25 @@ const render = (collectionId?: string) => {
4545 ) ,
4646 } ) ;
4747} ;
48+ const renderWithUnit = ( unitId : string ) => {
49+ const params : { libraryId : string , unitId ?: string } = { libraryId, unitId } ;
50+ return baseRender ( < AddContent /> , {
51+ path : '/library/:libraryId/:unitId?' ,
52+ params,
53+ extraWrapper : ( { children } ) => (
54+ < LibraryProvider
55+ libraryId = { libraryId }
56+ >
57+ { children }
58+ < ComponentEditorModal />
59+ </ LibraryProvider >
60+ ) ,
61+ } ) ;
62+ } ;
4863let axiosMock : MockAdapter ;
4964let mockShowToast : ( message : string , action ?: ToastActionData | undefined ) => void ;
5065
51- describe ( '<AddContentContainer />' , ( ) => {
66+ describe ( '<AddContent />' , ( ) => {
5267 beforeEach ( ( ) => {
5368 const mocks = initializeMocks ( ) ;
5469 axiosMock = mocks . axiosMock ;
@@ -290,4 +305,71 @@ describe('<AddContentContainer />', () => {
290305 expect ( mockShowToast ) . toHaveBeenCalledWith ( expectedError ) ;
291306 } ) ;
292307 } ) ;
308+
309+ it ( 'should not show collection/unit buttons when create component in container' , async ( ) => {
310+ const unitId = 'lct:orf1:lib1:unit:test-1' ;
311+ renderWithUnit ( unitId ) ;
312+
313+ expect ( await screen . findByRole ( 'button' , { name : 'Text' } ) ) . toBeInTheDocument ( ) ;
314+
315+ expect ( screen . queryByRole ( 'button' , { name : 'Collection' } ) ) . not . toBeInTheDocument ( ) ;
316+ expect ( screen . queryByRole ( 'button' , { name : 'Unit' } ) ) . not . toBeInTheDocument ( ) ;
317+ } ) ;
318+
319+ it ( 'should create a component in unit' , async ( ) => {
320+ const unitId = 'lct:orf1:lib1:unit:test-1' ;
321+ const usageKey = mockXBlockFields . usageKeyNewHtml ;
322+ const createUrl = getCreateLibraryBlockUrl ( libraryId ) ;
323+ const updateBlockUrl = getXBlockFieldsApiUrl ( usageKey ) ;
324+ const linkUrl = getLibraryContainerChildrenApiUrl ( unitId ) ;
325+
326+ axiosMock . onPost ( createUrl ) . reply ( 200 , {
327+ id : usageKey ,
328+ } ) ;
329+ axiosMock . onPost ( updateBlockUrl ) . reply ( 200 , mockXBlockFields . dataHtml ) ;
330+ axiosMock . onPost ( linkUrl ) . reply ( 200 ) ;
331+
332+ renderWithUnit ( unitId ) ;
333+
334+ const textButton = screen . getByRole ( 'button' , { name : / t e x t / i } ) ;
335+ fireEvent . click ( textButton ) ;
336+
337+ // Component should be linked to Unit on saving the changes in the editor.
338+ const saveButton = screen . getByLabelText ( 'Save changes and return to learning context' ) ;
339+ fireEvent . click ( saveButton ) ;
340+
341+ await waitFor ( ( ) => expect ( axiosMock . history . post . length ) . toEqual ( 3 ) ) ;
342+ expect ( axiosMock . history . post [ 0 ] . url ) . toEqual ( createUrl ) ;
343+ expect ( axiosMock . history . post [ 1 ] . url ) . toEqual ( updateBlockUrl ) ;
344+ expect ( axiosMock . history . post [ 2 ] . url ) . toEqual ( linkUrl ) ;
345+ } ) ;
346+
347+ it ( 'should show error on create a component in unit' , async ( ) => {
348+ const unitId = 'lct:orf1:lib1:unit:test-1' ;
349+ const usageKey = mockXBlockFields . usageKeyNewHtml ;
350+ const createUrl = getCreateLibraryBlockUrl ( libraryId ) ;
351+ const updateBlockUrl = getXBlockFieldsApiUrl ( usageKey ) ;
352+ const linkUrl = getLibraryContainerChildrenApiUrl ( unitId ) ;
353+
354+ axiosMock . onPost ( createUrl ) . reply ( 200 , {
355+ id : usageKey ,
356+ } ) ;
357+ axiosMock . onPost ( updateBlockUrl ) . reply ( 200 , mockXBlockFields . dataHtml ) ;
358+ axiosMock . onPost ( linkUrl ) . reply ( 400 ) ;
359+
360+ renderWithUnit ( unitId ) ;
361+
362+ const textButton = screen . getByRole ( 'button' , { name : / t e x t / i } ) ;
363+ fireEvent . click ( textButton ) ;
364+
365+ const saveButton = screen . getByLabelText ( 'Save changes and return to learning context' ) ;
366+ fireEvent . click ( saveButton ) ;
367+
368+ await waitFor ( ( ) => expect ( axiosMock . history . post . length ) . toEqual ( 3 ) ) ;
369+ expect ( axiosMock . history . post [ 0 ] . url ) . toEqual ( createUrl ) ;
370+ expect ( axiosMock . history . post [ 1 ] . url ) . toEqual ( updateBlockUrl ) ;
371+ expect ( axiosMock . history . post [ 2 ] . url ) . toEqual ( linkUrl ) ;
372+
373+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'There was an error linking the content to this container.' ) ;
374+ } ) ;
293375} ) ;
0 commit comments