@@ -28,9 +28,20 @@ const { libraryId } = mockContentLibrary;
2828const onClose = jest . fn ( ) ;
2929let mockShowToast : ( message : string ) => void ;
3030
31- const render = ( ) => baseRender ( < PickLibraryContentModal isOpen onClose = { onClose } /> , {
32- path : '/library/:libraryId/collection/:collectionId/*' ,
33- params : { libraryId, collectionId : 'collectionId' } ,
31+ const mockAddItemsToCollection = jest . fn ( ) ;
32+ const mockAddComponentsToContainer = jest . fn ( ) ;
33+ jest . spyOn ( api , 'addItemsToCollection' ) . mockImplementation ( mockAddItemsToCollection ) ;
34+ jest . spyOn ( api , 'addComponentsToContainer' ) . mockImplementation ( mockAddComponentsToContainer ) ;
35+
36+ const render = ( context : 'collection' | 'unit' ) => baseRender ( < PickLibraryContentModal isOpen onClose = { onClose } /> , {
37+ path : context === 'collection'
38+ ? '/library/:libraryId/collection/:collectionId/*'
39+ : '/library/:libraryId/container/:unitId/*' ,
40+ params : {
41+ libraryId,
42+ ...( context === 'collection' && { collectionId : 'collectionId' } ) ,
43+ ...( context === 'unit' && { unitId : 'unitId' } ) ,
44+ } ,
3445 extraWrapper : ( { children } ) => (
3546 < LibraryProvider
3647 libraryId = { libraryId }
@@ -46,62 +57,80 @@ describe('<PickLibraryContentModal />', () => {
4657 const mocks = initializeMocks ( ) ;
4758 mockShowToast = mocks . mockShowToast ;
4859 mocks . axiosMock . onGet ( getStudioHomeApiUrl ( ) ) . reply ( 200 , studioHomeMock ) ;
60+ jest . clearAllMocks ( ) ;
4961 } ) ;
5062
51- it ( 'can pick components from the modal' , async ( ) => {
52- const mockAddItemsToCollection = jest . fn ( ) ;
53- jest . spyOn ( api , 'addItemsToCollection' ) . mockImplementation ( mockAddItemsToCollection ) ;
54-
55- render ( ) ;
56-
57- // Wait for the content library to load
58- await waitFor ( ( ) => {
59- expect ( screen . getByText ( 'Test Library' ) ) . toBeInTheDocument ( ) ;
60- expect ( screen . queryAllByText ( 'Introduction to Testing' ) [ 0 ] ) . toBeInTheDocument ( ) ;
61- } ) ;
62-
63- // Select the first component
64- fireEvent . click ( screen . queryAllByRole ( 'button' , { name : 'Select' } ) [ 0 ] ) ;
65- expect ( await screen . findByText ( '1 Selected Component' ) ) . toBeInTheDocument ( ) ;
66-
67- fireEvent . click ( screen . queryAllByRole ( 'button' , { name : 'Add to Collection' } ) [ 0 ] ) ;
68-
69- await waitFor ( ( ) => {
70- expect ( mockAddItemsToCollection ) . toHaveBeenCalledWith (
71- libraryId ,
72- 'collectionId' ,
73- [ 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ] ,
74- ) ;
63+ [ 'collection' as const , 'unit' as const ] . forEach ( ( context ) => {
64+ it ( `can pick components from the modal (${ context } )` , async ( ) => {
65+ render ( context ) ;
66+
67+ // Wait for the content library to load
68+ await waitFor ( ( ) => {
69+ expect ( screen . getByText ( 'Test Library' ) ) . toBeInTheDocument ( ) ;
70+ expect ( screen . queryAllByText ( 'Introduction to Testing' ) [ 0 ] ) . toBeInTheDocument ( ) ;
71+ } ) ;
72+
73+ // Select the first component
74+ fireEvent . click ( screen . queryAllByRole ( 'button' , { name : 'Select' } ) [ 0 ] ) ;
75+ expect ( await screen . findByText ( '1 Selected Component' ) ) . toBeInTheDocument ( ) ;
76+
77+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a d d t o .* / i } ) ) ;
78+
79+ await waitFor ( ( ) => {
80+ if ( context === 'collection' ) {
81+ expect ( mockAddItemsToCollection ) . toHaveBeenCalledWith (
82+ libraryId ,
83+ 'collectionId' ,
84+ [ 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ] ,
85+ ) ;
86+ } else {
87+ expect ( mockAddComponentsToContainer ) . toHaveBeenCalledWith (
88+ 'unitId' ,
89+ [ 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ] ,
90+ ) ;
91+ }
92+ } ) ;
7593 expect ( onClose ) . toHaveBeenCalled ( ) ;
7694 expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Content linked successfully.' ) ;
7795 } ) ;
78- } ) ;
79-
80- it ( 'show error when api call fails' , async ( ) => {
81- const mockAddItemsToCollection = jest . fn ( ) . mockRejectedValue ( new Error ( 'Failed to add components' ) ) ;
82- jest . spyOn ( api , 'addItemsToCollection' ) . mockImplementation ( mockAddItemsToCollection ) ;
83- render ( ) ;
84-
85- // Wait for the content library to load
86- await waitFor ( ( ) => {
87- expect ( screen . getByText ( 'Test Library' ) ) . toBeInTheDocument ( ) ;
88- expect ( screen . queryAllByText ( 'Introduction to Testing' ) [ 0 ] ) . toBeInTheDocument ( ) ;
89- } ) ;
90-
91- // Select the first component
92- fireEvent . click ( screen . queryAllByRole ( 'button' , { name : 'Select' } ) [ 0 ] ) ;
93- expect ( await screen . findByText ( '1 Selected Component' ) ) . toBeInTheDocument ( ) ;
94-
95- fireEvent . click ( screen . queryAllByRole ( 'button' , { name : 'Add to Collection' } ) [ 0 ] ) ;
9696
97- await waitFor ( ( ) => {
98- expect ( mockAddItemsToCollection ) . toHaveBeenCalledWith (
99- libraryId ,
100- 'collectionId' ,
101- [ 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ] ,
102- ) ;
97+ it ( `show error when api call fails (${ context } )` , async ( ) => {
98+ if ( context === 'collection' ) {
99+ mockAddItemsToCollection . mockRejectedValueOnce ( new Error ( 'Error' ) ) ;
100+ } else {
101+ mockAddComponentsToContainer . mockRejectedValueOnce ( new Error ( 'Error' ) ) ;
102+ }
103+ render ( context ) ;
104+
105+ // Wait for the content library to load
106+ await waitFor ( ( ) => {
107+ expect ( screen . getByText ( 'Test Library' ) ) . toBeInTheDocument ( ) ;
108+ expect ( screen . queryAllByText ( 'Introduction to Testing' ) [ 0 ] ) . toBeInTheDocument ( ) ;
109+ } ) ;
110+
111+ // Select the first component
112+ fireEvent . click ( screen . queryAllByRole ( 'button' , { name : 'Select' } ) [ 0 ] ) ;
113+ expect ( await screen . findByText ( '1 Selected Component' ) ) . toBeInTheDocument ( ) ;
114+
115+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a d d t o .* / i } ) ) ;
116+
117+ await waitFor ( ( ) => {
118+ if ( context === 'collection' ) {
119+ expect ( mockAddItemsToCollection ) . toHaveBeenCalledWith (
120+ libraryId ,
121+ 'collectionId' ,
122+ [ 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ] ,
123+ ) ;
124+ } else {
125+ expect ( mockAddComponentsToContainer ) . toHaveBeenCalledWith (
126+ 'unitId' ,
127+ [ 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ] ,
128+ ) ;
129+ }
130+ } ) ;
103131 expect ( onClose ) . toHaveBeenCalled ( ) ;
104- expect ( mockShowToast ) . toHaveBeenCalledWith ( 'There was an error linking the content to this collection.' ) ;
132+ const name = context === 'collection' ? 'collection' : 'container' ;
133+ expect ( mockShowToast ) . toHaveBeenCalledWith ( `There was an error linking the content to this ${ name } .` ) ;
105134 } ) ;
106135 } ) ;
107136} ) ;
0 commit comments