Skip to content

Commit

Permalink
Event: Add triggering for "ready" event
Browse files Browse the repository at this point in the history
Fixes #136
Closes #166
  • Loading branch information
dmethvin committed Apr 8, 2016
1 parent 3775183 commit e318f07
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -63,7 +63,7 @@ module.exports = function( grunt ) {
linesThresholdPct: 85
}
},
files: [ "test/**/*.html" ]
files: [ "test/**/index.html" ]
},
coveralls: {
src: "coverage/lcov/lcov.info",
Expand Down
5 changes: 5 additions & 0 deletions src/event.js
Expand Up @@ -30,6 +30,11 @@ jQuery.each( [ "load", "unload", "error" ], function( _, name ) {

} );

// Trigger "ready" event only once, on document ready
jQuery( function() {
jQuery( document ).triggerHandler( "ready" );
} );

jQuery.event.special.ready = {
setup: function() {
if ( this === document ) {
Expand Down
20 changes: 10 additions & 10 deletions test/event.js
Expand Up @@ -52,16 +52,8 @@ test( "load() and unload() event methods", function() {
} );
} );

test( "ready event", function() {
expect( 4 );

expectWarning( "Setting a ready event", 1, function() {
jQuery( document ).on( "ready", function() {
ok( true, "ready event was triggered" );
} )
.trigger( "ready" )
.off( "ready" );
} );
test( "custom ready", function() {
expect( 2 );

expectNoWarning( "Custom ready event not on document", 1, function() {
jQuery( "#foo" ).on( "ready", function( e ) {
Expand All @@ -71,3 +63,11 @@ test( "ready event", function() {
.off( "ready" );
} );
} );

TestManager.runIframeTest( "document ready", "ready-event.html",
function( fired, warnings, assert ) {
assert.expect( 2 );

assert.ok( fired, "ready event fired" );
assert.equal( warnings.length, 1, "warnings: " + JSON.stringify( warnings ) );
} );
10 changes: 0 additions & 10 deletions test/index.html
Expand Up @@ -15,16 +15,6 @@
<!-- Load a jQuery and jquery-migrate plugin file based on URL -->
<script src="testinit.js"></script>
<script>
TestManager.init({
"jquery": {
urlTag: "jquery",
choices: "dev,min,git,3.0.0"
},
"jquery-migrate": {
urlTag: "plugin",
choices: "dev,min,git,3.0.0"
}
});
TestManager.loadProject( "jquery", "git" );
// Close this script tag so file will load
</script>
Expand Down
32 changes: 32 additions & 0 deletions test/ready-event.html
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Migrate `ready` event iframe test</title>
<!-- // Support: IE < 11 -->
<!-- Ensure IE doesn't fall back into oldIE modes -->
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

<!-- Load a jQuery and jquery-migrate plugin file based on URL -->
<script src="testinit.js"></script>
<script>
TestManager.loadProject( "jquery", "git" );
// Close this script tag so file will load
</script>
<script>
jQuery.noConflict();
TestManager.loadProject( "jquery-migrate", "dev", true );
// Close this script tag so file will load
</script>
<script>
jQuery.migrateMute = true;
jQuery.migrateReset();
jQuery( document ).on( "ready", function() {
parent.TestManager.iframeCallback( true, jQuery.migrateWarnings );
} );
</script>
</head>
<body>
<p>jQuery Migrate `ready` event iframe test</p>
</body>
</html>
70 changes: 59 additions & 11 deletions test/testinit.js
Expand Up @@ -57,12 +57,61 @@ TestManager = {
document.write( lines );
}
},
/**
* Iframe tests that require setup not covered in the standard unit test
*
* Note that options passed into the standard unit tests will also be passed to
* the iframe, but the iframe html page is responsible for processing them
* as appropriate (for example by calling TestManager.loadProject)
*/
runIframeTest: function( title, url, func ) {
var self = this;
QUnit.test( title, function( assert ) {
var iframe,
query = window.location.search.slice( 1 ),
done = assert.async();

self.iframeCallback = function() {
var args = Array.prototype.slice.call( arguments );

args.push( assert );

setTimeout( function() {
self.iframeCallback = undefined;

func.apply( this, args );
func = function() {};
iframe.remove();

done();
} );
};
iframe = jQuery( "<div/>" )
.css( { position: "absolute", width: "500px", left: "-600px" } )
.append( jQuery( "<iframe/>" ).attr( "src", url +
( query && ( /\?/.test( url ) ? "&" : "?" ) ) + query ) )
.appendTo( "#qunit-fixture" );
} );
},
iframeCallback: undefined,
init: function( projects ) {
var p, project;

this.projects = projects;
this.loaded = [];

// Do QUnit setup if QUnit is loaded (could be an iframe page)
if ( !window.QUnit ) {
return;
}

// Max time for async tests until it aborts test
// and start()'s the next test.
QUnit.config.testTimeout = 20 * 1000; // 20 seconds

// Enforce an "expect" argument or expect() call in all test bodies.
QUnit.config.requireExpects = true;

// Set the list of projects, including the project version choices.
for ( p in projects ) {
project = projects[ p ];
Expand All @@ -74,17 +123,16 @@ TestManager = {
}
}
};

/**
* QUnit configuration
*/

// Max time for async tests until it aborts test
// and start()'s the next test.
QUnit.config.testTimeout = 20 * 1000; // 20 seconds

// Enforce an "expect" argument or expect() call in all test bodies.
QUnit.config.requireExpects = true;
TestManager.init( {
"jquery": {
urlTag: "jquery",
choices: "dev,min,git,3.0.0"
},
"jquery-migrate": {
urlTag: "plugin",
choices: "dev,min,git,3.0.0"
}
} );

/**
* Load the TestSwarm listener if swarmURL is in the address.
Expand Down

0 comments on commit e318f07

Please sign in to comment.