jquery/test/data/testinit.js

317 lines
8.0 KiB
JavaScript
Raw Normal View History

/*jshint multistr:true, quotmark:false */
var fireNative, originaljQuery, original$,
2013-10-11 19:08:07 +00:00
baseURL = "",
supportjQuery = this.jQuery,
// see RFC 2606
externalHost = "example.com";
this.hasPHP = true;
this.isLocal = window.location.protocol === "file:";
// Setup global variables before loading jQuery for testing .noConflict()
supportjQuery.noConflict( true );
originaljQuery = this.jQuery = undefined;
original$ = this.$ = "replaced";
2012-11-01 04:40:27 +00:00
/**
* Returns an array of elements with the given IDs
* @example q( "main", "foo", "bar" )
* @result [<div id="main">, <span id="foo">, <input id="bar">]
*/
this.q = function() {
2012-05-28 16:43:41 +00:00
var r = [],
i = 0;
2012-05-28 16:43:41 +00:00
for ( ; i < arguments.length; i++ ) {
r.push( document.getElementById( arguments[i] ) );
}
return r;
};
/**
* Asserts that a select matches the given IDs
2012-05-28 16:43:41 +00:00
* @param {String} a - Assertion name
* @param {String} b - Sizzle selector
* @param {String} c - Array of ids to construct what is expected
* @example t("Check for something", "//[a]", ["foo", "baar"]);
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
*/
this.t = function( a, b, c ) {
var f = jQuery( b ).get(),
2012-05-28 16:43:41 +00:00
s = "",
i = 0;
2012-05-28 16:43:41 +00:00
for ( ; i < f.length; i++ ) {
s += ( s && "," ) + '"' + f[ i ].id + '"';
}
deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
};
this.createDashboardXML = function() {
var string = '<?xml version="1.0" encoding="UTF-8"?> \
<dashboard> \
<locations class="foo"> \
<location for="bar" checked="different"> \
<infowindowtab normal="ab" mixedCase="yes"> \
<tab title="Location"><![CDATA[blabla]]></tab> \
<tab title="Users"><![CDATA[blublu]]></tab> \
</infowindowtab> \
</location> \
</locations> \
</dashboard>';
return jQuery.parseXML(string);
};
this.createWithFriesXML = function() {
var string = '<?xml version="1.0" encoding="UTF-8"?> \
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" \
xmlns:xsd="http://www.w3.org/2001/XMLSchema" \
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \
<soap:Body> \
<jsconf xmlns="http://{{ externalHost }}/ns1"> \
<response xmlns:ab="http://{{ externalHost }}/ns2"> \
<meta> \
<component id="seite1" class="component"> \
<properties xmlns:cd="http://{{ externalHost }}/ns3"> \
<property name="prop1"> \
<thing /> \
<value>1</value> \
</property> \
<property name="prop2"> \
<thing att="something" /> \
</property> \
<foo_bar>foo</foo_bar> \
</properties> \
</component> \
</meta> \
</response> \
</jsconf> \
</soap:Body> \
</soap:Envelope>';
return jQuery.parseXML( string.replace( /\{\{\s*externalHost\s*\}\}/g, externalHost ) );
};
this.createXMLFragment = function() {
var xml, frag;
if ( window.ActiveXObject ) {
xml = new ActiveXObject( "msxml2.domdocument" );
} else {
xml = document.implementation.createDocument( "", "", null );
}
if ( xml ) {
frag = xml.createElement( "data" );
}
return frag;
};
fireNative = document.createEvent ?
function( node, type ) {
var event = document.createEvent( "HTMLEvents" );
event.initEvent( type, true, true );
node.dispatchEvent( event );
} :
function( node, type ) {
node.fireEvent( "on" + type, document.createEventObject() );
};
/**
* Add random number to url to stop caching
*
* @example url("data/test.html")
* @result "data/test.html?10538358428943"
*
* @example url("data/test.php?foo=bar")
* @result "data/test.php?foo=bar&10538358345554"
*/
function url( value ) {
return baseURL + value + (/\?/.test( value ) ? "&" : "?") +
new Date().getTime() + "" + parseInt( Math.random() * 100000, 10 );
}
2012-11-26 02:31:19 +00:00
// Ajax testing helper
this.ajaxTest = function( title, expect, options ) {
2012-11-26 10:49:20 +00:00
var requestOptions;
if ( jQuery.isFunction( options ) ) {
options = options();
}
options = options || [];
requestOptions = options.requests || options.request || options;
if ( !jQuery.isArray( requestOptions ) ) {
requestOptions = [ requestOptions ];
2012-11-26 02:31:19 +00:00
}
2012-11-26 10:49:20 +00:00
asyncTest( title, expect, function() {
if ( options.setup ) {
options.setup();
2012-11-26 02:31:19 +00:00
}
var completed = false,
2012-12-03 16:32:19 +00:00
remaining = requestOptions.length,
complete = function() {
2012-12-03 16:32:19 +00:00
if ( !completed && --remaining === 0 ) {
completed = true;
delete ajaxTest.abort;
if ( options.teardown ) {
options.teardown();
}
start();
2012-11-26 10:49:20 +00:00
}
},
2012-12-03 16:32:19 +00:00
requests = jQuery.map( requestOptions, function( options ) {
var request = ( options.create || jQuery.ajax )( options ),
callIfDefined = function( deferType, optionType ) {
var handler = options[ deferType ] || !!options[ optionType ];
2012-12-03 16:32:19 +00:00
return function( _, status ) {
if ( !completed ) {
if ( !handler ) {
ok( false, "unexpected " + status );
} else if ( jQuery.isFunction( handler ) ) {
handler.apply( this, arguments );
}
2012-12-03 16:32:19 +00:00
}
};
};
2012-11-26 10:49:20 +00:00
if ( options.afterSend ) {
options.afterSend( request );
}
2012-12-03 05:49:40 +00:00
return request
.done( callIfDefined( "done", "success" ) )
2012-12-03 16:32:19 +00:00
.fail( callIfDefined( "fail", "error" ) )
.always( complete );
2012-11-26 10:49:20 +00:00
});
2012-12-03 16:32:19 +00:00
ajaxTest.abort = function( reason ) {
if ( !completed ) {
completed = true;
delete ajaxTest.abort;
ok( false, "aborted " + reason );
jQuery.each( requests, function( i, request ) {
request.abort();
});
}
};
2012-11-26 10:49:20 +00:00
});
};
2012-11-26 02:31:19 +00:00
this.testIframe = function( fileName, name, fn ) {
asyncTest(name, function() {
// load fixture in iframe
var iframe = loadFixture(),
win = iframe.contentWindow,
interval = setInterval(function() {
if ( win && win.jQuery && win.jQuery.isReady ) {
clearInterval( interval );
start();
// call actual tests passing the correct jQuery instance to use
fn.call( this, win.jQuery, win, win.document );
document.body.removeChild( iframe );
iframe = null;
}
}, 15 );
});
function loadFixture() {
var src = url( "./data/" + fileName + ".html" ),
iframe = jQuery( "<iframe />" ).appendTo( "body" )[ 0 ];
iframe.style.cssText = "width: 500px; height: 500px; position: absolute; top: -600px; left: -600px; visibility: hidden;";
iframe.contentWindow.location = src;
return iframe;
}
};
this.testIframeWithCallback = function( title, fileName, func ) {
asyncTest( title, 1, function() {
var iframe;
window.iframeCallback = function() {
var args = arguments;
setTimeout(function() {
this.iframeCallback = undefined;
iframe.remove();
func.apply( this, args );
func = function() {};
start();
});
};
2013-10-11 19:08:07 +00:00
iframe = jQuery( "<div/>" ).css({ position: "absolute", width: "500px", left: "-600px" })
.append( jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) ) )
.appendTo( "#qunit-fixture" );
});
};
this.iframeCallback = undefined;
// Tests are always loaded async
QUnit.config.autostart = false;
this.loadTests = function() {
var loadSwarm,
url = window.location.search;
url = decodeURIComponent( url.slice( url.indexOf( "swarmURL=" ) + "swarmURL=".length ) );
loadSwarm = url && url.indexOf( "http" ) === 0;
// Get testSubproject from testrunner first
require([ "data/testrunner.js" ], function() {
var tests = [
"unit/core.js",
"unit/callbacks.js",
"unit/deferred.js",
"unit/support.js",
"unit/data.js",
"unit/queue.js",
"unit/attributes.js",
"unit/event.js",
"unit/selector.js",
"unit/traversing.js",
"unit/manipulation.js",
"unit/wrap.js",
"unit/css.js",
"unit/serialize.js",
"unit/ajax.js",
"unit/effects.js",
"unit/offset.js",
"unit/dimensions.js"
];
// Ensure load order (to preserve test numbers)
(function loadDep() {
var dep = tests.shift();
if ( dep ) {
require( [ dep ], loadDep );
} else {
QUnit.load();
/**
* Run in noConflict mode
*/
jQuery.noConflict();
// Load the TestSwarm listener if swarmURL is in the address.
if ( loadSwarm ) {
require( [ "http://swarm.jquery.org/js/inject.js?" + (new Date()).getTime() ], function() {
QUnit.start();
});
} else {
QUnit.start();
}
}
})();
});
};