@@ -221,46 +221,6 @@ describe('<ComponentPicker />', () => {
221221 } , '*' ) ;
222222 } ) ;
223223
224- it ( 'should pick component inside a collection using the sidebar' , async ( ) => {
225- render ( < ComponentPicker /> ) ;
226-
227- expect ( await screen . findByText ( 'Test Library 1' ) ) . toBeInTheDocument ( ) ;
228- fireEvent . click ( screen . getByDisplayValue ( / l i b : s a m p l e t a x o n o m y o r g 1 : t l 1 / i) ) ;
229-
230- // Wait for the content library to load
231- await screen . findByText ( / C h a n g e L i b r a r y / i) ;
232- expect ( await screen . findByText ( 'Test Library 1' ) ) . toBeInTheDocument ( ) ;
233-
234- // Click on the collection card to open the sidebar
235- fireEvent . click ( screen . queryAllByText ( 'Collection 1' ) [ 0 ] ) ;
236-
237- const sidebar = await screen . findByTestId ( 'library-sidebar' ) ;
238-
239- // Mock the collection search result
240- mockSearchResult ( mockCollectionResult ) ;
241-
242- // Click the add component from the component card
243- fireEvent . click ( within ( sidebar ) . getByRole ( 'button' , { name : 'Open' } ) ) ;
244-
245- // Wait for the collection to load
246- await screen . findByText ( / B a c k t o L i b r a r y / i) ;
247- await screen . findByText ( 'Introduction to Testing' ) ;
248-
249- // Click on the collection card to open the sidebar
250- fireEvent . click ( screen . getByText ( 'Introduction to Testing' ) ) ;
251-
252- const collectionSidebar = await screen . findByTestId ( 'library-sidebar' ) ;
253-
254- // Click the add component from the collection sidebar
255- fireEvent . click ( within ( collectionSidebar ) . getByRole ( 'button' , { name : 'Add to Course' } ) ) ;
256-
257- expect ( postMessageSpy ) . toHaveBeenCalledWith ( {
258- usageKey : 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ,
259- type : 'pickerComponentSelected' ,
260- category : 'html' ,
261- } , '*' ) ;
262- } ) ;
263-
264224 it ( 'should return to library selection' , async ( ) => {
265225 render ( < ComponentPicker /> ) ;
266226
@@ -428,3 +388,69 @@ describe('<ComponentPicker />', () => {
428388 expect ( screen . queryByText ( / n e v e r p u b l i s h e d / i) ) . not . toBeInTheDocument ( ) ;
429389 } ) ;
430390} ) ;
391+
392+ describe ( '<ComponentPicker /> with collection' , ( ) => {
393+ beforeEach ( ( ) => {
394+ initializeMocks ( ) ;
395+ postMessageSpy = jest . spyOn ( window . parent , 'postMessage' ) ;
396+
397+ mockSearchResult ( { ...mockResult } ) ;
398+ } ) ;
399+
400+ it ( 'should pick component inside a collection using the sidebar' , async ( ) => {
401+ jest . useFakeTimers ( ) ; // ✅ enable fake timers
402+
403+ render ( < ComponentPicker /> ) ;
404+
405+ expect ( await screen . findByText ( 'Test Library 1' ) ) . toBeInTheDocument ( ) ;
406+ fireEvent . click ( screen . getByDisplayValue ( / l i b : s a m p l e t a x o n o m y o r g 1 : t l 1 / i) ) ;
407+
408+ // Wait for the content library to load
409+ await screen . findByText ( / C h a n g e L i b r a r y / i) ;
410+ expect ( await screen . findByText ( 'Test Library 1' ) ) . toBeInTheDocument ( ) ;
411+
412+ // Click on the collection card to open the sidebar
413+ const collections = await screen . findAllByText ( 'Collection 1' ) ; // ✅ wait until it renders
414+ fireEvent . click ( collections [ 0 ] ) ;
415+
416+ // ⏩ let the 500ms single-click timer finish
417+ jest . advanceTimersByTime ( 500 ) ;
418+
419+ const sidebar = await screen . findByTestId ( 'library-sidebar' ) ;
420+
421+ // Mock the collection search result
422+ mockSearchResult ( mockCollectionResult ) ;
423+
424+ // Click the add component from the component card
425+ fireEvent . click ( within ( sidebar ) . getByRole ( 'button' , { name : 'Open' } ) ) ;
426+
427+ // ⏩ advance timers again in case sidebar open uses timeout
428+ jest . advanceTimersByTime ( 500 ) ;
429+
430+ // Wait for the collection to load
431+ await screen . findByText ( / B a c k t o L i b r a r y / i) ;
432+ await screen . findByText ( 'Introduction to Testing' ) ;
433+
434+ // Click on the collection card to open the sidebar
435+ fireEvent . click ( screen . getByText ( 'Introduction to Testing' ) ) ;
436+
437+ // ⏩ advance timers again for delayed sidebar open
438+ jest . advanceTimersByTime ( 500 ) ;
439+
440+ const collectionSidebar = await screen . findByTestId ( 'library-sidebar' ) ;
441+
442+ // Click the add component from the collection sidebar
443+ fireEvent . click ( within ( collectionSidebar ) . getByRole ( 'button' , { name : 'Add to Course' } ) ) ;
444+
445+ expect ( postMessageSpy ) . toHaveBeenCalledWith (
446+ {
447+ usageKey : 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd' ,
448+ type : 'pickerComponentSelected' ,
449+ category : 'html' ,
450+ } ,
451+ '*' ,
452+ ) ;
453+
454+ jest . useRealTimers ( ) ; // ✅ restore real timers
455+ } ) ;
456+ } ) ;
0 commit comments