-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathdraggingSpec.js
309 lines (250 loc) · 10.3 KB
/
draggingSpec.js
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
describe("Dragging Dragdealer", function() {
beforeEach(function() {
this.addMatchers(matchers);
});
it("should move handle along with mouse after pressing", function() {
helpers.initDragdealer('simple-slider');
helpers.dragTo('simple-slider', 100, 0);
expect('simple-slider').toHavePosition(100, 0);
});
it("should move custom handle along with mouse after pressing", function() {
helpers.initDragdealer('custom-handle', {
handleClass: 'custom-handle'
});
helpers.dragTo('custom-handle', 100, 0, 'custom-handle');
expect('custom-handle').toHavePosition(100, 0, 'custom-handle');
});
it("should not move disabled Dragdealer", function() {
helpers.initDragdealer('simple-slider', {
disabled: true
});
helpers.dragTo('simple-slider', 100, 0);
expect('simple-slider').toHavePosition(0, 0);
});
it("should not drag horizontal slider vertically", function() {
helpers.initDragdealer('square-slider', {
horizontal: true,
vertical: false
});
helpers.dragTo('square-slider', 0, 100);
expect('square-slider').toHavePosition(0, 0);
helpers.dragTo('square-slider', 200, 200);
expect('square-slider').toHavePosition(200, 0);
});
it("should not drag vertical slider horizontally", function() {
helpers.initDragdealer('square-slider', {
horizontal: false,
vertical: true
});
helpers.dragTo('square-slider', 100, 0);
expect('square-slider').toHavePosition(0, 0);
helpers.dragTo('square-slider', 200, 200);
expect('square-slider').toHavePosition(0, 200);
});
describe("should constrain handle position under the wrapper bounds", function() {
it("when wrapper is bigger than handle", function() {
helpers.initDragdealer('simple-slider');
helpers.dragTo('simple-slider', -100, 0);
expect('simple-slider').toHavePosition(0, 0);
helpers.dragTo('simple-slider', 0, -100);
expect('simple-slider').toHavePosition(0, 0);
helpers.dragTo('simple-slider', 0, 100);
expect('simple-slider').toHavePosition(0, 0);
helpers.dragTo('simple-slider', 500, 0);
expect('simple-slider').toHavePosition(400, 0);
});
it("when wrapper is bigger than handle and has padding", function() {
helpers.initDragdealer('square-slider', {
horizontal: true,
vertical: true,
top: 10,
bottom: 20,
left: 30,
right: 40
});
helpers.dragTo('square-slider', -1000, -1000);
expect('square-slider').toHavePosition(30, 10);
helpers.dragTo('square-slider', 1000, -1000);
expect('square-slider').toHavePosition(360, 10);
helpers.dragTo('square-slider', 1000, 1000);
expect('square-slider').toHavePosition(360, 380);
helpers.dragTo('square-slider', -1000, 1000);
expect('square-slider').toHavePosition(30, 380);
});
it("when handle is bigger than wrapper", function() {
helpers.initDragdealer('masked-slider', {
horizontal: true,
vertical: true,
top: 10,
bottom: 10,
left: 10,
right: 10
});
helpers.dragTo('masked-slider', 1000, 1000);
expect('masked-slider').toHavePosition(10, 10);
helpers.dragTo('masked-slider', -1000, 1000);
expect('masked-slider').toHavePosition(-510, 10);
helpers.dragTo('masked-slider', -1000, -1000);
expect('masked-slider').toHavePosition(-510, -510);
helpers.dragTo('masked-slider', 1000, -1000);
expect('masked-slider').toHavePosition(10, -510);
});
});
it("should slide handle after releasing drag", function() {
helpers.initDragdealer('simple-slider', {
slide: true
});
helpers.dragTo('simple-slider', 50, 0);
expect('simple-slider').toHavePosition(50, 0);
// The slider gets a force of 4x the last movement and keeps eating a part
// of it with every interval loop, normally we'd never move an entire 50px
// with one mouse move
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(250, 0);
});
it("should pull handle to closest step after releasing drag", function() {
helpers.initDragdealer('simple-slider', {
slide: false,
// Considering the simple slider has a wrapper of 500px width and a
// handle of 100px width, the step positions will be 0, 80, 160, 240, 320
// and 400
steps: 6
});
helpers.dragTo('simple-slider', 100, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(80, 0);
helpers.dragTo('simple-slider', 50, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(80, 0);
helpers.dragTo('simple-slider', 350, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(320, 0);
helpers.dragTo('simple-slider', 210, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(240, 0);
});
it("should slide handle to projected step after releasing drag", function() {
helpers.initDragdealer('simple-slider', {
// The slider gets a force of 4x the last movement
slide: true,
// Considering the simple slider has a wrapper of 500px width and a
// handle of 100px width, the step positions will be 0, 80, 160, 240, 320
// and 400
steps: 6
});
// is dragged 25px to the right, and will slide 125px, to 125, 0
helpers.dragTo('simple-slider', 25, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(160, 0);
// is dragged 15px to the left, and will slide 75px, to 135, 0
helpers.dragTo('simple-slider', 155, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(160, 0);
// is dragged 25px to the right, and will slide 125px, to 285, 0
helpers.dragTo('simple-slider', 185, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(320, 0);
// is dragged 20px to the left, and will slide 250px, to 70, 0
helpers.dragTo('simple-slider', 270, 0);
helpers.drop('simple-slider');
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(80, 0);
});
it("should snap handle to closest step after releasing drag", function() {
helpers.initDragdealer('simple-slider', {
slide: false,
// Considering the simple slider has a wrapper of 500px width and a
// handle of 100px width, the step positions will be 0, 80, 160, 240, 320
// and 400
steps: 6,
snap: true
});
helpers.dragTo('simple-slider', 100, 0);
helpers.drop('simple-slider');
expect('simple-slider').toHavePosition(80, 0);
helpers.dragTo('simple-slider', 50, 0);
helpers.drop('simple-slider');
expect('simple-slider').toHavePosition(80, 0);
helpers.dragTo('simple-slider', 350, 0);
helpers.drop('simple-slider');
expect('simple-slider').toHavePosition(320, 0);
helpers.dragTo('simple-slider', 210, 0);
helpers.drop('simple-slider');
expect('simple-slider').toHavePosition(240, 0);
});
it("should drag loose handle outside bigger wrapper", function() {
// Any positon offset outside the wrapper bounds will be split by 4
helpers.initDragdealer('simple-slider', {
loose: true
});
// This goes outside the wrapper with 100px, so the exceeding offset will
// be 25px
helpers.dragTo('simple-slider', -100, 0);
helpers.drop('simple-slider');
expect('simple-slider').toHavePosition(-25, 0);
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(0, 0);
// This goes outside the wrapper with 200px, since 400px is the rightmost
// position of a 100px wide handle inside a 500px wide wrapper, so the
// exceeding offset will be 50px
helpers.dragTo('simple-slider', 600, 0);
helpers.drop('simple-slider');
expect('simple-slider').toHavePosition(450, 0);
helpers.callRequestAnimationFrameMock(3000);
expect('simple-slider').toHavePosition(400, 0);
});
it("should drag loose handle inside smaller wrapper", function() {
// Any positon offset outside the wrapper bounds will be split by 4
helpers.initDragdealer('masked-slider', {
horizontal: true,
vertical: true,
loose: true
});
helpers.dragTo('masked-slider', 100, 200);
helpers.drop('masked-slider');
expect('masked-slider').toHavePosition(25, 50);
helpers.callRequestAnimationFrameMock(3000);
expect('masked-slider').toHavePosition(0, 0);
helpers.dragTo('masked-slider', -2000, -1000);
helpers.drop('masked-slider');
expect('masked-slider').toHavePosition(-875, -625);
helpers.callRequestAnimationFrameMock(3000);
expect('masked-slider').toHavePosition(-500, -500);
});
it("should not break dragging after repositioning", function() {
// Fix for https://github.com/skidding/dragdealer/issues/3
var dragdealer = helpers.initDragdealer('masked-slider', {
horizontal: true,
vertical: true
});
$('#masked-slider').css('margin-top', 200);
helpers.dragTo('masked-slider', -250, -250);
expect('masked-slider').toHavePosition(-250, -250);
expect(dragdealer.getValue()).toEqual([0.5, 0.5]);
});
it("should not work after unbinding events", function() {
// Spec for https://github.com/skidding/dragdealer/issues/8
var dragdealer = helpers.initDragdealer('simple-slider');
dragdealer.unbindEventListeners();
helpers.dragTo('simple-slider', 200, 0);
expect('simple-slider').toHavePosition(0, 0);
});
it("should stop sliding animation after unbinding events", function() {
// Spec for https://github.com/skidding/dragdealer/issues/8
var dragdealer = helpers.initDragdealer('simple-slider');
helpers.dragTo('simple-slider', 200, 0);
helpers.drop('simple-slider');
dragdealer.unbindEventListeners();
helpers.callRequestAnimationFrameMock(3000);
// The handle would reach the 400, 0 position if we wouldn't unbind it
expect('simple-slider').toHavePosition(200, 0);
});
});