11import { ref } from 'vue' ;
22import useDraggable from '../useDraggable' ;
33import type { InteractContext } from '../useInteractContext' ;
4+ import type {
5+ DraggableOptions ,
6+ DragEvent ,
7+ } from '@interactjs/actions/drag/plugin' ;
48
59describe ( 'useDraggable' , ( ) => {
610 let context : InteractContext ;
711 let draggableMock : jest . Mock ;
12+ let setMock : jest . Mock ;
813
914 beforeEach ( ( ) => {
1015 draggableMock = jest . fn ( ) ;
16+ setMock = jest . fn ( ) ;
1117 context = {
12- interactable : ref ( { draggable : draggableMock } as any ) ,
18+ interactable : ref ( {
19+ draggable : draggableMock ,
20+ set : setMock ,
21+ target : document . createElement ( 'div' ) ,
22+ } as any ) ,
1323 interact : { } as any ,
1424 position : ref ( { x : 0 , y : 0 } ) ,
1525 size : ref ( { width : 0 , height : 0 } ) ,
@@ -27,6 +37,24 @@ describe('useDraggable', () => {
2737 expect ( typeof options . listeners . end ) . toBe ( 'function' ) ;
2838 } ) ;
2939
40+ it ( 'draggableOptions getter should return merged initial options' , ( ) => {
41+ const initialOpts : DraggableOptions = {
42+ inertia : { enabled : true } ,
43+ autoScroll : { enabled : true } ,
44+ } ;
45+ const { init, draggableOptions } = useDraggable ( context , initialOpts ) ;
46+ init ( ) ;
47+
48+ const currentOptions = draggableOptions . value ;
49+ expect ( currentOptions . inertia ) . toEqual ( initialOpts . inertia ) ;
50+ expect ( currentOptions . autoScroll ) . toEqual ( initialOpts . autoScroll ) ;
51+ expect ( currentOptions . listeners ) . toBeDefined ( ) ;
52+ const listeners = currentOptions . listeners as any ;
53+ expect ( typeof listeners . start ) . toBe ( 'function' ) ;
54+ expect ( typeof listeners . move ) . toBe ( 'function' ) ;
55+ expect ( typeof listeners . end ) . toBe ( 'function' ) ;
56+ } ) ;
57+
3058 it ( 'should handle drag events correctly' , ( ) => {
3159 const { init, isDragging, position } = useDraggable ( context ) ;
3260 init ( ) ;
@@ -46,20 +74,49 @@ describe('useDraggable', () => {
4674 expect ( isDragging . value ) . toBe ( false ) ;
4775 } ) ;
4876
49- it ( 'should call interactable.set when draggableOptions are set' , ( ) => {
50- const setMock = jest . fn ( ) ;
51- context . interactable . value = {
52- draggable : draggableMock ,
53- set : setMock ,
54- } as any ;
77+ it ( 'draggableOptions setter should update options and call interactable.set correctly' , ( ) => {
78+ const { init, draggableOptions } = useDraggable ( context , {
79+ lockAxis : 'x' ,
80+ } ) ;
81+ init ( ) ;
5582
56- const { draggableOptions } = useDraggable ( context ) ;
83+ const newSpecificOptions : DraggableOptions = {
84+ inertia : { resistance : 10 , enabled : true } ,
85+ autoScroll : {
86+ enabled : true ,
87+ container : document . createElement ( 'div' ) ,
88+ } ,
89+ cursorChecker : ( ) => 'grabbing' ,
90+ } ;
5791
58- const newOptions = { enabled : false } ;
59- draggableOptions . value = newOptions ;
92+ draggableOptions . value = newSpecificOptions ;
6093
6194 expect ( setMock ) . toHaveBeenCalledTimes ( 1 ) ;
62- expect ( setMock ) . toHaveBeenCalledWith ( newOptions ) ;
95+ expect ( setMock ) . toHaveBeenCalledWith ( {
96+ drag : expect . objectContaining ( {
97+ inertia : newSpecificOptions . inertia ,
98+ autoScroll : newSpecificOptions . autoScroll ,
99+ cursorChecker : newSpecificOptions . cursorChecker ,
100+ listeners : expect . objectContaining ( {
101+ start : expect . any ( Function ) ,
102+ move : expect . any ( Function ) ,
103+ end : expect . any ( Function ) ,
104+ } ) ,
105+ } ) ,
106+ } ) ;
107+
108+ const updatedOptions = draggableOptions . value ;
109+ expect ( updatedOptions . inertia ) . toEqual ( newSpecificOptions . inertia ) ;
110+ expect ( updatedOptions . autoScroll ) . toEqual (
111+ newSpecificOptions . autoScroll ,
112+ ) ;
113+ expect ( updatedOptions . cursorChecker ) . toEqual (
114+ newSpecificOptions . cursorChecker ,
115+ ) ;
116+ expect ( updatedOptions . lockAxis ) . toBeUndefined ( ) ;
117+ expect ( updatedOptions . listeners ) . toBeDefined ( ) ;
118+ const listeners = updatedOptions . listeners as any ;
119+ expect ( typeof listeners . start ) . toBe ( 'function' ) ;
63120 } ) ;
64121
65122 it ( 'init throws when context.interactable.value is null' , ( ) => {
0 commit comments