aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slider.js
blob: 6ea1a3bac71ef7ac44da51fcd4f628f8bb7e1be2 (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
/*
 * slider unit tests
 */
(function($) {
//
// Selectable Test Helper Functions
//

var defaults = {
	animate: false,
	axis: "???",
	disabled: false,
	distance: 1,
	handle: ".ui-slider-handle",
	handles: null,
	max: 100,
	min: 0,
	noKeyboard: false,
	range: false,
	realMax: "???",
	round: true,
	startValue: "???",
	stepping: "???",
	steps: 0
};

var keyCodes = {
	leftArrow: 37,
	upArrow: 38,
	rightArrow: 39,
	downArrow: 40
};

$.each(keyCodes, function(key, val) {
	$.fn[key] = function() {
		return this.simulate("keydown", { keyCode: val });
	}
});

function assertChange(stepping, start, result, action) {
	return function() {
		expect(1);
		var slider = $("#slider3").slider({
			stepping: stepping,
			startValue: start,
			min: 0,
			max: 1000,
			change: function(event, ui) {
				equals(ui.value, result, "changed to " + ui.value);
			}
		});
		action.apply(slider);
	}
}

// Slider Tests
module("slider");

test("init", function() {
	expect(6);

	$("<div></div>").appendTo('body').slider().remove();
	ok(true, '.slider() called on element');

	$([]).slider().remove();
	ok(true, '.slider() called on empty collection');

	$('<div></div>').slider().remove();
	ok(true, '.slider() called on disconnected DOMElement');

	$('<div></div>').slider().slider("foo").remove();
	ok(true, 'arbitrary method called after init');

	el = $('<div></div>').slider();
	var foo = el.data("foo.slider");
	el.remove();
	ok(true, 'arbitrary option getter after init');

	$('<div></div>').slider().data("foo.slider", "bar").remove();
	ok(true, 'arbitrary option setter after init');
});

test("destroy", function() {
	expect(6);

	$("<div></div>").appendTo('body').slider().slider("destroy").remove();
	ok(true, '.slider("destroy") called on element');

	$([]).slider().slider("destroy").remove();
	ok(true, '.slider("destroy") called on empty collection');

	$('<div></div>').slider().slider("destroy").remove();
	ok(true, '.slider("destroy") called on disconnected DOMElement');

	$('<div></div>').slider().slider("destroy").slider("foo").remove();
	ok(true, 'arbitrary method called after destroy');

	el = $('<div></div>').slider();
	var foo = el.slider("destroy").data("foo.slider");
	el.remove();
	ok(true, 'arbitrary option getter after destroy');

	$('<div></div>').slider().slider("destroy").data("foo.slider", "bar").remove();
	ok(true, 'arbitrary option setter after destroy');
});

test("defaults", function() {
	el = $('<div></div>').slider();
	$.each(defaults, function(key, val) {
		var actual = el.data(key + ".slider"), expected = val;
		same(actual, expected, key);
	});
	el.remove();
});

module("slider: single handle");

test("change one step via keydown", assertChange(1, undefined, 1, function() {
	this.find("a").rightArrow();
}))
test("change - 10 steps via keydown", assertChange(10, 20, 10, function() {
	this.find("a").leftArrow();
}))
test("change +10 steps via keydown", assertChange(10, 20, 30, function() {
	this.find("a").rightArrow();
}))

test("moveTo, absolute value", assertChange(1, 1, 10, function() {
	this.slider("moveTo", 10);
}))

test("moveTo, absolute value as string", assertChange(1, 1, 10, function() {
	this.slider("moveTo", "10");
}))

test("moveTo, absolute value, below min", assertChange(1, 1, 0, function() {
	this.slider("moveTo", -10);
}))

test("moveTo, relative positive value", assertChange(1, 1, 11, function() {
	this.slider("moveTo", "+=10");
}))

test("moveTo, relative positive value, above max", assertChange(1, 10, 1000, function() {
	this.slider("moveTo", "+=2000");
}))

test("moveTo, relative negative value", assertChange(1, 20, 10, function() {
	this.slider("moveTo", "-=10");
}))

test("options update min/max", function() {
	expect(2);
	var slider = $("#slider3").slider({
		stepping: 1,
		startValue: 1
	});
	slider.slider("moveTo", "-=10");
	equals(slider.slider("value"), 0);
	slider.data("min.slider", -10);
	slider.slider("moveTo", "-=20");
	equals(slider.slider("value"), -10);
})

module("slider: setup and teardown");

test("destroy and recreate", function() {
	expect(3)
	var slider = $("#slider3").slider();
	slider.slider("moveTo", "+=20");
	equals(slider.slider("value"), 20);
	slider.slider("destroy");

	slider.slider("moveTo", "+=30");
	ok(true, "nothing happens after slider is destroyed");

	slider.slider().slider("moveTo", "30");

	equals(Math.round(slider.slider("value")), 30);
})

test("handle creation", function() {
	var slider = $("#slider1");
	equals(slider.children().size(), 0);
	slider.slider({
		handles: [
			{ start: 0 },
			{ start: 10 }
		]
	});
	equals(slider.children().size(), 2);
	var instance = $.data(slider[0], "slider")
	equals(instance.handle.length, 2);
	ok(instance.handle.jquery, "handle must be a jquery object")
})

})(jQuery);