@@ -55,6 +55,7 @@ describe('<LibraryCollectionPage />', () => {
5555 const mocks = initializeMocks ( ) ;
5656 axiosMock = mocks . axiosMock ;
5757 mockShowToast = mocks . mockShowToast ;
58+ jest . useFakeTimers ( ) ;
5859 fetchMock . mockReset ( ) ;
5960
6061 // The Meilisearch client-side API uses fetch, not Axios.
@@ -89,6 +90,10 @@ describe('<LibraryCollectionPage />', () => {
8990 } ) ;
9091 } ) ;
9192
93+ afterEach ( ( ) => {
94+ jest . runOnlyPendingTimers ( ) ;
95+ jest . useRealTimers ( ) ;
96+ } ) ;
9297 const renderLibraryCollectionPage = async ( collectionId ?: string , libraryId ?: string ) => {
9398 const libId = libraryId || mockContentLibrary . libraryId ;
9499 const colId = collectionId || mockCollection . collectionId ;
@@ -354,28 +359,40 @@ describe('<LibraryCollectionPage />', () => {
354359 expect ( screen . getByText ( / n o m a t c h i n g c o m p o n e n t s / i) ) . toBeInTheDocument ( ) ;
355360 } ) ;
356361
357- it ( 'should remove component from collection and hides sidebar' , async ( ) => {
362+ it ( 'should remove unit from collection and hides sidebar' , async ( ) => {
358363 const url = getLibraryCollectionItemsApiUrl (
359364 mockContentLibrary . libraryId ,
360365 mockCollection . collectionId ,
361366 ) ;
362367 axiosMock . onDelete ( url ) . reply ( 204 ) ;
363- const displayName = 'Introduction to Testing ' ;
368+ const displayName = 'Test Unit ' ;
364369 await renderLibraryCollectionPage ( ) ;
365370
371+ // Wait for the unit cards to load
372+ await waitFor ( ( ) => expect ( screen . getAllByTestId ( 'container-card-menu-toggle' ) . length ) . toBeGreaterThan ( 0 ) ) ;
373+
366374 // open sidebar
367375 fireEvent . click ( await screen . findByText ( displayName ) ) ;
376+
377+ // advance timers so the sidebar opens
378+ jest . advanceTimersByTime ( 500 ) ;
379+
368380 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'library-sidebar' ) ) . toBeInTheDocument ( ) ) ;
369381
370- const menuBtns = await screen . findAllByRole ( 'button' , { name : 'Component actions menu' } ) ;
371- // open menu
372- fireEvent . click ( menuBtns [ 0 ] ) ;
382+ // Open menu
383+ fireEvent . click ( ( await screen . findAllByTestId ( 'container-card-menu-toggle' ) ) [ 0 ] ) ;
384+
385+ // Click remove to collection
386+ fireEvent . click ( screen . getByRole ( 'button' , { name : 'Remove from collection' } ) ) ;
373387
374- fireEvent . click ( await screen . findByText ( 'Remove from collection' ) ) ;
375388 await waitFor ( ( ) => {
376389 expect ( axiosMock . history . delete . length ) . toEqual ( 1 ) ;
377390 } ) ;
378391 expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Item successfully removed' ) ;
392+
393+ // advance timers so the sidebar close logic executes
394+ jest . advanceTimersByTime ( 500 ) ;
395+
379396 // Should close sidebar as component was removed
380397 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'library-sidebar' ) ) . not . toBeInTheDocument ( ) ) ;
381398 } ) ;
@@ -399,7 +416,7 @@ describe('<LibraryCollectionPage />', () => {
399416 expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Failed to remove item' ) ;
400417 } ) ;
401418
402- it ( 'should remove unit from collection and hides sidebar' , async ( ) => {
419+ it . only ( 'should remove unit from collection and hides sidebar' , async ( ) => {
403420 const url = getLibraryCollectionItemsApiUrl (
404421 mockContentLibrary . libraryId ,
405422 mockCollection . collectionId ,
@@ -413,6 +430,8 @@ describe('<LibraryCollectionPage />', () => {
413430
414431 // open sidebar
415432 fireEvent . click ( await screen . findByText ( displayName ) ) ;
433+ // ⏩ let the 500ms pass in test-land
434+ jest . advanceTimersByTime ( 500 ) ;
416435 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'library-sidebar' ) ) . toBeInTheDocument ( ) ) ;
417436
418437 // Open menu
@@ -425,6 +444,8 @@ describe('<LibraryCollectionPage />', () => {
425444 expect ( axiosMock . history . delete . length ) . toEqual ( 1 ) ;
426445 } ) ;
427446 expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Item successfully removed' ) ;
447+ // ⏩ let the 500ms pass in test-land
448+ jest . advanceTimersByTime ( 500 ) ;
428449 // Should close sidebar as component was removed
429450 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'library-sidebar' ) ) . not . toBeInTheDocument ( ) ) ;
430451 } ) ;
0 commit comments