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

QUnit.module( "slider: events", { afterEach: helper.moduleAfterEach }  );

//Specs from http://wiki.jqueryui.com/Slider#specs
//"change callback: triggers when the slider has stopped moving and has a new
// value (even if same as previous value), via mouse(mouseup) or keyboard(keyup)
// or value method/option"
QUnit.test( "mouse based interaction", function( assert ) {
	assert.expect( 4 );

	var element = $( "#slider1" )
		.slider( {
			start: function( event ) {
				assert.equal( event.originalEvent.type, "mousedown", "start triggered by mousedown" );
			},
			slide: function( event ) {
				assert.equal( event.originalEvent.type, "mousemove", "slider triggered by mousemove" );
			},
			stop: function( event ) {
				assert.equal( event.originalEvent.type, "mouseup", "stop triggered by mouseup" );
			},
			change: function( event ) {
				assert.equal( event.originalEvent.type, "mouseup", "change triggered by mouseup" );
			}
		} );

	element.find( ".ui-slider-handle" ).eq( 0 )
		.simulate( "drag", { dx: 10, dy: 10 } );

} );
QUnit.test( "keyboard based interaction", function( assert ) {
	assert.expect( 3 );

	// Test keyup at end of handle slide (keyboard)
	var element = $( "#slider1" )
		.slider( {
			start: function( event ) {
				assert.equal( event.originalEvent.type, "keydown", "start triggered by keydown" );
			},
			slide: function() {
				assert.ok( false, "Slider never triggered by keys" );
			},
			stop: function( event ) {
				assert.equal( event.originalEvent.type, "keyup", "stop triggered by keyup" );
			},
			change: function( event ) {
				assert.equal( event.originalEvent.type, "keyup", "change triggered by keyup" );
			}
		} );

	element.find( ".ui-slider-handle" ).eq( 0 )
		.simulate( "keydown", { keyCode: $.ui.keyCode.LEFT } )
		.simulate( "keypress", { keyCode: $.ui.keyCode.LEFT } )
		.simulate( "keyup", { keyCode: $.ui.keyCode.LEFT } );

} );
QUnit.test( "programmatic event triggers", function( assert ) {
	assert.expect( 6 );

	// Test value method
	var element = $( "<div></div>" )
		.slider( {
			change: function() {
				assert.ok( true, "change triggered by value method" );
			}
		} )
		.slider( "value", 0 );

	// Test values method
	element = $( "<div></div>" )
		.slider( {
			values: [ 10, 20 ],
			change: function() {
				assert.ok( true, "change triggered by values method" );
			}
		} )
		.slider( "values", [ 80, 90 ] );

	// Test value option
	element = $( "<div></div>" )
		.slider( {
			change: function() {
				assert.ok( true, "change triggered by value option" );
			}
		} )
		.slider( "option", "value", 0 );

	// Test values option
	element = $( "<div></div>" )
		.slider( {
			values: [ 10, 20 ],
			change: function() {
				assert.ok( true, "change triggered by values option" );
			}
		} )
		.slider( "option", "values", [ 80, 90 ] );

} );

QUnit.test( "mouse based interaction part two: when handles overlap", function( assert ) {
	assert.expect( 6 );

	var element = $( "#slider1" )
		.slider( {
			values: [ 0, 0, 0 ],
			start: function( event, ui ) {
				assert.equal( handles.index( ui.handle ), 2, "rightmost handle activated when overlapping at minimum (#3736)" );
			}
		} ),
		handles = element.find( ".ui-slider-handle" );
	handles.eq( 0 ).simulate( "drag", { dx: 10 } );
	element.slider( "destroy" );

	element = $( "#slider1" )
		.slider( {
			values: [ 10, 10, 10 ],
			max: 10,
			start: function( event, ui ) {
				assert.equal( handles.index( ui.handle ), 0, "leftmost handle activated when overlapping at maximum" );
			}
		} ),
		handles = element.find( ".ui-slider-handle" );
	handles.eq( 0 ).simulate( "drag", { dx: -10 } );
	element.slider( "destroy" );

	element = $( "#slider1" )
		.slider( {
			values: [ 19, 20 ]
		} ),
		handles = element.find( ".ui-slider-handle" );
	handles.eq( 0 ).simulate( "drag", { dx: 10 } );
	element.one( "slidestart", function( event, ui ) {
		assert.equal( handles.index( ui.handle ), 0, "left handle activated if left was moved last" );
	} );
	handles.eq( 0 ).simulate( "drag", { dx: 10 } );
	element.slider( "destroy" );

	element = $( "#slider1" )
		.slider( {
			values: [ 19, 20 ]
		} ),
		handles = element.find( ".ui-slider-handle" );
	handles.eq( 1 ).simulate( "drag", { dx: -10 } );
	element.one( "slidestart", function( event, ui ) {
		assert.equal( handles.index( ui.handle ), 1, "right handle activated if right was moved last (#3467)" );
	} );
	handles.eq( 0 ).simulate( "drag", { dx: 10 } );

	element = $( "#slider1" )
		.slider( {
			range: true,
			min: 0,
			max: 100,
			values: [ 0, 50 ]
		} ),
		handles = element.find( ".ui-slider-handle" );

	element.slider( "option", { values: [ 100, 100 ] } );
	handles.eq( 0 ).simulate( "drag", { dx: -10 } );
	assert.equal( element.slider( "values" )[ 0 ], 99, "setting both values of range slider to the maximum doesn't lock slider" );

	element.slider( "option", { values: [ 0, 0 ] } );
	handles.eq( 1 ).simulate( "drag", { dx: 10 } );
	assert.equal( element.slider( "values" )[ 1 ], 1, "setting both values of range slider to the minimum  doesn't lock slider" );
} );

QUnit.test( "event data", function( assert ) {
	assert.expect( 6 );

	var slideHandleIndex = 3,
		values = [ 8, 9, 7, 4 ],
		newValues = [ 8, 9, 7, 5 ],
		element = $( "#slider1" )
			.slider( {
				values: values,
				start: function( event, ui ) {
					assert.deepEqual( ui, expectedUiHash, "passing ui to start event" );
				},
				slide: function( event, ui ) {
					assert.deepEqual( ui, expectedChangedUiHash, "passing ui to slide event" );
				},
				stop: function( event, ui ) {
					assert.deepEqual( ui, expectedChangedUiHash, "passing ui to stop event" );
				},
				change: function( event, ui ) {
					assert.deepEqual( ui, expectedChangedUiHash, "passing ui to change event" );
				}
			} ),
		handles = element.find( ".ui-slider-handle" ),
		expectedUiHash = {
			handle: handles.eq( slideHandleIndex )[ 0 ],
			handleIndex: slideHandleIndex,
			values: values,
			value: values[ slideHandleIndex ]
		},
		expectedChangedUiHash = $.extend( {}, expectedUiHash, {
			values: newValues,
			value: newValues[ slideHandleIndex ]
		} );

	handles.eq( slideHandleIndex ).simulate( "drag", { dx: 10 } );

	element.slider( "destroy" );
	element = $( "#slider1" ).slider( {
		min: 0,
		max: 100,
		value: 1,
		slide: function( event, ui ) {
			assert.equal( ui.value, 0, "should pass 0 value if slider reaches it" );
		}
	} );
	handles = element.find( ".ui-slider-handle" );
	handles.eq( 0 ).simulate( "drag", { dx: -10 } );

	element.slider( "destroy" );
	element = $( "#slider1" ).slider( {
		min: 0,
		max: 100,
		values: [ 1, 2 ],
		slide: function( event, ui ) {
			assert.equal( ui.value, 0, "should pass 0 value if one of handles reaches it" );
		}
	} );
	handles = element.find( ".ui-slider-handle" );
	handles.eq( 0 ).simulate( "drag", { dx: -10 } );
} );

} );