diff options
author | Scott González <scott.gonzalez@gmail.com> | 2015-04-03 15:21:16 -0400 |
---|---|---|
committer | Scott González <scott.gonzalez@gmail.com> | 2015-04-09 09:21:06 -0400 |
commit | 7c896ddb8563e1f4fc655904614cf72d010e2ecb (patch) | |
tree | 160c3d2c1c4aa7e89f38f8371558f3fccd8e8d8a /tests/lib/common.js | |
parent | d0ea32e3ad613eaaa523d0c88c776dab2b26b25f (diff) | |
download | jquery-ui-7c896ddb8563e1f4fc655904614cf72d010e2ecb.tar.gz jquery-ui-7c896ddb8563e1f4fc655904614cf72d010e2ecb.zip |
Tests: Change test infrastructure to use AMD and reduce boilerplate
Ref #10119
Ref gh-1528
* Adds RequireJS and relies on AMD for loading dependencies.
* Updates to grunt-contrib-qunit 0.6.0.
* Convert `domEqual()` to a proper QUnit assertion.
* Introduces two bootstrap files (JS and CSS) which use `data-` attributes to
reduce the amount of boilerplate needed in each test
Diffstat (limited to 'tests/lib/common.js')
-rw-r--r-- | tests/lib/common.js | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/tests/lib/common.js b/tests/lib/common.js new file mode 100644 index 000000000..9faa4e246 --- /dev/null +++ b/tests/lib/common.js @@ -0,0 +1,133 @@ +define([ + "jquery" +], function( $ ) { + +var exports = {}; + +function testWidgetDefaults( widget, defaults ) { + var pluginDefaults = $.ui[ widget ].prototype.options; + + // Ensure that all defaults have the correct value + test( "defined defaults", function() { + var count = 0; + $.each( defaults, function( key, val ) { + expect( ++count ); + if ( $.isFunction( val ) ) { + ok( $.isFunction( pluginDefaults[ key ] ), key ); + return; + } + deepEqual( pluginDefaults[ key ], val, key ); + }); + }); + + // Ensure that all defaults were tested + test( "tested defaults", function() { + var count = 0; + $.each( pluginDefaults, function( key ) { + expect( ++count ); + ok( key in defaults, key ); + }); + }); +} + +function testWidgetOverrides( widget ) { + if ( $.uiBackCompat === false ) { + test( "$.widget overrides", function() { + expect( 4 ); + $.each([ + "_createWidget", + "destroy", + "option", + "_trigger" + ], function( i, method ) { + strictEqual( $.ui[ widget ].prototype[ method ], + $.Widget.prototype[ method ], "should not override " + method ); + }); + }); + } +} + +function testBasicUsage( widget ) { + test( "basic usage", function() { + expect( 3 ); + + var defaultElement = $.ui[ widget ].prototype.defaultElement; + $( defaultElement ).appendTo( "body" )[ widget ]().remove(); + ok( true, "initialized on element" ); + + $( defaultElement )[ widget ]().remove(); + ok( true, "initialized on disconnected DOMElement - never connected" ); + + // Ensure manipulating removed elements works (#3664) + $( defaultElement ).appendTo( "body" ).remove()[ widget ]().remove(); + ok( true, "initialized on disconnected DOMElement - removed" ); + }); +} + +exports.testWidget = function( widget, settings ) { + module( widget + ": common widget" ); + + exports.testJshint( widget ); + testWidgetDefaults( widget, settings.defaults ); + testWidgetOverrides( widget ); + testBasicUsage( widget ); + test( "version", function() { + expect( 1 ); + ok( "version" in $.ui[ widget ].prototype, "version property exists" ); + }); +}; + +exports.testJshint = function( module ) { + + // Function.prototype.bind check is needed because JSHint doesn't work in ES3 browsers anymore + // https://github.com/jshint/jshint/issues/1384 + if ( QUnit.urlParams.nojshint || !Function.prototype.bind ) { + return; + } + + asyncTest( "JSHint", function() { + require( [ "jshint" ], function() { + expect( 1 ); + + $.when( + $.ajax( { + url: "../../../ui/.jshintrc", + dataType: "json" + } ), + $.ajax( { + url: "../../../ui/" + module + ".js", + dataType: "text" + } ) + ) + .done( function( hintArgs, srcArgs ) { + var globals, passed, errors, + jshintrc = hintArgs[ 0 ], + source = srcArgs[ 0 ]; + + globals = jshintrc.globals || {}; + delete jshintrc.globals; + passed = JSHINT( source, jshintrc, globals ); + errors = $.map( JSHINT.errors, function( error ) { + + // JSHINT may report null if there are too many errors + if ( !error ) { + return; + } + + return "[L" + error.line + ":C" + error.character + "] " + + error.reason + "\n" + error.evidence + "\n"; + } ).join( "\n" ); + ok( passed, errors ); + start(); + } ) + .fail(function( hintError, srcError ) { + ok( false, "error loading source: " + ( hintError || srcError ).statusText ); + start(); + } ); + }); + }); +}; + +return exports; + +}); |