2008-08-18 11:38:12 +00:00
|
|
|
/*
|
|
|
|
* spinner unit tests
|
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
// Spinner Tests
|
|
|
|
module("spinner");
|
|
|
|
|
|
|
|
test("init", function() {
|
2008-08-19 09:54:47 +00:00
|
|
|
expect(3);
|
2008-08-18 11:38:12 +00:00
|
|
|
|
2008-08-19 09:54:47 +00:00
|
|
|
$("#spin").spinner();
|
2008-08-18 11:38:12 +00:00
|
|
|
ok(true, '.spinner() called on element');
|
|
|
|
|
2008-08-19 09:54:47 +00:00
|
|
|
$([]).spinner().remove();
|
|
|
|
ok(true, '.spinner() called on empty collection');
|
|
|
|
|
|
|
|
$('<input id="spinner_dis">').spinner().remove();
|
|
|
|
ok(true, '.spinner() called on disconnected element');
|
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("destroy", function() {
|
2008-08-19 09:54:47 +00:00
|
|
|
expect(3);
|
2008-08-18 11:38:12 +00:00
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
$("#spin").spinner().spinner("destroy");
|
2008-08-18 11:38:12 +00:00
|
|
|
ok(true, '.spinner("destroy") called on element');
|
|
|
|
|
2008-08-19 09:54:47 +00:00
|
|
|
$([]).spinner().spinner("destroy").remove();
|
|
|
|
ok(true, '.spinner().spinner("destroy") called on empty collection');
|
|
|
|
|
|
|
|
$('<input id="spinner_dis">').spinner().spinner("destroy").remove();
|
|
|
|
ok(true, '.spinner().spinner("destroy") called on disconnected element');
|
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
});
|
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
test("re-attach", function() {
|
2008-08-19 09:54:47 +00:00
|
|
|
expect(3);
|
2008-08-19 08:51:59 +00:00
|
|
|
|
|
|
|
el = $("#spin").spinner().spinner("destroy").spinner();
|
|
|
|
ok(true, '.spinner().spinner("destroy").spinner() called on element');
|
|
|
|
|
2008-08-19 09:54:47 +00:00
|
|
|
$([]).spinner().spinner("destroy").spinner().remove();
|
|
|
|
ok(true, '.spinner().spinner("destroy").spinner() called on empty collection');
|
2008-08-19 08:51:59 +00:00
|
|
|
|
2008-08-19 09:54:47 +00:00
|
|
|
$('<input id="spinner_dis">').spinner().spinner("destroy").spinner().remove();
|
|
|
|
ok(true, '.spinner().spinner("destroy").spinner() called on disconnected element');
|
2008-08-19 08:51:59 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test("disable", function() {
|
|
|
|
expect(1);
|
|
|
|
|
|
|
|
$("#spin").spinner().spinner("disable");
|
|
|
|
ok(true, '.spinner("disable") called on element');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test("enable", function() {
|
|
|
|
expect(1);
|
|
|
|
|
2008-08-19 09:54:47 +00:00
|
|
|
$("#spin").spinner().spinner("disable").spinner("enable");
|
2008-08-19 08:51:59 +00:00
|
|
|
ok(true, '.spinner("enable") called on element');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
test("defaults", function() {
|
2008-08-19 09:54:47 +00:00
|
|
|
expect(7);
|
2008-08-18 11:38:12 +00:00
|
|
|
el = $("#spin").spinner();
|
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
equals(el.data("currency.spinner"), false, "currency");
|
2008-08-19 09:54:47 +00:00
|
|
|
equals(el.data("disabled.spinner"), false, "disabled");
|
2008-08-18 11:38:12 +00:00
|
|
|
equals(el.data("incremental.spinner"), true, "incremental");
|
|
|
|
equals(el.data("max.spinner"), undefined, "max");
|
|
|
|
equals(el.data("min.spinner"), undefined, "min");
|
|
|
|
equals(el.data("start.spinner"), 0, "start");
|
|
|
|
equals(el.data("stepping.spinner"), 1, "stepping");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test("set defaults on init", function() {
|
2008-08-19 09:54:47 +00:00
|
|
|
expect(7);
|
|
|
|
el = $("#spin").spinner({ currency:true, disabled:true, incremental:false, max:200, min:-100, start:50, stepping:2 });
|
2008-08-18 11:38:12 +00:00
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
equals(el.data("currency.spinner"), true, "currency");
|
2008-08-19 09:54:47 +00:00
|
|
|
equals(el.data("disabled.spinner"), true, "disabled");
|
2008-08-18 11:38:12 +00:00
|
|
|
equals(el.data("incremental.spinner"), false, "incremental");
|
|
|
|
equals(el.data("max.spinner"), 200, "max");
|
|
|
|
equals(el.data("min.spinner"), -100, "min");
|
|
|
|
equals(el.data("start.spinner"), 50, "start");
|
|
|
|
equals(el.data("stepping.spinner"), 2, "stepping");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test("keydown on input", function() {
|
2008-08-18 11:44:42 +00:00
|
|
|
expect(6);
|
2008-08-18 11:38:12 +00:00
|
|
|
el = $("#spin").spinner();
|
|
|
|
|
|
|
|
equals(el.val(), 0, "start number");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
|
|
|
equals(el.val(), 1, "Up key");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_RIGHT})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_RIGHT});
|
|
|
|
|
2008-08-19 07:33:30 +00:00
|
|
|
equals(el.val(), 1, "Right key");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
2008-08-18 11:44:42 +00:00
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_HOME})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_HOME});
|
|
|
|
|
|
|
|
equals(el.val(), 0, "Home key to start");
|
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_DOWN})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_DOWN});
|
|
|
|
|
2008-08-18 11:44:42 +00:00
|
|
|
equals(el.val(), -1, "Down Key");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_LEFT})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_LEFT});
|
|
|
|
|
2008-08-19 07:33:30 +00:00
|
|
|
equals(el.val(), -1, "Left Key");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test("keydown on input with options", function() {
|
|
|
|
expect(4);
|
|
|
|
|
|
|
|
el = $("#spin").spinner({ incremental:false, max:200, min:-100, start:50, stepping:10 });
|
|
|
|
|
|
|
|
equals(el.val(), 50, "start number");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
|
|
|
equals(el.val(), 60, "Stepping 10 on 50");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_END})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_END});
|
|
|
|
|
|
|
|
equals(el.val(), 200, "End key to max");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_HOME})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_HOME});
|
|
|
|
|
|
|
|
equals(el.val(), -100, "Home key to min");
|
|
|
|
|
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("currency and decimal options", function() {
|
2008-08-19 08:56:48 +00:00
|
|
|
expect(5);
|
2008-08-19 08:51:59 +00:00
|
|
|
|
2008-08-19 08:56:48 +00:00
|
|
|
el = $("#spin").spinner({ currency:"$", incremental:false, max:120, min:-50, stepping:0.3 });
|
2008-08-19 08:51:59 +00:00
|
|
|
|
|
|
|
equals(el.val(), "$0.00", "start number");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
2008-08-19 08:56:48 +00:00
|
|
|
equals(el.val(), "$0.30", "Stepping 0.30");
|
2008-08-19 08:51:59 +00:00
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_END})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_END});
|
|
|
|
|
|
|
|
equals(el.val(), "$120.00", "End key to max");
|
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_HOME})
|
|
|
|
.simulate("keyup",{keyCode:$.simulate.VK_HOME});
|
|
|
|
|
|
|
|
equals(el.val(), "-$50.00", "Home key to min");
|
|
|
|
|
2008-08-19 08:56:48 +00:00
|
|
|
for ( var i = 1 ; i<=120 ; i++ ) {
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP});
|
|
|
|
}
|
|
|
|
|
|
|
|
el.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
|
|
|
equals(el.val(), "-$14.00", "keydown 120 times");
|
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
});
|
|
|
|
|
2008-08-19 06:24:24 +00:00
|
|
|
test("spin without auto-incremental stepping", function() {
|
|
|
|
expect(2);
|
|
|
|
|
|
|
|
el = $("#spin").spinner({ incremental:false });
|
|
|
|
|
|
|
|
for ( var i = 1 ; i<=120 ; i++ ) {
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP});
|
|
|
|
}
|
|
|
|
|
|
|
|
el.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
|
|
|
equals(el.val(), 120, "keydown 120 times");
|
|
|
|
|
|
|
|
for ( var i = 1 ; i<=210 ; i++ ) {
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_DOWN});
|
|
|
|
}
|
|
|
|
|
|
|
|
el.simulate("keyup",{keyCode:$.simulate.VK_DOWN});
|
|
|
|
|
|
|
|
equals(el.val(), -90, "keydown 210 times");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2008-08-19 05:54:49 +00:00
|
|
|
test("spin with auto-incremental stepping", function() {
|
|
|
|
expect(2);
|
|
|
|
|
|
|
|
el = $("#spin").spinner();
|
|
|
|
|
|
|
|
for ( var i = 1 ; i<=120 ; i++ ) {
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP});
|
|
|
|
}
|
|
|
|
|
|
|
|
el.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
2008-08-19 06:07:56 +00:00
|
|
|
equals(el.val(), 300, "keydown 120 times (100+20*10)");
|
2008-08-19 05:54:49 +00:00
|
|
|
|
2008-08-19 06:07:56 +00:00
|
|
|
for ( var i = 1 ; i<=210 ; i++ ) {
|
2008-08-19 05:54:49 +00:00
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_DOWN});
|
|
|
|
}
|
|
|
|
|
|
|
|
el.simulate("keyup",{keyCode:$.simulate.VK_DOWN});
|
|
|
|
|
2008-08-19 06:07:56 +00:00
|
|
|
equals(el.val(), -1800, "keydown 210 times (300-100-100*10-10*100)");
|
2008-08-19 05:54:49 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
test("mouse click on buttons", function() {
|
|
|
|
expect(4);
|
|
|
|
el = $("#spin").spinner();
|
|
|
|
|
2008-08-19 07:33:30 +00:00
|
|
|
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
|
|
|
equals(el.val(), 1, "Mouse click to up");
|
|
|
|
|
2008-08-19 07:33:30 +00:00
|
|
|
$(".ui-spinner-up").trigger("dblclick");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
|
|
|
equals(el.val(), 2, "Mouse double click to up");
|
|
|
|
|
2008-08-19 07:33:30 +00:00
|
|
|
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
|
|
|
equals(el.val(), 1, "Mouse click to down");
|
|
|
|
|
2008-08-19 07:33:30 +00:00
|
|
|
$(".ui-spinner-down").trigger("dblclick");
|
2008-08-18 11:38:12 +00:00
|
|
|
|
|
|
|
equals(el.val(), 0, "Mouse double click to down");
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2008-08-19 06:07:56 +00:00
|
|
|
test("callback", function() {
|
|
|
|
expect(2);
|
|
|
|
|
|
|
|
var s = 0,
|
|
|
|
c = 0;
|
|
|
|
|
|
|
|
el = $("#spin").spinner({
|
|
|
|
spin: function(){
|
|
|
|
s++;
|
|
|
|
},
|
|
|
|
change: function(){
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
for ( var i = 1 ; i<=5 ; i++ ) {
|
2008-08-19 06:07:56 +00:00
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP});
|
|
|
|
}
|
|
|
|
|
|
|
|
el.simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
2008-08-19 08:51:59 +00:00
|
|
|
equals(s, 5, "Spin 5 times");
|
2008-08-19 06:07:56 +00:00
|
|
|
|
|
|
|
el.simulate("keydown",{keyCode:$.simulate.VK_UP}).simulate("keyup",{keyCode:$.simulate.VK_UP});
|
|
|
|
|
|
|
|
equals(c, 2, "Change 2 times");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2008-08-18 11:38:12 +00:00
|
|
|
test("mouse wheel on input", function() {
|
|
|
|
expect(0);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|