jquery-ui/tests/unit/datepicker/core.js

174 lines
4.6 KiB
JavaScript
Raw Normal View History

define( [
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
2015-07-15 13:40:38 +00:00
"ui/datepicker"
], function( $, testHelper ) {
module( "datepicker: core" );
test( "input's value determines starting date", function() {
expect( 3 );
2013-11-30 12:22:17 +00:00
var input = $( "#datepicker" ).val( "1/1/14" ).datepicker(),
picker = input.datepicker( "widget" );
input.datepicker( "open" );
equal( picker.find( ".ui-calendar-month" ).html(), "January", "correct month displayed" );
equal( picker.find( ".ui-calendar-year" ).html(), "2014", "correct year displayed" );
equal( picker.find( ".ui-state-active" ).html(), "1", "correct day highlighted" );
input.val( "" ).datepicker( "destroy" );
});
asyncTest( "base structure", function() {
expect( 5 );
2015-07-15 13:40:38 +00:00
var input = testHelper.initNewInput(),
widget = input.datepicker( "widget" );
input.focus();
setTimeout(function() {
ok( widget.is( ":visible" ), "Datepicker visible" );
2014-06-19 22:13:20 +00:00
equal( widget.children().length, 2, "Child count" );
ok( widget.is( ".ui-calendar" ), "Class ui-calendar" );
ok( widget.is( ".ui-datepicker" ), "Class ui-datepicker" );
ok( widget.is( ".ui-front" ), "Class ui-front" );
input.datepicker( "close" );
2013-11-02 13:54:05 +00:00
start();
}, 50 );
2013-11-02 13:54:05 +00:00
});
asyncTest( "Keyboard handling: input", function() {
expect( 10 );
var picker, instance,
input = $( "#datepicker" ).datepicker();
function step1() {
2015-07-15 13:40:38 +00:00
testHelper.init( input );
picker = input.datepicker( "widget" );
ok( !picker.is( ":visible" ), "datepicker closed" );
input.val( "" ).simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
setTimeout(function() {
ok( picker.is( ":visible" ), "Keystroke down opens datepicker" );
input.datepicker( "destroy" );
step2();
}, 100 );
2014-04-23 15:49:03 +00:00
}
function step2() {
2015-07-15 13:40:38 +00:00
testHelper.init( input );
2014-04-23 15:49:03 +00:00
picker = input.datepicker( "widget" );
ok( !picker.is( ":visible" ), "datepicker closed" );
input.val( "" ).simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
setTimeout(function() {
ok( picker.is( ":visible" ), "Keystroke up opens datepicker" );
input.datepicker( "destroy" );
step3();
}, 100 );
2014-04-23 15:49:03 +00:00
}
function step3() {
2015-07-15 13:40:38 +00:00
testHelper.init( input );
instance = input.datepicker( "instance" );
// Enter = Select preset date
input
.val( "1/1/14" )
.datepicker( "refresh" )
.datepicker( "open" )
.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ),
"Keystroke enter - preset" );
input
.val( "" )
.datepicker( "open" );
ok( instance.isOpen, "datepicker is open before escape" );
input.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
ok( !instance.isOpen, "escape closes the datepicker" );
input
.val( "1/1/14" )
.datepicker( "open" )
.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ),
"Keystroke esc - preset" );
input
.val( "1/1/14" )
.datepicker( "open" )
.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } )
.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ),
"Keystroke esc - abandoned" );
input
.val( "1/2/14" )
.simulate( "keyup" );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 2 ),
"Picker updated as user types into input" );
input.datepicker( "destroy" );
start();
2014-04-23 15:49:03 +00:00
}
step1();
});
// TODO: implement
test( "ARIA", function() {
expect( 0 );
});
asyncTest( "mouse", function() {
expect( 4 );
2015-07-15 13:40:38 +00:00
var input = testHelper.init( $( "#datepicker" ).val( "" ) ),
picker = input.datepicker( "widget" );
input.datepicker( "open" );
setTimeout(function() {
input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" );
$( ".ui-calendar-calendar tbody button:contains(12)", picker ).simulate( "mousedown", {} );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate(
input.datepicker( "valueAsDate" ),
new Date( 2008, 4 - 1, 12 ),
"Mouse click - preset"
);
input.val( "" ).datepicker( "refresh" );
input.simulate( "click" );
strictEqual( input.datepicker( "valueAsDate" ), null, "Mouse click - close" );
input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" );
input.simulate( "click" );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate(
input.datepicker( "valueAsDate" ),
new Date( 2008, 4 - 1, 4 ),
"Mouse click - close + preset"
);
input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" );
picker.find( "a.ui-calendar-prev" ).simulate( "click" );
input.simulate( "click" );
2015-07-15 13:40:38 +00:00
testHelper.equalsDate(
input.datepicker( "valueAsDate" ),
new Date( 2008, 4 - 1, 4 ),
"Mouse click - abandoned"
);
start();
}, 100 );
});
} );