2015-04-07 00:06:31 +00:00
|
|
|
define( [
|
2016-04-03 15:11:33 +00:00
|
|
|
"qunit",
|
2015-04-07 00:06:31 +00:00
|
|
|
"jquery",
|
2015-04-07 14:55:52 +00:00
|
|
|
"./helper",
|
2015-07-15 01:58:04 +00:00
|
|
|
"ui/widgets/datepicker"
|
2016-04-03 15:11:33 +00:00
|
|
|
], function( QUnit, $, testHelper ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2009-02-02 14:36:08 +00:00
|
|
|
|
2020-05-16 07:16:24 +00:00
|
|
|
var beforeAfterEach = testHelper.beforeAfterEach;
|
|
|
|
|
|
|
|
QUnit.module( "datepicker: events", beforeAfterEach() );
|
2009-02-02 14:36:08 +00:00
|
|
|
|
2012-04-19 15:17:35 +00:00
|
|
|
var selectedThis = null,
|
|
|
|
selectedDate = null,
|
|
|
|
selectedInst = null;
|
2009-02-04 04:27:14 +00:00
|
|
|
|
2015-08-21 04:11:33 +00:00
|
|
|
function callback( date, inst ) {
|
2009-02-04 04:27:14 +00:00
|
|
|
selectedThis = this;
|
|
|
|
selectedDate = date;
|
|
|
|
selectedInst = inst;
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:11:33 +00:00
|
|
|
function callback2( year, month, inst ) {
|
2009-02-04 04:27:14 +00:00
|
|
|
selectedThis = this;
|
2012-12-26 13:08:48 +00:00
|
|
|
selectedDate = year + "/" + month;
|
2009-02-04 04:27:14 +00:00
|
|
|
selectedInst = inst;
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
QUnit.test( "events", function( assert ) {
|
|
|
|
assert.expect( 26 );
|
2012-04-19 15:17:35 +00:00
|
|
|
var dateStr, newMonthYear, inp2,
|
2015-08-21 04:11:33 +00:00
|
|
|
inp = testHelper.init( "#inp", { onSelect: callback } ),
|
2012-04-19 15:17:35 +00:00
|
|
|
date = new Date();
|
2015-08-21 04:11:33 +00:00
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// OnSelect
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.val( "" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedThis, inp[ 0 ], "Callback selected this" );
|
|
|
|
assert.equal( selectedInst, $.data( inp[ 0 ], testHelper.PROP_NAME ), "Callback selected inst" );
|
|
|
|
assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback selected date" );
|
|
|
|
inp.val( "" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.DOWN } ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
|
|
|
|
date.setDate( date.getDate() + 7 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback selected date - ctrl+down" );
|
|
|
|
inp.val( "" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback selected date - esc" );
|
2016-04-03 15:11:33 +00:00
|
|
|
dateStr = "02/04/2008";
|
|
|
|
inp.val( dateStr ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
|
|
|
|
assert.equal( dateStr, selectedDate,
|
|
|
|
"onSelect is called after enter keydown" );
|
2015-08-21 04:11:33 +00:00
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// OnChangeMonthYear
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.datepicker( "option", { onChangeMonthYear: callback2, onSelect: null } ).
|
|
|
|
val( "" ).datepicker( "show" );
|
|
|
|
newMonthYear = function( date ) {
|
|
|
|
return date.getFullYear() + "/" + ( date.getMonth() + 1 );
|
2009-02-04 04:27:14 +00:00
|
|
|
};
|
|
|
|
date = new Date();
|
2015-08-21 04:11:33 +00:00
|
|
|
date.setDate( 1 );
|
|
|
|
inp.simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP } );
|
|
|
|
date.setMonth( date.getMonth() - 1 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedThis, inp[ 0 ], "Callback change month/year this" );
|
|
|
|
assert.equal( selectedInst, $.data( inp[ 0 ], testHelper.PROP_NAME ), "Callback change month/year inst" );
|
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year date - pgup" );
|
|
|
|
inp.simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN } );
|
|
|
|
date.setMonth( date.getMonth() + 1 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year date - pgdn" );
|
|
|
|
inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } );
|
|
|
|
date.setFullYear( date.getFullYear() - 1 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year date - ctrl+pgup" );
|
|
|
|
inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.HOME } );
|
|
|
|
date.setFullYear( date.getFullYear() + 1 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year date - ctrl+home" );
|
|
|
|
inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_DOWN } );
|
|
|
|
date.setFullYear( date.getFullYear() + 1 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year date - ctrl+pgdn" );
|
|
|
|
inp.datepicker( "setDate", new Date( 2007, 1 - 1, 26 ) );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, "2007/1", "Callback change month/year date - setDate" );
|
2009-02-04 04:27:14 +00:00
|
|
|
selectedDate = null;
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.datepicker( "setDate", new Date( 2007, 1 - 1, 12 ) );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.ok( selectedDate == null, "Callback change month/year date - setDate no change" );
|
2015-08-21 04:11:33 +00:00
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// OnChangeMonthYear step by 2
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.datepicker( "option", { stepMonths: 2 } ).
|
|
|
|
datepicker( "hide" ).val( "" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP } );
|
|
|
|
date.setMonth( date.getMonth() - 14 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year by 2 date - pgup" );
|
|
|
|
inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } );
|
|
|
|
date.setMonth( date.getMonth() - 12 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year by 2 date - ctrl+pgup" );
|
|
|
|
inp.simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN } );
|
|
|
|
date.setMonth( date.getMonth() + 2 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year by 2 date - pgdn" );
|
|
|
|
inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_DOWN } );
|
|
|
|
date.setMonth( date.getMonth() + 12 );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, newMonthYear( date ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback change month/year by 2 date - ctrl+pgdn" );
|
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// OnClose
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.datepicker( "option", { onClose: callback, onChangeMonthYear: null, stepMonths: 1 } ).
|
|
|
|
val( "" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedThis, inp[ 0 ], "Callback close this" );
|
|
|
|
assert.equal( selectedInst, $.data( inp[ 0 ], testHelper.PROP_NAME ), "Callback close inst" );
|
|
|
|
assert.equal( selectedDate, "", "Callback close date - esc" );
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.val( "" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", new Date() ),
|
2015-08-21 04:11:33 +00:00
|
|
|
"Callback close date - enter" );
|
|
|
|
inp.val( "02/04/2008" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, "02/04/2008", "Callback close date - preset" );
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.val( "02/04/2008" ).datepicker( "show" ).
|
|
|
|
simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.END } );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedDate, "", "Callback close date - ctrl+end" );
|
2015-08-21 04:11:33 +00:00
|
|
|
|
|
|
|
inp2 = testHelper.init( "#inp2" );
|
|
|
|
inp2.datepicker().datepicker( "option", { onClose: callback } ).datepicker( "show" );
|
|
|
|
inp.datepicker( "show" );
|
2016-04-03 15:11:33 +00:00
|
|
|
assert.equal( selectedThis, inp2[ 0 ], "Callback close this" );
|
2015-08-21 04:11:33 +00:00
|
|
|
} );
|
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
QUnit.test( "beforeShowDay-getDate", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2021-06-06 22:58:12 +00:00
|
|
|
var inp = testHelper.init( "#inp", {
|
|
|
|
beforeShowDay: function() {
|
|
|
|
inp.datepicker( "getDate" ); return [ true, "" ];
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
var dp = $( "#ui-datepicker-div" );
|
2015-08-21 04:11:33 +00:00
|
|
|
inp.val( "01/01/2010" ).datepicker( "show" );
|
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// Contains non-breaking space
|
|
|
|
assert.equal( $( "div.ui-datepicker-title" ).text(),
|
2015-08-21 04:11:33 +00:00
|
|
|
|
|
|
|
// Support: IE <9, jQuery <1.8
|
2012-11-15 23:57:44 +00:00
|
|
|
// In IE7/8 with jQuery <1.8, encoded spaces behave in strange ways
|
2015-08-21 04:11:33 +00:00
|
|
|
$( "<span>January 2010</span>" ).text(), "Initial month" );
|
|
|
|
$( "a.ui-datepicker-next", dp ).trigger( "click" );
|
|
|
|
$( "a.ui-datepicker-next", dp ).trigger( "click" );
|
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// Contains non-breaking space
|
|
|
|
assert.equal( $( "div.ui-datepicker-title" ).text(),
|
2015-08-21 04:11:33 +00:00
|
|
|
$( "<span>March 2010</span>" ).text(), "After next clicks" );
|
|
|
|
inp.datepicker( "hide" ).datepicker( "show" );
|
|
|
|
$( "a.ui-datepicker-prev", dp ).trigger( "click" );
|
|
|
|
$( "a.ui-datepicker-prev", dp ).trigger( "click" );
|
|
|
|
|
2016-04-03 15:11:33 +00:00
|
|
|
// Contains non-breaking space
|
|
|
|
assert.equal( $( "div.ui-datepicker-title" ).text(),
|
2015-08-21 04:11:33 +00:00
|
|
|
$( "<span>November 2009</span>" ).text(), "After prev clicks" );
|
|
|
|
inp.datepicker( "hide" );
|
|
|
|
} );
|
2012-11-09 21:27:45 +00:00
|
|
|
|
2015-04-07 00:06:31 +00:00
|
|
|
} );
|