Debian Codegraph @

tests/unit/draggable/events.js

raw symbols SCIP
1 define( [
2 "qunit",
3 "jquery",
4 "lib/helper",
5 "ui/widgets/draggable"
6 ], function( QUnit, $, helper ) {
7 "use strict";
8
9 var element;
10
11 QUnit.module( "draggable: events", {
12 beforeEach: function() {
13 element = $( "<div>" ).appendTo( "#qunit-fixture" );
14 },
15 afterEach: function() {
16 element.draggable( "destroy" );
17 return helper.moduleAfterEach.apply( this, arguments );
18 }
19 } );
20
21 QUnit.test( "callbacks occurrence count", function( assert ) {
22 assert.expect( 3 );
23
24 var start = 0,
25 stop = 0,
26 dragc = 0;
27
28 element.draggable( {
29 start: function() {
30 start++;
31 },
32 drag: function() {
33 dragc++;
34 },
35 stop: function() {
36 stop++;
37 }
38 } );
39
40 element.simulate( "drag", {
41 dx: 10,
42 dy: 10
43 } );
44
45 assert.equal( start, 1, "start callback should happen exactly once" );
46 assert.equal( dragc, 3, "drag callback should happen exactly once per mousemove" );
47 assert.equal( stop, 1, "stop callback should happen exactly once" );
48 } );
49
50 QUnit.test( "stopping the start callback", function( assert ) {
51 assert.expect( 3 );
52
53 var start = 0,
54 stop = 0,
55 dragc = 0;
56
57 element.draggable( {
58 start: function() {
59 start++;
60 return false;
61 },
62 drag: function() {
63 dragc++;
64 },
65 stop: function() {
66 stop++;
67 }
68 } );
69
70 element.simulate( "drag", {
71 dx: 10,
72 dy: 10
73 } );
74
75 assert.equal( start, 1, "start callback should happen exactly once" );
76 assert.equal( dragc, 0, "drag callback should not happen at all" );
77 assert.equal( stop, 0, "stop callback should not happen if there wasnt even a start" );
78 } );
79
80 QUnit.test( "stopping the drag callback", function( assert ) {
81 assert.expect( 2 );
82
83 var start = 0,
84 stop = 0,
85 dragc = 0;
86
87 element.draggable( {
88 start: function() {
89 start++;
90 },
91 drag: function() {
92 dragc++;
93 return false;
94 },
95 stop: function() {
96 stop++;
97 }
98 } );
99
100 element.simulate( "drag", {
101 dx: 10,
102 dy: 10
103 } );
104
105 assert.equal( start, 1, "start callback should happen exactly once" );
106 assert.equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" );
107 } );
108
109 QUnit.test( "stopping the stop callback", function( assert ) {
110 assert.expect( 1 );
111
112 element.draggable( {
113 helper: "clone",
114 stop: function() {
115 return false;
116 }
117 } );
118
119 element.simulate( "drag", {
120 dx: 10,
121 dy: 10
122 } );
123
124 assert.ok( element.draggable( "instance" ).helper, "the clone should not be deleted if the stop callback is stopped" );
125 } );
126
127 // https://bugs.jqueryui.com/ticket/6884
128 // Draggable: ui.offset.left differs between the "start" and "drag" hooks
129 QUnit.test( "position and offset in hash is consistent between start, drag, and stop", function( assert ) {
130 assert.expect( 4 );
131
132 var startPos, startOffset, dragPos, dragOffset, stopPos, stopOffset;
133
134 element = $( "<div style='margin: 2px;'></div>" ).appendTo( "#qunit-fixture" );
135
136 element.draggable( {
137 start: function( event, ui ) {
138 startPos = ui.position;
139 startOffset = ui.offset;
140 },
141 drag: function( event, ui ) {
142 dragPos = ui.position;
143 dragOffset = ui.offset;
144 },
145 stop: function( event, ui ) {
146 stopPos = ui.position;
147 stopOffset = ui.offset;
148 }
149 } );
150
151 element.simulate( "drag", {
152 dx: 10,
153 dy: 10,
154 moves: 1
155 } );
156
157 startPos.left += 10;
158 startPos.top += 10;
159 startOffset.left += 10;
160 startOffset.top += 10;
161
162 assert.deepEqual( startPos, dragPos, "start position equals drag position plus distance" );
163 assert.deepEqual( dragPos, stopPos, "drag position equals stop position" );
164 assert.deepEqual( startOffset, dragOffset, "start offset equals drag offset plus distance" );
165 assert.deepEqual( dragOffset, stopOffset, "drag offset equals stop offset" );
166 } );
167
168 } );