aboutsummaryrefslogtreecommitdiffstats
path: root/ui/tests/draggable.js
blob: a0f403e6fbb77dcc903c6f3279f4614515d67443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var drag = function(el, dx, dy, complete) {
	
	// speed = sync -> Drag syncrhonously.
	// speed = fast|slow -> Drag asyncrhonously - animated.
	
	return $(el).simulate("drag", {
		dx: dx||0, dy: dy||0, speed: 'sync', complete: complete 
	});
};

	
module("Draggable");

test("create and destroy", function() {

	expect(2);

	$("#draggable1").draggable();
	ok($("#draggable1").data("draggable"), "Accessing draggable instance after creation");
	
	$("#draggable1").draggable("destroy");
	ok(!$("#draggable1").data("draggable"), "Accessing draggable instance after destroy");
	
});

test("No options, relative", function() {

	expect(4);

	$("#draggable1").draggable();
	
	var offset = $("#draggable1").data("draggable").element.offset();
	equals(offset.left, -979, "Checking initial left position");
	equals(offset.top, -979, "Checking initial top position");
	
	drag("#draggable1", 50, 50);

	var offset = $("#draggable1").data("draggable").element.offset();	
	equals(offset.left, -929, "Checking new left position");
	equals(offset.top, -929, "Checking new top position");
	
	
});

test("No options, absolute", function() {

	expect(4);

	$("#draggable2").draggable();
	
	var offset = $("#draggable2").data("draggable").element.offset();
	equals(offset.left, -979, "Checking initial left position");
	equals(offset.top, -979, "Checking initial top position");
	
	drag("#draggable2", 50, 50);

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -929, "Checking new left position");
	equals(offset.top, -929, "Checking new top position");
	
	
});

test("{ helper: 'clone' }", function() {

	expect(4);

	$("#draggable1, #draggable2").draggable({ helper: "clone" });
	
	drag("#draggable1, #draggable2", 50, 50);

	var offset = $("#draggable1").data("draggable").element.offset();	
	equals(offset.left, -979, "Relative: Original position should be untouched");
	equals(offset.top, -979, "Relative: Original position should be untouched");

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -979, "Absolute: Original position should be untouched");
	equals(offset.top, -979, "Absolute: Original position should be untouched");
	
	
});

test("{ grid: [50,50] }", function() {

	expect(4);

	$("#draggable1, #draggable2").draggable({ grid: [50,50] });
	
	drag("#draggable1, #draggable2", 30, 30);

	var offset = $("#draggable1").data("draggable").element.offset();	
	equals(offset.left, -929, "Relative: 50px move in grid");
	equals(offset.top, -929, "Relative: 50px move in grid");

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -929, "Absolute: 50px move in grid");
	equals(offset.top, -929, "Absolute: 50px move in grid");
	
	
});

test("{ axis: 'y' }", function() {

	expect(4);

	$("#draggable1, #draggable2").draggable({ axis: "y" });
	
	drag("#draggable1, #draggable2", 50, 50);

	var offset = $("#draggable1").data("draggable").element.offset();	
	equals(offset.left, -979, "Relative: 0px move");
	equals(offset.top, -929, "Relative: 50px move");

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -979, "Absolute: 0px move");
	equals(offset.top, -929, "Absolute: 50px move");
	
	
});

test("{ axis: 'x' }", function() {

	expect(4);

	$("#draggable1, #draggable2").draggable({ axis: "x" });
	
	drag("#draggable1, #draggable2", 50, 50);

	var offset = $("#draggable1").data("draggable").element.offset();	
	equals(offset.left, -929, "Relative: 50px move");
	equals(offset.top, -979, "Relative: 0px move");

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -929, "Absolute: 50px move");
	equals(offset.top, -979, "Absolute: 0px move");
	
	
});

test("{ cancel: 'span' }", function() {

	expect(2);

	$("#draggable2").draggable({ cancel: 'span' });
	
	drag("#draggable2 span", 50, 50);

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -979, "Trigger on span: 0px move");
	equals(offset.top, -979, "Trigger on span: 0px move");
	
	
});

test("{ handle: 'span' }", function() {

	expect(4);

	$("#draggable2").draggable({ handle: 'span' });
	
	drag("#draggable2 span", 50, 50);

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -929, "Trigger on span: 50px move");
	equals(offset.top, -929, "Trigger on span: 50px move");
	
	drag("#draggable2", 50, 50);
	
	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -929, "Trigger on element: 0px move");
	equals(offset.top, -929, "Trigger on element: 0px move");
	
	
});

test("{ containment: 'parent' }", function() {

	expect(4);

	$("#draggable2, #draggable1").draggable({ containment: 'parent' });
	
	drag("#draggable2, #draggable1", -100, -100);

	var offset = $("#draggable2").data("draggable").element.offset();	
	equals(offset.left, -989, "Absolute: 10px move (to parent's margin end)");
	equals(offset.top, -989, "Absolute: 10px move (to parent's margin end)");
	
	var offset = $("#draggable1").data("draggable").element.offset();	
	equals(offset.left, -989, "Relative: 10px move (to parent's margin end)");
	equals(offset.top, -989, "Relative: 10px move (to parent's margin end)");
	
	
});

test("{ cursorAt: { left: -5, top: -5 } }", function() {

	expect(4);

	var position = null;
	$("#draggable2").draggable({
		cursorAt: { left: -5, top: -5 },
		drag: function(e, ui) {
			position = ui.absolutePosition;
		}
	});
	
	drag("#draggable2", -1, -1);

	equals(position.left, -875, "Absolute: -1px move");
	equals(position.top, -925, "Absolute: -1px move");
	
	var position = null;
	$("#draggable1").draggable({
		cursorAt: { left: -5, top: -5 },
		drag: function(e, ui) {
			position = ui.absolutePosition;
		}
	});
	
	drag("#draggable1", -1, -1);

	equals(position.left, -875, "Relative: -1px move");
	equals(position.top, -925, "Relative: -1px move");

});

test("{ cursor: 'move' }", function() {

	expect(2);

	var cursor = null;
	$("#draggable2").draggable({
		cursor: "move",
		start: function(e, ui) {
			cursor = $("body").css("cursor");
		}
	});
	
	drag("#draggable2", -1, -1);

	equals(cursor, "move", "start callback: cursor 'move'");
	equals($("body").css("cursor"), "auto", "after drag: cursor restored");

});

test("{ distance: 10 }", function() {

	expect(2);

	var dragged = false;
	$("#draggable2").draggable({
		distance: 10,
		start: function(e, ui) {
			dragged = true;
		}
	});
	
	drag("#draggable2", -9, -9);
	equals(dragged, false, "The draggable should not have moved when moving -9px");
	
	drag("#draggable2", -10, -10);
	equals(dragged, true, "The draggable should have moved when moving -10px");

});

test("{ opacity: 0.5 }", function() {

	expect(1);

	var opacity = null;
	$("#draggable2").draggable({
		opacity: 0.5,
		start: function(e, ui) {
			opacity = $(this).css("opacity");
		}
	});
	
	drag("#draggable2", -1, -1);

	equals(opacity, 0.5, "start callback: opacity is 0.5");

});

test("{ zIndex: 10 }", function() {

	expect(1);

	var zIndex = null;
	$("#draggable2").draggable({
		zIndex: 10,
		start: function(e, ui) {
			zIndex = $(this).css("zIndex");
		}
	});
	
	drag("#draggable2", -1, -1);

	equals(zIndex, "10", "start callback: zIndex is 10");

});

test("callbacks occurance count", function() {

	expect(3);

	var start = 0, stop = 0, dragc = 0;
	$("#draggable2").draggable({
		start: function() { start++; },
		drag: function() { dragc++; },
		stop: function() { stop++; }
	});
	
	drag("#draggable2", 10, 10);

	equals(start, 1, "start callback should happen exactly once");
	equals(dragc, 11, "drag callback should happen exactly 1+10 times (first simultaneously with start)");
	equals(stop, 1, "stop callback should happen exactly once");

});