diff options
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/helper.js | 59 | ||||
-rw-r--r-- | tests/lib/qunit.js | 23 | ||||
-rw-r--r-- | tests/lib/testIframe.js | 7 |
3 files changed, 81 insertions, 8 deletions
diff --git a/tests/lib/helper.js b/tests/lib/helper.js index 2315c5e19..2be4c48de 100644 --- a/tests/lib/helper.js +++ b/tests/lib/helper.js @@ -51,6 +51,65 @@ exports.moduleAfterEach = function( assert ) { } }; +exports.testIframe = function( title, fileName, func, wrapper, iframeStyles ) { + if ( !wrapper ) { + wrapper = QUnit.test; + } + wrapper.call( QUnit, title, function( assert ) { + var done = assert.async(), + $iframe = jQuery( "<iframe></iframe>" ) + .css( { + position: "absolute", + top: "0", + left: "-600px", + width: "500px", + zIndex: 1, + background: "white" + } ) + .attr( { id: "qunit-fixture-iframe", src: fileName } ); + + // Add other iframe styles + if ( iframeStyles ) { + $iframe.css( iframeStyles ); + } + + // Test iframes are expected to invoke this via startIframeTest + // (cf. iframeTest.js) + window.iframeCallback = function() { + var args = Array.prototype.slice.call( arguments ); + + args.unshift( assert ); + + setTimeout( function() { + var result; + + this.iframeCallback = undefined; + + result = func.apply( this, args ); + + function finish() { + func = function() {}; + $iframe.remove(); + done(); + } + + // Wait for promises returned by `func`. + if ( result && result.then ) { + result.then( finish ); + } else { + finish(); + } + } ); + }; + + // Attach iframe to the body for visibility-dependent code. + // It will be removed by either the above code, or the testDone + // callback in qunit.js. + $iframe.prependTo( document.body ); + } ); +}; +window.iframeCallback = undefined; + return exports; } ); diff --git a/tests/lib/qunit.js b/tests/lib/qunit.js index 6441019bd..c4c96ef58 100644 --- a/tests/lib/qunit.js +++ b/tests/lib/qunit.js @@ -7,6 +7,8 @@ define( [ ], function( QUnit, $ ) { "use strict"; +var ajaxSettings = $.ajaxSettings; + QUnit.config.autostart = false; QUnit.config.requireExpects = true; @@ -34,16 +36,21 @@ QUnit.config.urlConfig.push( { label: "Enable jquery-migrate" } ); -QUnit.reset = ( function( reset ) { - return function() { +QUnit.testDone( function() { + + // Ensure jQuery events and data on the fixture are properly removed + $( "#qunit-fixture" ).empty(); - // Ensure jQuery events and data on the fixture are properly removed - $( "#qunit-fixture" ).empty(); + // Remove the iframe fixture + $( "#qunit-fixture-iframe" ).remove(); - // Let QUnit reset the fixture - reset.apply( this, arguments ); - }; -} )( QUnit.reset ); + // Reset internal $ state + if ( ajaxSettings ) { + $.ajaxSettings = $.extend( true, {}, ajaxSettings ); + } else { + delete $.ajaxSettings; + } +} ); return QUnit; diff --git a/tests/lib/testIframe.js b/tests/lib/testIframe.js new file mode 100644 index 000000000..4db56833c --- /dev/null +++ b/tests/lib/testIframe.js @@ -0,0 +1,7 @@ +window.startIframeTest = function() { + var args = Array.prototype.slice.call( arguments ); + + // Note: jQuery may be undefined if page did not load it + args.unshift( window.jQuery, window, document ); + window.parent.iframeCallback.apply( null, args ); +}; |