3 var reset, jshintLoaded;
5 window.TestHelpers = {};
7 function includeStyle( url ) {
8 document.write( "<link rel='stylesheet' href='../../../" + url + "'>" );
11 function includeScript( url ) {
12 document.write( "<script src='../../../" + url + "'></script>" );
15 function url( value ) {
16 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random() * 100000, 10);
20 QUnit.reset = function() {
21 // Ensure jQuery events and data on the fixture are properly removed
22 jQuery("#qunit-fixture").empty();
23 // Let QUnit reset the fixture
24 reset.apply( this, arguments );
28 QUnit.config.requireExpects = true;
30 QUnit.config.urlConfig.push({
32 label: "Minified source",
33 tooltip: "Load minified source files instead of the regular unminified ones."
36 TestHelpers.loadResources = QUnit.urlParams.min ?
38 includeStyle( "dist/jquery-ui.min.css" );
39 includeScript( "dist/jquery-ui.min.js" );
41 function( resources ) {
42 $.each( resources.css || [], function( i, resource ) {
43 includeStyle( "themes/base/jquery." + resource + ".css" );
45 $.each( resources.js || [], function( i, resource ) {
46 includeScript( resource );
50 QUnit.config.urlConfig.push({
53 tooltip: "Skip running JSHint, e.g. within TestSwarm, where Jenkins runs it already"
57 TestHelpers.testJshint = function( module ) {
58 if ( QUnit.urlParams.nojshint ) {
62 if ( !jshintLoaded ) {
63 includeScript( "external/jshint.js" );
67 asyncTest( "JSHint", function() {
72 url: url("../../../ui/.jshintrc"),
76 url: url("../../../ui/jquery.ui." + module + ".js"),
79 ).done(function( hintArgs, srcArgs ) {
80 var passed = JSHINT( srcArgs[ 0 ], hintArgs[ 0 ] ),
81 errors = $.map( JSHINT.errors, function( error ) {
82 // JSHINT may report null if there are too many errors
87 return "[L" + error.line + ":C" + error.character + "] " +
88 error.reason + "\n" + error.evidence + "\n";
94 ok( false, "error loading source" );
100 function testWidgetDefaults( widget, defaults ) {
101 var pluginDefaults = $.ui[ widget ].prototype.options;
103 // ensure that all defaults have the correct value
104 test( "defined defaults", function() {
106 $.each( defaults, function( key, val ) {
108 if ( $.isFunction( val ) ) {
109 ok( $.isFunction( pluginDefaults[ key ] ), key );
112 deepEqual( pluginDefaults[ key ], val, key );
116 // ensure that all defaults were tested
117 test( "tested defaults", function() {
119 $.each( pluginDefaults, function( key ) {
121 ok( key in defaults, key );
126 function testWidgetOverrides( widget ) {
127 if ( $.uiBackCompat === false ) {
128 test( "$.widget overrides", function() {
135 ], function( i, method ) {
136 strictEqual( $.ui[ widget ].prototype[ method ],
137 $.Widget.prototype[ method ], "should not override " + method );
143 function testBasicUsage( widget ) {
144 test( "basic usage", function() {
147 var defaultElement = $.ui[ widget ].prototype.defaultElement;
148 $( defaultElement ).appendTo( "body" )[ widget ]().remove();
149 ok( true, "initialized on element" );
151 $( defaultElement )[ widget ]().remove();
152 ok( true, "initialized on disconnected DOMElement - never connected" );
154 $( defaultElement ).appendTo( "body" ).remove()[ widget ]().remove();
155 ok( true, "initialized on disconnected DOMElement - removed" );
159 TestHelpers.commonWidgetTests = function( widget, settings ) {
160 module( widget + ": common widget" );
162 TestHelpers.testJshint( widget );
163 testWidgetDefaults( widget, settings.defaults );
164 testWidgetOverrides( widget );
165 testBasicUsage( widget );
166 test( "version", function() {
168 ok( "version" in $.ui[ widget ].prototype, "version property exists" );
173 * Taken from https://github.com/jquery/qunit/tree/master/addons/close-enough
175 window.closeEnough = function( actual, expected, maxDifference, message ) {
176 var passes = (actual === expected) || Math.abs(actual - expected) <= maxDifference;
177 QUnit.push(passes, actual, expected, message);
181 * Experimental assertion for comparing DOM objects.
183 * Serializes an element and some properties and attributes and it's children if any, otherwise the text.
184 * Then compares the result using deepEqual.
186 window.domEqual = function( selector, modifier, message ) {
187 var expected, actual,
194 "aria-activedescendant",
216 function getElementStyles( elem ) {
218 style = elem.ownerDocument.defaultView ?
219 elem.ownerDocument.defaultView.getComputedStyle( elem, null ) :
223 if ( style && style.length && style[ 0 ] && style[ style[ 0 ] ] ) {
227 if ( typeof style[ key ] === "string" ) {
228 styles[ $.camelCase( key ) ] = style[ key ];
231 // support: Opera, IE <9
233 for ( key in style ) {
234 if ( typeof style[ key ] === "string" ) {
235 styles[ key ] = style[ key ];
243 function extract( elem ) {
244 if ( !elem || !elem.length ) {
245 QUnit.push( false, actual, expected,
246 "domEqual failed, can't extract " + selector + ", message was: " + message );
252 $.each( properties, function( index, attr ) {
253 var value = elem.prop( attr );
254 result[ attrĀ ] = value !== undefined ? value : "";
256 $.each( attributes, function( index, attr ) {
257 var value = elem.attr( attr );
258 result[ attrĀ ] = value !== undefined ? value : "";
260 result.style = getElementStyles( elem[ 0 ] );
261 result.events = $._data( elem[ 0 ], "events" );
262 result.data = $.extend( {}, elem.data() );
263 delete result.data[ $.expando ];
264 children = elem.children();
265 if ( children.length ) {
266 result.children = elem.children().map(function() {
267 return extract( $( this ) );
270 result.text = elem.text();
276 actual = extract( $( selector ) );
277 QUnit.push( QUnit.equiv(actual, expected), actual, expected, message );
280 // Get current state prior to modifier
281 expected = extract( $( selector ) );
283 // Run modifier (async or sync), then compare state via done()
284 if ( modifier.length ) {