From 2963622420abbd5752b8fc15af1cadba4ae8f011 Mon Sep 17 00:00:00 2001 From: Felix Nagel Date: Sun, 22 May 2016 21:48:51 +0200 Subject: [PATCH] Datepicker tests: Use setup and teardown methods, remove helper file --- tests/unit/datepicker/core.js | 211 +++++++++++++------------- tests/unit/datepicker/datepicker.html | 1 - tests/unit/datepicker/events.js | 120 ++++++++------- tests/unit/datepicker/helper.js | 19 --- tests/unit/datepicker/methods.js | 126 +++++++-------- tests/unit/datepicker/options.js | 94 +++++++----- 6 files changed, 277 insertions(+), 294 deletions(-) delete mode 100644 tests/unit/datepicker/helper.js diff --git a/tests/unit/datepicker/core.js b/tests/unit/datepicker/core.js index f884e9680..46f2a1989 100644 --- a/tests/unit/datepicker/core.js +++ b/tests/unit/datepicker/core.js @@ -1,126 +1,122 @@ define( [ "jquery", - "./helper", "ui/widgets/datepicker" -], function( $, testHelper ) { - -module( "datepicker: core" ); +], 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 ); - var input = $( "#datepicker" ).val( "1/1/14" ).datepicker(), - picker = input.datepicker( "widget" ); - - input.datepicker( "open" ); + this.element = $( "" ).appendTo( "#qunit-fixture" ); + this.element.val( "1/1/14" ).datepicker(); + this.widget = this.element.datepicker( "widget" ); - 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" ); + this.element.datepicker( "open" ); - input.val( "" ).datepicker( "destroy" ); + 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 input = testHelper.initNewInput(), - widget = input.datepicker( "widget" ); + var that = this; - input.focus(); + this.element.focus(); setTimeout( function() { - ok( widget.is( ":visible" ), "Datepicker visible" ); - equal( widget.children().length, 3, "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" ); + 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" ); - input.datepicker( "close" ); + that.element.datepicker( "close" ); start(); }, 50 ); } ); -asyncTest( "Keyboard handling: input", function( assert ) { - expect( 10 ); - var picker, instance, - input = $( "#datepicker" ).datepicker(); +asyncTest( "Keyboard handling: focus", function() { + expect( 2 ); - function step1() { - testHelper.init( input ); - picker = input.datepicker( "widget" ); + var that = this; - ok( !picker.is( ":visible" ), "datepicker closed" ); + ok( !this.widget.is( ":visible" ), "datepicker closed" ); - input.val( "" ).focus(); - setTimeout( function() { - ok( picker.is( ":visible" ), "Datepicker opens when receiving focus" ); - input.datepicker( "destroy" ); - step2(); - }, 100 ); - } + this.element.focus(); + setTimeout( function() { + ok( that.widget.is( ":visible" ), "Datepicker opens when receiving focus" ); + start(); + }, 100 ); +} ); - function step2() { - testHelper.init( input ); - picker = input.datepicker( "widget" ); +asyncTest( "Keyboard handling: keystroke up", function() { + expect( 2 ); - ok( !picker.is( ":visible" ), "datepicker closed" ); + var that = this; - input.val( "" ).simulate( "keydown", { keyCode: $.ui.keyCode.UP } ); - setTimeout( function() { - ok( picker.is( ":visible" ), "Keystroke up opens datepicker" ); - input.datepicker( "destroy" ); - step3(); - }, 100 ); - } + ok( !this.widget.is( ":visible" ), "datepicker closed" ); - function step3() { - 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 } ); - assert.dateEqual( 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 } ); - assert.dateEqual( 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 } ); - assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), - "Keystroke esc - abandoned" ); - - input - .val( "1/2/14" ) - .simulate( "keyup" ); - assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 2 ), - "Picker updated as user types into input" ); - - input.datepicker( "destroy" ); + this.element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } ); + setTimeout( function() { + ok( that.widget.is( ":visible" ), "Keystroke up opens datepicker" ); start(); - } + }, 100 ); +} ); - step1(); +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 @@ -131,37 +127,36 @@ test( "ARIA", function() { asyncTest( "mouse", function( assert ) { expect( 4 ); - var input = testHelper.init( $( "#datepicker" ).val( "" ) ), - picker = input.datepicker( "widget" ); + var that = this; - input.datepicker( "open" ); + this.element.datepicker( "open" ); setTimeout( function() { - input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); - $( ".ui-calendar-calendar tbody button:contains(12)", picker ).simulate( "mousedown", {} ); + that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); + $( ".ui-calendar-calendar tbody button:contains(12)", that.widget ).simulate( "mousedown", {} ); assert.dateEqual( - input.datepicker( "valueAsDate" ), + that.element.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" ); + that.element.val( "" ).datepicker( "refresh" ); + that.element.simulate( "click" ); + strictEqual( that.element.datepicker( "valueAsDate" ), null, "Mouse click - close" ); - input.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); - input.simulate( "click" ); + that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" ); + that.element.simulate( "click" ); assert.dateEqual( - input.datepicker( "valueAsDate" ), + that.element.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" ); + 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( - input.datepicker( "valueAsDate" ), + that.element.datepicker( "valueAsDate" ), new Date( 2008, 4 - 1, 4 ), "Mouse click - abandoned" ); diff --git a/tests/unit/datepicker/datepicker.html b/tests/unit/datepicker/datepicker.html index 3b20272b1..ae9348700 100644 --- a/tests/unit/datepicker/datepicker.html +++ b/tests/unit/datepicker/datepicker.html @@ -14,7 +14,6 @@
-
diff --git a/tests/unit/datepicker/events.js b/tests/unit/datepicker/events.js index 902896f2a..eb327f7f7 100644 --- a/tests/unit/datepicker/events.js +++ b/tests/unit/datepicker/events.js @@ -1,25 +1,34 @@ define( [ "jquery", - "./helper", "ui/widgets/datepicker" -], function( $, testHelper ) { - -module( "datepicker: events" ); +], function( $ ) { + +module( "datepicker: events", { + setup: function() { + this.element = $( "#datepicker" ).datepicker( { show: false, hide: false } ); + this.widget = this.element.datepicker( "widget" ); + }, + teardown: function() { + this.element.datepicker( "destroy" ).val( "" ); + } +} ); test( "beforeOpen", function() { expect( 3 ); - var input = testHelper.init( "#datepicker", { - beforeOpen: function() { - ok( true, "beforeOpen event fired before open" ); - ok( input.datepicker( "widget" ).is( ":hidden" ), "calendar hidden on beforeOpen" ); - }, - open: function() { - ok( input.datepicker( "widget" ).is( ":visible" ), "calendar open on open" ); - } - } ); + var that = this; + + this.element.datepicker( { + beforeOpen: function() { + ok( true, "beforeOpen event fired before open" ); + ok( that.element.datepicker( "widget" ).is( ":hidden" ), "calendar hidden on beforeOpen" ); + }, + open: function() { + ok( that.element.datepicker( "widget" ).is( ":visible" ), "calendar open on open" ); + } + } ); - input + this.element .datepicker( "open" ) .datepicker( "close" ) .datepicker( "option", { @@ -36,98 +45,101 @@ test( "beforeOpen", function() { test( "close", function() { expect( 4 ); - var shouldFire, - input = testHelper.init( "#datepicker", { - close: function() { - ok( shouldFire, "close event fired" ); - } - } ); + var shouldFire; + + this.element.datepicker( { + close: function() { + ok( shouldFire, "close event fired" ); + } + } ); shouldFire = false; - input.datepicker( "open" ); + this.element.datepicker( "open" ); shouldFire = true; - input.datepicker( "close" ); + this.element.datepicker( "close" ); shouldFire = false; - input.datepicker( "open" ); + this.element.datepicker( "open" ); shouldFire = true; $( "body" ).trigger( "mousedown" ); shouldFire = false; - input.datepicker( "open" ); + this.element.datepicker( "open" ); shouldFire = true; - input.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); + this.element.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); shouldFire = false; - input.datepicker( "open" ); + this.element.datepicker( "open" ); shouldFire = true; - input.datepicker( "widget" ).find( "tbody tr:first button:first" ).simulate( "mousedown" ); + this.element.datepicker( "widget" ).find( "tbody tr:first button:first" ).simulate( "mousedown" ); } ); test( "open", function() { expect( 2 ); - var input = testHelper.init( "#datepicker", { - open: function() { - ok( true, "open event fired on open" ); - ok( widget.is( ":visible" ), "calendar open on open" ); - } - } ), - widget = input.datepicker( "widget" ); + var that = this; - input.datepicker( "open" ); + this.element.datepicker( { + open: function() { + ok( true, "open event fired on open" ); + ok( that.widget.is( ":visible" ), "calendar open on open" ); + } + } ); + + this.element.datepicker( "open" ); } ); asyncTest( "select", function() { expect( 4 ); - var input = testHelper.init( "#datepicker", { - select: function( event ) { - ok( true, "select event fired " + message ); - equal( - event.originalEvent.type, - "calendarselect", - "select originalEvent " + message - ); - } - } ), - widget = input.datepicker( "widget" ), - message = ""; + var message = "", + that = this; + + this.element.datepicker( { + select: function( event ) { + ok( true, "select event fired " + message ); + equal( + event.originalEvent.type, + "calendarselect", + "select originalEvent " + message + ); + } + } ); function step1() { message = "on calendar cell click"; - input + that.element .simulate( "focus" ) .simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); setTimeout( function() { - widget.find( "tbody tr:first button:first" ).simulate( "mousedown" ); - input.datepicker( "close" ); + that.widget.find( "tbody tr:first button:first" ).simulate( "mousedown" ); + that.element.datepicker( "close" ); step2(); }, 100 ); } function step2() { message = "on calendar cell enter"; - input + that.element .simulate( "focus" ) .simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); setTimeout( function() { $( document.activeElement ) .simulate( "keydown", { keyCode: $.ui.keyCode.RIGHT } ) .simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } ); - input.datepicker( "close" ); + that.element.datepicker( "close" ); step3(); }, 100 ); } function step3() { message = "on calendar escape (not expected)"; - input + that.element .simulate( "focus" ) .simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); setTimeout( function() { $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); - input.datepicker( "close" ); + that.element.datepicker( "close" ); start(); }, 100 ); } diff --git a/tests/unit/datepicker/helper.js b/tests/unit/datepicker/helper.js deleted file mode 100644 index da75cf770..000000000 --- a/tests/unit/datepicker/helper.js +++ /dev/null @@ -1,19 +0,0 @@ -define( [ - "jquery", - "lib/helper", - "ui/widgets/datepicker" -], function( $, helper ) { - -return $.extend( helper, { - init: function( id, options ) { - options = $.extend( { show: false, hide: false }, options || {} ); - return $( id ).datepicker( options ); - }, - initNewInput: function( options ) { - options = $.extend( { show: false, hide: false }, options || {} ); - return $( "" ).datepicker( options ) - .appendTo( "#qunit-fixture" ); - } -} ); - -} ); diff --git a/tests/unit/datepicker/methods.js b/tests/unit/datepicker/methods.js index a274de3f9..3ce146861 100644 --- a/tests/unit/datepicker/methods.js +++ b/tests/unit/datepicker/methods.js @@ -1,15 +1,23 @@ define( [ "jquery", - "./helper", "ui/widgets/datepicker" -], function( $, testHelper ) { - -module( "datepicker: methods" ); +], function( $ ) { + +module( "datepicker: methods", { + setup: function() { + this.element = $( "#datepicker" ).datepicker( { show: false, hide: false } ); + this.widget = this.element.datepicker( "widget" ); + }, + teardown: function() { + this.element.datepicker( "destroy" ).val( "" ); + } +} ); test( "destroy", function( assert ) { expect( 3 ); - var input = $( "#datepicker" ); + var input = $( "" ).appendTo( "#qunit-fixture" ); + assert.domEqual( input, function() { input.datepicker(); ok( input.attr( "aria-owns" ), "aria-owns attribute added" ); @@ -21,108 +29,86 @@ test( "destroy", function( assert ) { test( "enable / disable", function() { expect( 10 ); - var input = testHelper.init( "#datepicker" ), - calendar = input.datepicker( "widget" ); - - input.datepicker( "disable" ); - ok( input.datepicker( "option", "disabled" ), "disabled option is set" ); - ok( calendar.hasClass( "ui-datepicker-disabled" ), "has disabled widget class name" ); - ok( input.hasClass( "ui-state-disabled" ), "has disabled state class name" ); - equal( input.attr( "aria-disabled" ), "true", "has ARIA disabled" ); - equal( input.attr( "disabled" ), "disabled", "input disabled" ); - - input.datepicker( "enable" ); - ok( !input.datepicker( "option", "disabled" ), "enabled after enable() call" ); - ok( !calendar.hasClass( "ui-datepicker-disabled" ), "no longer has disabled widget class name" ); - ok( !input.hasClass( "ui-state-disabled" ), "no longer has disabled state class name" ); - equal( input.attr( "aria-disabled" ), "false", "no longer has ARIA disabled" ); - equal( input.attr( "disabled" ), undefined, "input no longer disabled" ); + this.element.datepicker( "disable" ); + ok( this.element.datepicker( "option", "disabled" ), "disabled option is set" ); + ok( this.widget.hasClass( "ui-datepicker-disabled" ), "has disabled widget class name" ); + ok( this.element.hasClass( "ui-state-disabled" ), "has disabled state class name" ); + equal( this.element.attr( "aria-disabled" ), "true", "has ARIA disabled" ); + equal( this.element.attr( "disabled" ), "disabled", "input disabled" ); + + this.element.datepicker( "enable" ); + ok( !this.element.datepicker( "option", "disabled" ), "enabled after enable() call" ); + ok( !this.widget.hasClass( "ui-datepicker-disabled" ), "no longer has disabled widget class name" ); + ok( !this.element.hasClass( "ui-state-disabled" ), "no longer has disabled state class name" ); + equal( this.element.attr( "aria-disabled" ), "false", "no longer has ARIA disabled" ); + equal( this.element.attr( "disabled" ), undefined, "input no longer disabled" ); } ); test( "widget", function() { expect( 1 ); - var actual = $( "#datepicker" ).datepicker().datepicker( "widget" ); - deepEqual( $( "body > .ui-front" )[ 0 ], actual[ 0 ] ); - actual.remove(); + deepEqual( $( "body > .ui-front" )[ 0 ], this.widget[ 0 ] ); + this.widget.remove(); } ); test( "open / close", function() { expect( 7 ); - var input = testHelper.initNewInput( { show: false, hide: false } ), - calendar = input.datepicker( "widget" ); - - ok( calendar.is( ":hidden" ), "calendar hidden on init" ); + ok( this.widget.is( ":hidden" ), "calendar hidden on init" ); - input.datepicker( "open" ); - ok( calendar.is( ":visible" ), "open: calendar visible" ); - equal( calendar.attr( "aria-hidden" ), "false", "open: calendar aria-hidden" ); - equal( calendar.attr( "aria-expanded" ), "true", "close: calendar aria-expanded" ); + this.element.datepicker( "open" ); + ok( this.widget.is( ":visible" ), "open: calendar visible" ); + equal( this.widget.attr( "aria-hidden" ), "false", "open: calendar aria-hidden" ); + equal( this.widget.attr( "aria-expanded" ), "true", "close: calendar aria-expanded" ); - input.datepicker( "close" ); - ok( !calendar.is( ":visible" ), "close: calendar hidden" ); - equal( calendar.attr( "aria-hidden" ), "true", "close: calendar aria-hidden" ); - equal( calendar.attr( "aria-expanded" ), "false", "close: calendar aria-expanded" ); + this.element.datepicker( "close" ); + ok( !this.widget.is( ":visible" ), "close: calendar hidden" ); + equal( this.widget.attr( "aria-hidden" ), "true", "close: calendar aria-hidden" ); + equal( this.widget.attr( "aria-expanded" ), "false", "close: calendar aria-expanded" ); } ); test( "value", function() { expect( 4 ); - var input = $( "#datepicker" ).datepicker(), - picker = input.datepicker( "widget" ); + this.element.datepicker( "value", "1/1/14" ); + equal( this.element.val(), "1/1/14", "input's value set" ); - input.datepicker( "value", "1/1/14" ); - equal( input.val(), "1/1/14", "input's value set" ); - - input.datepicker( "open" ); + this.element.datepicker( "open" ); ok( - picker.find( "button[data-timestamp]" ).eq( 0 ).hasClass( "ui-state-active" ), + this.widget.find( "button[data-timestamp]" ).eq( 0 ).hasClass( "ui-state-active" ), "first day marked as selected" ); - equal( input.datepicker( "value" ), "1/1/14", "getter" ); + equal( this.element.datepicker( "value" ), "1/1/14", "getter" ); - input.val( "abc" ); - strictEqual( input.datepicker( "value" ), null, "Invalid values should return null." ); + this.element.val( "abc" ); + strictEqual( this.element.datepicker( "value" ), null, "Invalid values should return null." ); } ); test( "valueAsDate", function( assert ) { - expect( 6 ); + expect( 5 ); - var input = testHelper.init( "#datepicker" ), - picker = input.datepicker( "widget" ), - date1 = new Date( 2008, 6 - 1, 4 ); + strictEqual( this.element.datepicker( "valueAsDate" ), null, "Default" ); - input.datepicker( "valueAsDate", new Date( 2014, 0, 1 ) ); - equal( input.val(), "1/1/14", "Input's value set" ); + this.element.datepicker( "valueAsDate", new Date( 2014, 0, 1 ) ); + equal( this.element.val(), "1/1/14", "Input's value set" ); ok( - picker.find( "button[data-timestamp]" ).eq( 0 ).hasClass( "ui-state-active" ), + this.widget.find( "button[data-timestamp]" ).eq( 0 ).hasClass( "ui-state-active" ), "First day marked as selected" ); - assert.dateEqual( input.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Getter" ); - - input.val( "a/b/c" ); - equal( input.datepicker( "valueAsDate" ), null, "Invalid dates return null" ); + assert.dateEqual( this.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ), "Getter" ); - input.val( "" ).datepicker( "destroy" ); - input = testHelper.init( "#datepicker" ); - - strictEqual( input.datepicker( "valueAsDate" ), null, "Set date - default" ); - input.datepicker( "valueAsDate", date1 ); - assert.dateEqual( input.datepicker( "valueAsDate" ), date1, "Set date - 2008-06-04" ); + this.element.val( "a/b/c" ); + equal( this.element.datepicker( "valueAsDate" ), null, "Invalid dates return null" ); } ); test( "isValid", function() { expect( 2 ); - var input = $( "#datepicker" ).datepicker(); - - input.val( "1/1/14" ); - ok( input.datepicker( "isValid" ) ); - input.val( "1/1/abc" ); - ok( !input.datepicker( "isValid" ) ); + this.element.val( "1/1/14" ); + ok( this.element.datepicker( "isValid" ) ); - input.datepicker( "destroy" ); + this.element.val( "1/1/abc" ); + ok( !this.element.datepicker( "isValid" ) ); } ); } ); diff --git a/tests/unit/datepicker/options.js b/tests/unit/datepicker/options.js index 6021f1b7e..f04b3fb4d 100644 --- a/tests/unit/datepicker/options.js +++ b/tests/unit/datepicker/options.js @@ -1,47 +1,51 @@ define( [ "jquery", - "./helper", "ui/widgets/datepicker" -], function( $, testHelper ) { - -module( "datepicker: options" ); +], function( $ ) { + +module( "datepicker: options", { + setup: function() { + this.element = $( "#datepicker" ).datepicker( { show: false, hide: false } ); + this.widget = this.element.datepicker( "widget" ); + }, + teardown: function() { + this.element.datepicker( "destroy" ).val( "" ); + } +} ); test( "appendTo", function() { expect( 6 ); - var container, - detached = $( "
" ), - input = $( "#datepicker" ); - input.datepicker(); - container = input.datepicker( "widget" ).parent()[ 0 ]; + var container = this.widget.parent()[ 0 ], + detached = $( "
" ); + equal( container, document.body, "defaults to body" ); - input.datepicker( "destroy" ); + this.element.datepicker( "destroy" ); - input.datepicker( { appendTo: "#qunit-fixture" } ); - container = input.datepicker( "widget" ).parent()[ 0 ]; + this.element.datepicker( { appendTo: "#qunit-fixture" } ); + container = this.element.datepicker( "widget" ).parent()[ 0 ]; equal( container, $( "#qunit-fixture" )[ 0 ], "child of specified element" ); - input.datepicker( "destroy" ); + this.element.datepicker( "destroy" ); - input.datepicker( { appendTo: "#does-not-exist" } ); - container = input.datepicker( "widget" ).parent()[ 0 ]; + this.element.datepicker( { appendTo: "#does-not-exist" } ); + container = this.element.datepicker( "widget" ).parent()[ 0 ]; equal( container, document.body, "set to body if element does not exist" ); - input.datepicker( "destroy" ); + this.element.datepicker( "destroy" ); - input.datepicker() + this.element.datepicker() .datepicker( "option", "appendTo", "#qunit-fixture" ); - container = input.datepicker( "widget" ).parent()[ 0 ]; + container = this.element.datepicker( "widget" ).parent()[ 0 ]; equal( container, $( "#qunit-fixture" )[ 0 ], "modified after init" ); - input.datepicker( "destroy" ); + this.element.datepicker( "destroy" ); - input.datepicker( { appendTo: detached } ); - container = input.datepicker( "widget" ).parent()[ 0 ]; + this.element.datepicker( { appendTo: detached } ); + container = this.element.datepicker( "widget" ).parent()[ 0 ]; equal( container, detached[ 0 ], "detached jQuery object" ); - input.datepicker( "destroy" ); + this.element.datepicker( "destroy" ); - input.datepicker( { appendTo: detached[ 0 ] } ); - container = input.datepicker( "widget" ).parent()[ 0 ]; + this.element.datepicker( { appendTo: detached[ 0 ] } ); + container = this.element.datepicker( "widget" ).parent()[ 0 ]; equal( container, detached[ 0 ], "detached DOM element" ); - input.datepicker( "destroy" ); } ); test( "Pass-through options", function() { @@ -58,14 +62,14 @@ test( "Pass-through options", function() { numberOfMonths: 3, showWeek: true }, - input = $( "#datepicker" ).val( "1/1/14" ).datepicker(), - datepickerInstance = input.datepicker( "instance" ); + input = $( "" ).val( "1/1/14" ).appendTo( "#qunit-fixture" ).datepicker(), + instance = input.datepicker( "instance" ); $.each( options, function( key, value ) { input.datepicker( "option", key, value ); deepEqual( - datepickerInstance.calendar.calendar( "option", key ), + instance.calendar.calendar( "option", key ), value, "option " + key + ": correct value" ); @@ -82,6 +86,7 @@ test( "Pass-through options", function() { asyncTest( "position", function( assert ) { expect( 3 ); + var input = $( "" ).datepicker().appendTo( "body" ).css( { position: "absolute", top: 0, @@ -108,34 +113,39 @@ asyncTest( "position", function( assert ) { } ); } ); -test( "Stop datepicker from appearing with beforeOpen event handler", function() { - expect( 3 ); +test( "Stop datepicker from appearing with beforeOpen event handler - nothing", function() { + expect( 1 ); - var input = testHelper.init( "#datepicker", { + this.element.datepicker( { beforeOpen: function() {} } ); - input.datepicker( "open" ); - ok( input.datepicker( "widget" ).is( ":visible" ), "beforeOpen returns nothing" ); - input.datepicker( "close" ).datepicker( "destroy" ); + this.element.datepicker( "open" ); + ok( this.element.datepicker( "widget" ).is( ":visible" ), "beforeOpen returns nothing" ); +} ); + +test( "Stop datepicker from appearing with beforeOpen event handler - true", function() { + expect( 1 ); - input = testHelper.init( "#datepicker", { + this.element.datepicker( { beforeOpen: function() { return true; } } ); - input.datepicker( "open" ); - ok( input.datepicker( "widget" ).is( ":visible" ), "beforeOpen returns true" ); - input.datepicker( "close" ).datepicker( "destroy" ); + this.element.datepicker( "open" ); + ok( this.element.datepicker( "widget" ).is( ":visible" ), "beforeOpen returns true" ); +} ); + +test( "Stop datepicker from appearing with beforeOpen event handler - false", function() { + expect( 1 ); - input = testHelper.init( "#datepicker", { + this.element.datepicker( { beforeOpen: function() { return false; } } ); - input.datepicker( "open" ); - ok( !input.datepicker( "widget" ).is( ":visible" ), "beforeOpen returns false" ); - input.datepicker( "destroy" ); + this.element.datepicker( "open" ); + ok( !this.element.datepicker( "widget" ).is( ":visible" ), "beforeOpen returns false" ); } ); } ); -- 2.39.5