define( [ "jquery", "ui/widgets/datepicker" ], function( $ ) { module( "datepicker: core", { setup: function() { this.element = $( "#datepicker" ).datepicker( { show: false, hide: false } ); this.widget = this.element.datepicker( "widget" ); }, teardown: function() { this.element.datepicker( "destroy" ).val( "" ); } } ); test( "input's value determines starting date", function() { expect( 3 ); this.element = $( "" ).appendTo( "#qunit-fixture" ); this.element.val( "1/1/14" ).datepicker(); this.widget = this.element.datepicker( "widget" ); this.element.datepicker( "open" ); equal( this.widget.find( ".ui-calendar-month" ).html(), "January", "correct month displayed" ); equal( this.widget.find( ".ui-calendar-year" ).html(), "2014", "correct year displayed" ); equal( this.widget.find( ".ui-state-active" ).html(), "1", "correct day highlighted" ); } ); asyncTest( "base structure", function() { expect( 5 ); var that = this; this.element.focus(); setTimeout( function() { ok( that.widget.is( ":visible" ), "Datepicker visible" ); equal( that.widget.children().length, 3, "Child count" ); ok( that.widget.is( ".ui-calendar" ), "Class ui-calendar" ); ok( that.widget.is( ".ui-datepicker" ), "Class ui-datepicker" ); ok( that.widget.is( ".ui-front" ), "Class ui-front" ); that.element.datepicker( "close" ); start(); }, 50 ); } ); asyncTest( "Keyboard handling: focus", function() { expect( 2 ); var that = this; ok( !this.widget.is( ":visible" ), "datepicker closed" ); this.element.focus(); setTimeout( function() { ok( that.widget.is( ":visible" ), "Datepicker opens when receiving focus" ); start(); }, 100 ); } ); asyncTest( "Keyboard handling: keystroke up", function() { expect( 2 ); var that = this; ok( !this.widget.is( ":visible" ), "datepicker closed" ); this.element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } ); setTimeout( function() { ok( that.widget.is( ":visible" ), "Keystroke up opens datepicker" ); start(); }, 100 ); } ); test( "Keyboard handling: input", function( assert ) { expect( 6 ); var that = this, instance = that.element.datepicker( "instance" ); // Enter = Select preset date that.element .val( "1/1/14" ) .datepicker( "refresh" ) .datepicker( "open" ) .simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Keystroke enter - preset" ); that.element .val( "" ) .datepicker( "open" ); ok( instance.isOpen, "datepicker is open before escape" ); that.element.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); ok( !instance.isOpen, "escape closes the datepicker" ); that.element .val( "1/1/14" ) .datepicker( "open" ) .simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Keystroke esc - preset" ); that.element .val( "1/1/14" ) .datepicker( "open" ) .simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } ) .simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Keystroke esc - abandoned" ); that.element .val( "1/2/14" ) .simulate( "keyup" ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 2 ), "Picker updated as user types into input" ); } ); // TODO: implement test( "ARIA", function() { expect( 0 ); } ); asyncTest( "mouse", function( assert ) { expect( 4 ); var that = this; this.element.datepicker( "open" ); setTimeout( function() { that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); $( ".ui-calendar-calendar tbody button:contains(12)", that.widget ).simulate( "mousedown", {} ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 12 ), "Mouse click - preset" ); that.element.val( "" ).datepicker( "refresh" ); that.element.simulate( "click" ); strictEqual( that.element.datepicker( "valueAsDate" ), null, "Mouse click - close" ); that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); that.element.simulate( "click" ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 4 ), "Mouse click - close + preset" ); that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); that.widget.find( "a.ui-calendar-prev" ).simulate( "click" ); that.element.simulate( "click" ); assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 4 ), "Mouse click - abandoned" ); start(); }, 100 ); } ); } );