aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/draggable/events.js
blob: 45c3c20d5463fdb4dd2f5189d33c16b63ad00a63 (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
define( [
	"qunit",
	"jquery",
	"lib/helper",
	"ui/widgets/draggable"
], function( QUnit, $, helper ) {
"use strict";

var element;

QUnit.module( "draggable: events", {
	beforeEach: function() {
		element = $( "<div>" ).appendTo( "#qunit-fixture" );
	},
	afterEach: function() {
		element.draggable( "destroy" );
		return helper.moduleAfterEach.apply( this, arguments );
	}
} );

QUnit.test( "callbacks occurrence count", function( assert ) {
	assert.expect( 3 );

	var start = 0,
		stop = 0,
		dragc = 0;

	element.draggable( {
		start: function() {
			start++;
		},
		drag: function() {
			dragc++;
		},
		stop: function() {
			stop++;
		}
	} );

	element.simulate( "drag", {
		dx: 10,
		dy: 10
	} );

	assert.equal( start, 1, "start callback should happen exactly once" );
	assert.equal( dragc, 3, "drag callback should happen exactly once per mousemove" );
	assert.equal( stop, 1, "stop callback should happen exactly once" );
} );

QUnit.test( "stopping the start callback", function( assert ) {
	assert.expect( 3 );

	var start = 0,
		stop = 0,
		dragc = 0;

	element.draggable( {
		start: function() {
			start++;
			return false;
		},
		drag: function() {
			dragc++;
		},
		stop: function() {
			stop++;
		}
	} );

	element.simulate( "drag", {
		dx: 10,
		dy: 10
	} );

	assert.equal( start, 1, "start callback should happen exactly once" );
	assert.equal( dragc, 0, "drag callback should not happen at all" );
	assert.equal( stop, 0, "stop callback should not happen if there wasnt even a start" );
} );

QUnit.test( "stopping the drag callback", function( assert ) {
	assert.expect( 2 );

	var start = 0,
		stop = 0,
		dragc = 0;

	element.draggable( {
		start: function() {
			start++;
		},
		drag: function() {
			dragc++;
			return false;
		},
		stop: function() {
			stop++;
		}
	} );

	element.simulate( "drag", {
		dx: 10,
		dy: 10
	} );

	assert.equal( start, 1, "start callback should happen exactly once" );
	assert.equal( stop, 1, "stop callback should happen, as we need to actively stop the drag" );
} );

QUnit.test( "stopping the stop callback", function( assert ) {
	assert.expect( 1 );

	element.draggable( {
		helper: "clone",
		stop: function() {
			return false;
		}
	} );

	element.simulate( "drag", {
		dx: 10,
		dy: 10
	} );

	assert.ok( element.draggable( "instance" ).helper, "the clone should not be deleted if the stop callback is stopped" );
} );

// https://bugs.jqueryui.com/ticket/6884
// Draggable: ui.offset.left differs between the "start" and "drag" hooks
QUnit.test( "position and offset in hash is consistent between start, drag, and stop", function( assert ) {
	assert.expect( 4 );

	var startPos, startOffset, dragPos, dragOffset, stopPos, stopOffset;

	element = $( "<div style='margin: 2px;'></div>" ).appendTo( "#qunit-fixture" );

	element.draggable( {
		start: function( event, ui ) {
			startPos = ui.position;
			startOffset = ui.offset;
		},
		drag: function( event, ui ) {
			dragPos = ui.position;
			dragOffset = ui.offset;
		},
		stop: function( event, ui ) {
			stopPos = ui.position;
			stopOffset = ui.offset;
		}
	} );

	element.simulate( "drag", {
		dx: 10,
		dy: 10,
		moves: 1
	} );

	startPos.left += 10;
	startPos.top += 10;
	startOffset.left += 10;
	startOffset.top += 10;

	assert.deepEqual( startPos, dragPos, "start position equals drag position plus distance" );
	assert.deepEqual( dragPos, stopPos, "drag position equals stop position" );
	assert.deepEqual( startOffset, dragOffset, "start offset equals drag offset plus distance" );
	assert.deepEqual( dragOffset, stopOffset, "drag offset equals stop offset" );
} );

} );