diff options
author | Amanpreet Singh <apsdehal@gmail.com> | 2016-04-03 20:21:14 +0530 |
---|---|---|
committer | Amanpreet Singh <apsdehal@gmail.com> | 2016-04-14 00:06:11 +0530 |
commit | fd1c236c0f85f1fdb5972c243d78cbf720f77434 (patch) | |
tree | f16a7b79175d6eb974639183c46589cfa5709175 /tests/unit/autocomplete/options.js | |
parent | f375fd1300b4e08c0e71a073131260dfb8e5762f (diff) | |
download | jquery-ui-fd1c236c0f85f1fdb5972c243d78cbf720f77434.tar.gz jquery-ui-fd1c236c0f85f1fdb5972c243d78cbf720f77434.zip |
Autocomplete: Shift to use no globals
Diffstat (limited to 'tests/unit/autocomplete/options.js')
-rw-r--r-- | tests/unit/autocomplete/options.js | 179 |
1 files changed, 92 insertions, 87 deletions
diff --git a/tests/unit/autocomplete/options.js b/tests/unit/autocomplete/options.js index 76c13e754..c3f445db1 100644 --- a/tests/unit/autocomplete/options.js +++ b/tests/unit/autocomplete/options.js @@ -1,108 +1,110 @@ define( [ + "qunit", "jquery", "ui/widgets/autocomplete" -], function( $ ) { +], function( QUnit, $ ) { -module( "autocomplete: options" ); +QUnit.module( "autocomplete: options" ); var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl" ]; -test( "appendTo: null", function() { - expect( 1 ); +QUnit.test( "appendTo: null", function( assert ) { + assert.expect( 1 ); var element = $( "#autocomplete" ).autocomplete(); - equal( element.autocomplete( "widget" ).parent()[ 0 ], document.body, + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], document.body, "defaults to body" ); element.autocomplete( "destroy" ); } ); -test( "appendTo: explicit", function() { - expect( 6 ); +QUnit.test( "appendTo: explicit", function( assert ) { + assert.expect( 6 ); var detached = $( "<div>" ), element = $( "#autocomplete" ); element.autocomplete( { appendTo: ".autocomplete-wrap" } ); - equal( element.autocomplete( "widget" ).parent()[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], $( "#autocomplete-wrap1" )[ 0 ], "first found element" ); - equal( $( "#autocomplete-wrap2 .ui-autocomplete" ).length, 0, + assert.equal( $( "#autocomplete-wrap2 .ui-autocomplete" ).length, 0, "only appends to one element" ); element.autocomplete( "destroy" ); element.autocomplete().autocomplete( "option", "appendTo", "#autocomplete-wrap1" ); - equal( element.autocomplete( "widget" ).parent()[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], $( "#autocomplete-wrap1" )[ 0 ], "modified after init" ); element.autocomplete( "destroy" ); element.autocomplete( { appendTo: detached } ); - equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ], "detached jQuery object" ); element.autocomplete( "destroy" ); element.autocomplete( { appendTo: detached[ 0 ] } ); - equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ], "detached DOM element" ); element.autocomplete( "destroy" ); element.autocomplete().autocomplete( "option", "appendTo", detached ); - equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ], "detached DOM element via option()" ); element.autocomplete( "destroy" ); } ); -test( "appendTo: ui-front", function() { - expect( 2 ); +QUnit.test( "appendTo: ui-front", function( assert ) { + assert.expect( 2 ); var element = $( "#autocomplete" ); $( "#autocomplete-wrap2" ).addClass( "ui-front" ); element.autocomplete(); - equal( element.autocomplete( "widget" ).parent()[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], $( "#autocomplete-wrap2" )[ 0 ], "null, inside .ui-front" ); element.autocomplete( "destroy" ); element.autocomplete( { appendTo: $() } ); - equal( element.autocomplete( "widget" ).parent()[ 0 ], + assert.equal( element.autocomplete( "widget" ).parent()[ 0 ], $( "#autocomplete-wrap2" )[ 0 ], "empty jQuery object, inside .ui-front" ); } ); -function autoFocusTest( afValue, focusedLength ) { +function autoFocusTest( assert, afValue, focusedLength ) { + var ready = assert.async(); var element = $( "#autocomplete" ).autocomplete( { autoFocus: afValue, delay: 0, source: data, open: function() { - equal( + assert.equal( element.autocomplete( "widget" ) .find( ".ui-menu-item-wrapper.ui-state-active" ) .length, focusedLength, "first item is " + ( afValue ? "" : "not" ) + " auto focused" ); - start(); + ready(); } } ); element.val( "ja" ).trigger( "keydown" ); - stop(); } -test( "autoFocus: false", function() { - expect( 1 ); - autoFocusTest( false, 0 ); +QUnit.test( "autoFocus: false", function( assert ) { + assert.expect( 1 ); + autoFocusTest( assert, false, 0 ); } ); -test( "autoFocus: true", function() { - expect( 1 ); - autoFocusTest( true, 1 ); +QUnit.test( "autoFocus: true", function( assert ) { + assert.expect( 1 ); + autoFocusTest( assert, true, 1 ); } ); -asyncTest( "delay", function() { - expect( 2 ); +QUnit.test( "delay", function( assert ) { + var ready = assert.async(); + assert.expect( 2 ); var element = $( "#autocomplete" ).autocomplete( { source: data, delay: 25 @@ -110,16 +112,17 @@ asyncTest( "delay", function() { menu = element.autocomplete( "widget" ); element.val( "ja" ).trigger( "keydown" ); - ok( menu.is( ":hidden" ), "menu is closed immediately after search" ); + assert.ok( menu.is( ":hidden" ), "menu is closed immediately after search" ); setTimeout( function() { - ok( menu.is( ":visible" ), "menu is open after delay" ); - start(); + assert.ok( menu.is( ":visible" ), "menu is open after delay" ); + ready(); }, 50 ); } ); -asyncTest( "disabled", function( assert ) { - expect( 5 ); +QUnit.test( "disabled", function( assert ) { + var ready = assert.async(); + assert.expect( 5 ); var element = $( "#autocomplete" ).autocomplete( { source: data, delay: 0 @@ -127,38 +130,39 @@ asyncTest( "disabled", function( assert ) { menu = element.autocomplete( "disable" ).autocomplete( "widget" ); element.val( "ja" ).trigger( "keydown" ); - ok( menu.is( ":hidden" ) ); + assert.ok( menu.is( ":hidden" ) ); assert.lacksClasses( element, "ui-state-disabled" ); assert.hasClasses( menu, "ui-autocomplete-disabled" ); - ok( !element.attr( "aria-disabled" ), "element doesn't get aria-disabled" ); + assert.ok( !element.attr( "aria-disabled" ), "element doesn't get aria-disabled" ); setTimeout( function() { - ok( menu.is( ":hidden" ) ); - start(); + assert.ok( menu.is( ":hidden" ) ); + ready(); } ); } ); -test( "minLength", function() { - expect( 2 ); +QUnit.test( "minLength", function( assert ) { + assert.expect( 2 ); var element = $( "#autocomplete" ).autocomplete( { source: data } ), menu = element.autocomplete( "widget" ); element.autocomplete( "search", "" ); - ok( menu.is( ":hidden" ), "blank not enough for minLength: 1" ); + assert.ok( menu.is( ":hidden" ), "blank not enough for minLength: 1" ); element.autocomplete( "option", "minLength", 0 ); element.autocomplete( "search", "" ); - ok( menu.is( ":visible" ), "blank enough for minLength: 0" ); + assert.ok( menu.is( ":visible" ), "blank enough for minLength: 0" ); } ); -asyncTest( "minLength, exceed then drop below", function() { - expect( 4 ); +QUnit.test( "minLength, exceed then drop below", function( assert ) { + var ready = assert.async(); + assert.expect( 4 ); var element = $( "#autocomplete" ).autocomplete( { minLength: 2, source: function( req, res ) { - equal( req.term, "12", "correct search term" ); + assert.equal( req.term, "12", "correct search term" ); setTimeout( function() { res( [ "item" ] ); } ); @@ -166,20 +170,20 @@ asyncTest( "minLength, exceed then drop below", function() { } ), menu = element.autocomplete( "widget" ); - ok( menu.is( ":hidden" ), "menu is hidden before first search" ); + assert.ok( menu.is( ":hidden" ), "menu is hidden before first search" ); element.autocomplete( "search", "12" ); - ok( menu.is( ":hidden" ), "menu is hidden before second search" ); + assert.ok( menu.is( ":hidden" ), "menu is hidden before second search" ); element.autocomplete( "search", "1" ); setTimeout( function() { - ok( menu.is( ":hidden" ), "menu is hidden after searches" ); - start(); + assert.ok( menu.is( ":hidden" ), "menu is hidden after searches" ); + ready(); } ); } ); -test( "minLength, exceed then drop below then exceed", function() { - expect( 3 ); +QUnit.test( "minLength, exceed then drop below then exceed", function( assert ) { + assert.expect( 3 ); var _res = [], element = $( "#autocomplete" ).autocomplete( { minLength: 2, @@ -190,11 +194,11 @@ test( "minLength, exceed then drop below then exceed", function() { menu = element.autocomplete( "widget" ); // Trigger a valid search - ok( menu.is( ":hidden" ), "menu is hidden before first search" ); + assert.ok( menu.is( ":hidden" ), "menu is hidden before first search" ); element.autocomplete( "search", "12" ); // Trigger a search below the minLength, to turn on cancelSearch flag - ok( menu.is( ":hidden" ), "menu is hidden before second search" ); + assert.ok( menu.is( ":hidden" ), "menu is hidden before second search" ); element.autocomplete( "search", "1" ); // Trigger a valid search @@ -206,46 +210,47 @@ test( "minLength, exceed then drop below then exceed", function() { // React to second search _res[ 1 ]( [ "13" ] ); - ok( menu.is( ":visible" ), "menu is visible after searches" ); + assert.ok( menu.is( ":visible" ), "menu is visible after searches" ); } ); -test( "source, local string array", function() { - expect( 1 ); +QUnit.test( "source, local string array", function( assert ) { + assert.expect( 1 ); var element = $( "#autocomplete" ).autocomplete( { source: data } ), menu = element.autocomplete( "widget" ); element.val( "ja" ).autocomplete( "search" ); - equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" ); + assert.equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" ); } ); -function sourceTest( source, async ) { +function sourceTest( assert, source, async ) { + var ready; var element = $( "#autocomplete" ).autocomplete( { source: source } ), menu = element.autocomplete( "widget" ); function result() { var items = menu.find( ".ui-menu-item" ); - equal( items.length, 3, "Should find three results." ); - deepEqual( items.eq( 0 ).data( "ui-autocomplete-item" ), { + assert.equal( items.length, 3, "Should find three results." ); + assert.deepEqual( items.eq( 0 ).data( "ui-autocomplete-item" ), { label: "java", value: "java" } ); - deepEqual( items.eq( 1 ).data( "ui-autocomplete-item" ), { + assert.deepEqual( items.eq( 1 ).data( "ui-autocomplete-item" ), { label: "javascript", value: "javascript" } ); - deepEqual( items.eq( 2 ).data( "ui-autocomplete-item" ), { + assert.deepEqual( items.eq( 2 ).data( "ui-autocomplete-item" ), { label: "clojure", value: "clojure" } ); element.autocomplete( "destroy" ); if ( async ) { - start(); + ready(); } } if ( async ) { - stop(); + ready = assert.async(); $( document ).one( "ajaxStop", result ); } element.val( "j" ).autocomplete( "search" ); @@ -254,9 +259,9 @@ function sourceTest( source, async ) { } } -test( "source, local object array, only labels", function() { - expect( 4 ); - sourceTest( [ +QUnit.test( "source, local object array, only labels", function( assert ) { + assert.expect( 4 ); + sourceTest( assert, [ { label: "java", value: null }, { label: "php", value: null }, { label: "coldfusion", value: "" }, @@ -265,9 +270,9 @@ test( "source, local object array, only labels", function() { ] ); } ); -test( "source, local object array, only values", function() { - expect( 4 ); - sourceTest( [ +QUnit.test( "source, local object array, only values", function( assert ) { + assert.expect( 4 ); + sourceTest( assert, [ { value: "java", label: null }, { value: "php", label: null }, { value: "coldfusion", label: "" }, @@ -276,25 +281,25 @@ test( "source, local object array, only values", function() { ] ); } ); -test( "source, url string with remote json string array", function() { - expect( 4 ); - sourceTest( "remote_string_array.txt", true ); +QUnit.test( "source, url string with remote json string array", function( assert ) { + assert.expect( 4 ); + sourceTest( assert, "remote_string_array.txt", true ); } ); -test( "source, url string with remote json object array, only value properties", function() { - expect( 4 ); - sourceTest( "remote_object_array_values.txt", true ); +QUnit.test( "source, url string with remote json object array, only value properties", function( assert ) { + assert.expect( 4 ); + sourceTest( assert, "remote_object_array_values.txt", true ); } ); -test( "source, url string with remote json object array, only label properties", function() { - expect( 4 ); - sourceTest( "remote_object_array_labels.txt", true ); +QUnit.test( "source, url string with remote json object array, only label properties", function( assert ) { + assert.expect( 4 ); + sourceTest( assert, "remote_object_array_labels.txt", true ); } ); -test( "source, custom", function() { - expect( 5 ); - sourceTest( function( request, response ) { - equal( request.term, "j" ); +QUnit.test( "source, custom", function( assert ) { + assert.expect( 5 ); + sourceTest( assert, function( request, response ) { + assert.equal( request.term, "j" ); response( [ "java", { label: "javascript", value: null }, @@ -303,17 +308,17 @@ test( "source, custom", function() { } ); } ); -test( "source, update after init", function() { - expect( 2 ); +QUnit.test( "source, update after init", function( assert ) { + assert.expect( 2 ); var element = $( "#autocomplete" ).autocomplete( { source: [ "java", "javascript", "haskell" ] } ), menu = element.autocomplete( "widget" ); element.val( "ja" ).autocomplete( "search" ); - equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" ); + assert.equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" ); element.autocomplete( "option", "source", [ "php", "asp" ] ); element.val( "ph" ).autocomplete( "search" ); - equal( menu.find( ".ui-menu-item" ).text(), "php" ); + assert.equal( menu.find( ".ui-menu-item" ).text(), "php" ); } ); } ); |