11import userEvent from '@testing-library/user-event' ;
22import {
3+ fireEvent ,
34 initializeMocks ,
45 render ,
56 screen ,
@@ -16,9 +17,16 @@ import {
1617import { mockContentSearchConfig , mockGetBlockTypes } from '../../search-manager/data/api.mock' ;
1718import { mockClipboardEmpty } from '../../generic/data/api.mock' ;
1819import LibraryLayout from '../LibraryLayout' ;
20+ import { getLibraryContainerChildrenApiUrl } from '../data/api' ;
21+ import { RequestStatus } from '../../data/constants' ;
22+ import { closestCorners } from '@dnd-kit/core' ;
23+ import { ToastActionData } from '../../generic/toast-context' ;
24+ import { act } from 'react' ;
1925
2026const path = '/library/:libraryId/*' ;
2127const libraryTitle = mockContentLibrary . libraryData . title ;
28+ let axiosMock : import ( "axios-mock-adapter/types" ) ;
29+ let mockShowToast : ( message : string , action ?: ToastActionData | undefined ) => void ;
2230
2331mockClipboardEmpty . applyMock ( ) ;
2432mockGetContainerMetadata . applyMock ( ) ;
@@ -29,9 +37,22 @@ mockContentLibrary.applyMock();
2937mockXBlockFields . applyMock ( ) ;
3038mockLibraryBlockMetadata . applyMock ( ) ;
3139
40+ jest . mock ( '@dnd-kit/core' , ( ) => ( {
41+ ...jest . requireActual ( '@dnd-kit/core' ) ,
42+ // Since jsdom (used by jest) does not support getBoundingClientRect function
43+ // which is required for drag-n-drop calculations, we mock closestCorners fn
44+ // from dnd-kit to return collided elements as per the test. This allows us to
45+ // test all drag-n-drop handlers.
46+ closestCorners : jest . fn ( ) ,
47+ } ) ) ;
48+ // eslint-disable-next-line no-promise-executor-return
49+ const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
50+
3251describe ( '<LibraryUnitPage />' , ( ) => {
3352 beforeEach ( ( ) => {
34- initializeMocks ( ) ;
53+ const mocks = initializeMocks ( ) ;
54+ axiosMock = mocks . axiosMock ;
55+ mockShowToast = mocks . mockShowToast ;
3556 } ) ;
3657
3758 const renderLibraryUnitPage = ( unitId ?: string , libraryId ?: string ) => {
@@ -111,4 +132,19 @@ describe('<LibraryUnitPage />', () => {
111132 userEvent . click ( closeButton ) ;
112133 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'library-sidebar' ) ) . not . toBeInTheDocument ( ) ) ;
113134 } ) ;
135+
136+ it ( 'should call update order api on dragging component' , async ( ) => {
137+ renderLibraryUnitPage ( ) ;
138+ const firstDragHandle = ( await screen . findAllByRole ( 'button' , { name : 'Drag to reorder' } ) ) [ 0 ] ;
139+ axiosMock
140+ . onPatch ( getLibraryContainerChildrenApiUrl ( mockGetContainerMetadata . containerId ) )
141+ . reply ( 200 ) ;
142+ closestCorners . mockReturnValue ( [ { id : "lb:org1:Demo_course:html:text-1" } ] ) ;
143+ await act ( async ( ) => {
144+ fireEvent . keyDown ( firstDragHandle , { code : 'Space' } ) ;
145+ await sleep ( 1 ) ;
146+ fireEvent . keyUp ( firstDragHandle , { code : 'Space' } ) ;
147+ } )
148+ await waitFor ( ( ) => expect ( mockShowToast ) . toHaveBeenLastCalledWith ( 'test' ) ) ;
149+ } ) ;
114150} ) ;
0 commit comments