@@ -21,7 +21,10 @@ import { mockContentSearchConfig, mockGetBlockTypes } from '../../search-manager
2121import { mockClipboardEmpty } from '../../generic/data/api.mock' ;
2222import { LibraryLayout } from '..' ;
2323import { ContentTagsDrawer } from '../../content-tags-drawer' ;
24- import { getLibraryCollectionItemsApiUrl } from '../data/api' ;
24+ import {
25+ getLibraryCollectionItemsApiUrl ,
26+ getLibraryContainersApiUrl ,
27+ } from '../data/api' ;
2528
2629let axiosMock : MockAdapter ;
2730let mockShowToast ;
@@ -406,4 +409,56 @@ describe('<LibraryCollectionPage />', () => {
406409 // Should close sidebar as component was removed
407410 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'library-sidebar' ) ) . not . toBeInTheDocument ( ) ) ;
408411 } ) ;
412+
413+ it ( 'should create a unit inside a collection' , async ( ) => {
414+ await renderLibraryCollectionPage ( ) ;
415+ const unitTitle = 'This is a Test' ;
416+ const containerUrl = getLibraryContainersApiUrl ( mockContentLibrary . libraryId ) ;
417+ axiosMock . onPost ( containerUrl ) . reply ( 200 , {
418+ id : 'unit-1' ,
419+ slug : 'this-is-a-test' ,
420+ title : unitTitle ,
421+ } ) ;
422+ const collectionUrl = getLibraryCollectionItemsApiUrl (
423+ mockContentLibrary . libraryId ,
424+ mockCollection . collectionId ,
425+ ) ;
426+ axiosMock . onPatch ( collectionUrl ) . reply ( 200 ) ;
427+
428+ expect ( await screen . findByRole ( 'heading' ) ) . toBeInTheDocument ( ) ;
429+ expect ( screen . queryByText ( / a d d c o n t e n t / i) ) . not . toBeInTheDocument ( ) ;
430+
431+ // Open Add content sidebar
432+ const newButton = screen . getByRole ( 'button' , { name : / n e w / i } ) ;
433+ fireEvent . click ( newButton ) ;
434+ expect ( screen . getByText ( / a d d c o n t e n t / i) ) . toBeInTheDocument ( ) ;
435+
436+ // Open New unit Modal
437+ const sidebar = screen . getByTestId ( 'library-sidebar' ) ;
438+ const newUnitButton = within ( sidebar ) . getAllByRole ( 'button' , { name : / u n i t / i } ) [ 0 ] ;
439+ fireEvent . click ( newUnitButton ) ;
440+ const unitModalHeading = await screen . findByRole ( 'heading' , { name : / n e w u n i t / i } ) ;
441+ expect ( unitModalHeading ) . toBeInTheDocument ( ) ;
442+
443+ // Fill the form
444+ const createButton = screen . getByRole ( 'button' , { name : / c r e a t e / i } ) ;
445+ const nameField = screen . getByRole ( 'textbox' , { name : / n a m e y o u r u n i t / i } ) ;
446+
447+ fireEvent . change ( nameField , { target : { value : unitTitle } } ) ;
448+ fireEvent . click ( createButton ) ;
449+
450+ // Check success
451+ await waitFor ( ( ) => expect ( axiosMock . history . post . length ) . toBe ( 1 ) ) ;
452+
453+ // Check that the unit was created
454+ expect ( axiosMock . history . post [ 0 ] . url ) . toBe ( containerUrl ) ;
455+ expect ( axiosMock . history . post [ 0 ] . data ) . toContain ( `"display_name":"${ unitTitle } "` ) ;
456+ expect ( axiosMock . history . post [ 0 ] . data ) . toContain ( '"container_type":"unit"' ) ;
457+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Unit created successfully' ) ;
458+
459+ // Check that the unit was added to the collection
460+ expect ( axiosMock . history . patch . length ) . toBe ( 1 ) ;
461+ expect ( axiosMock . history . patch [ 0 ] . url ) . toBe ( collectionUrl ) ;
462+ expect ( axiosMock . history . patch [ 0 ] . data ) . toContain ( '"usage_keys":["unit-1"]' ) ;
463+ } ) ;
409464} ) ;
0 commit comments