11import userEvent from '@testing-library/user-event' ;
2+ import { act } from 'react' ;
23import {
34 fireEvent ,
45 initializeMocks ,
@@ -18,14 +19,11 @@ import { mockContentSearchConfig, mockGetBlockTypes } from '../../search-manager
1819import { mockClipboardEmpty } from '../../generic/data/api.mock' ;
1920import LibraryLayout from '../LibraryLayout' ;
2021import { getLibraryContainerChildrenApiUrl } from '../data/api' ;
21- import { RequestStatus } from '../../data/constants' ;
22- import { closestCorners } from '@dnd-kit/core' ;
2322import { ToastActionData } from '../../generic/toast-context' ;
24- import { act } from 'react' ;
2523
2624const path = '/library/:libraryId/*' ;
2725const libraryTitle = mockContentLibrary . libraryData . title ;
28- let axiosMock : import ( " axios-mock-adapter/types" ) ;
26+ let axiosMock : import ( ' axios-mock-adapter/types' ) ;
2927let mockShowToast : ( message : string , action ?: ToastActionData | undefined ) => void ;
3028
3129mockClipboardEmpty . applyMock ( ) ;
@@ -37,16 +35,15 @@ mockContentLibrary.applyMock();
3735mockXBlockFields . applyMock ( ) ;
3836mockLibraryBlockMetadata . applyMock ( ) ;
3937
38+ const closestCenter = jest . fn ( ) ;
4039jest . mock ( '@dnd-kit/core' , ( ) => ( {
4140 ...jest . requireActual ( '@dnd-kit/core' ) ,
4241 // Since jsdom (used by jest) does not support getBoundingClientRect function
4342 // which is required for drag-n-drop calculations, we mock closestCorners fn
4443 // from dnd-kit to return collided elements as per the test. This allows us to
4544 // test all drag-n-drop handlers.
46- closestCorners : jest . fn ( ) ,
45+ closestCenter : ( ) => closestCenter ( ) ,
4746} ) ) ;
48- // eslint-disable-next-line no-promise-executor-return
49- const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
5047
5148describe ( '<LibraryUnitPage />' , ( ) => {
5249 beforeEach ( ( ) => {
@@ -139,12 +136,29 @@ describe('<LibraryUnitPage />', () => {
139136 axiosMock
140137 . onPatch ( getLibraryContainerChildrenApiUrl ( mockGetContainerMetadata . containerId ) )
141138 . reply ( 200 ) ;
142- closestCorners . mockReturnValue ( [ { id : "lb:org1:Demo_course:html:text-1" } ] ) ;
139+ closestCenter . mockReturnValue ( [ { id : 'lb:org1:Demo_course:html:text-1' } ] ) ;
140+ await act ( async ( ) => {
141+ fireEvent . keyDown ( firstDragHandle , { code : 'Space' } ) ;
142+ } ) ;
143+ await act ( async ( ) => {
144+ fireEvent . keyDown ( firstDragHandle , { code : 'Space' } ) ;
145+ } ) ;
146+ await waitFor ( ( ) => expect ( mockShowToast ) . toHaveBeenLastCalledWith ( 'Order updated' ) ) ;
147+ } ) ;
148+
149+ it ( 'should show toast error message on update order failure' , async ( ) => {
150+ renderLibraryUnitPage ( ) ;
151+ const firstDragHandle = ( await screen . findAllByRole ( 'button' , { name : 'Drag to reorder' } ) ) [ 0 ] ;
152+ axiosMock
153+ . onPatch ( getLibraryContainerChildrenApiUrl ( mockGetContainerMetadata . containerId ) )
154+ . reply ( 500 ) ;
155+ closestCenter . mockReturnValue ( [ { id : 'lb:org1:Demo_course:html:text-1' } ] ) ;
143156 await act ( async ( ) => {
144157 fireEvent . keyDown ( firstDragHandle , { code : 'Space' } ) ;
145- await sleep ( 1 ) ;
146- fireEvent . keyUp ( firstDragHandle , { code : 'Space' } ) ;
147- } )
148- await waitFor ( ( ) => expect ( mockShowToast ) . toHaveBeenLastCalledWith ( 'test' ) ) ;
158+ } ) ;
159+ await act ( async ( ) => {
160+ fireEvent . keyDown ( firstDragHandle , { code : 'Space' } ) ;
161+ } ) ;
162+ await waitFor ( ( ) => expect ( mockShowToast ) . toHaveBeenLastCalledWith ( 'Failed to update components order' ) ) ;
149163 } ) ;
150164} ) ;
0 commit comments