Skip to content

Commit 08d73d7

Browse files
committed
Tests: Make iframe tests wait after checking isReady
Ref jquerygh-3040
1 parent 755e7cc commit 08d73d7

15 files changed

+48
-72
lines changed

README.md

+13-20
Original file line numberDiff line numberDiff line change
@@ -340,32 +340,25 @@ url("data/test.php?foo=bar");
340340
```
341341

342342

343-
### Load tests in an iframe ###
344-
345-
Loads a given page constructing a url with fileName: `"./data/" + fileName + ".html"`
346-
and fires the given callback on jQuery ready (using the jQuery loading from that page)
347-
and passes the iFrame's jQuery to the callback.
348-
349-
```js
350-
testIframe( fileName, testName, callback );
351-
```
352-
353-
Callback arguments:
343+
### Load tests in an iframe (window.iframeCallback) ###
354344

355345
```js
356-
callback( jQueryFromIFrame, iFrameWindow, iFrameDocument );
346+
testIframeWithCallback( testName, fileName,
347+
function callback( arg1, arg2, ... assert ) {
348+
...
349+
} );
357350
```
358351

359-
### Load tests in an iframe (window.iframeCallback) ###
360-
361352
Loads a given page constructing a url with fileName: `"./data/" + fileName + ".html"`
362-
The given callback is fired when window.iframeCallback is called by the page.
363-
The arguments passed to the callback are the same as the
364-
arguments passed to window.iframeCallback, whatever that may be.
353+
The iframe page is responsible for determining when `window.parent.iframeCallback`
354+
should be called, for example at document ready or window.onload time.
355+
Arguments passed to the callback are the same as the arguments passed
356+
to `window.parent.iframeCallback` by the iframe, plus the QUnit `assert`
357+
object from the `QUnit.test()` that this wrapper sets up for you.
358+
The iframe should send any objects needed by the unit test via arguments, for example
359+
its `jQuery`, `window`, and `document` objects from the iframe.
360+
365361

366-
```js
367-
testIframeWithCallback( testName, fileName, callback );
368-
```
369362

370363
Questions?
371364
----------

test/data/dimensions/documentLarge.html

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
<body>
1313
<div>
1414
<script src="../../jquery.js"></script>
15+
<script>
16+
jQuery( function() {
17+
window.parent.iframeCallback( jQuery, window, document );
18+
} );
19+
</script>
1520
</div>
1621
</body>
1722
</html>

test/data/offset/absolute.html

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
$(this).css({ top: pos.top, left: pos.left });
2525
return false;
2626
});
27+
window.parent.iframeCallback( jQuery, window, document );
2728
});
2829
</script>
2930
</head>

test/data/offset/body.html

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
$("marker").css( $(this).offset() );
1717
return false;
1818
});
19+
window.parent.iframeCallback( jQuery, window, document );
1920
});
2021
</script>
2122
</head>

test/data/offset/fixed.html

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$("#marker").css( $(this).offset() );
2121
return false;
2222
});
23+
window.parent.iframeCallback( jQuery, window, document );
2324
});
2425
</script>
2526
</head>

test/data/offset/relative.html

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$(this).css({ position: 'absolute', top: pos.top, left: pos.left });
2121
return false;
2222
});
23+
window.parent.iframeCallback( jQuery, window, document );
2324
});
2425
</script>
2526
</head>

test/data/offset/scroll.html

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
$("#marker").css( $(this).offset() );
2525
return false;
2626
});
27+
window.parent.iframeCallback( jQuery, window, document );
2728
});
2829
</script>
2930
</head>

test/data/offset/static.html

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
$(this).css({ position: 'absolute', top: pos.top, left: pos.left });
2020
return false;
2121
});
22+
window.parent.iframeCallback( jQuery, window, document );
2223
});
2324
</script>
2425
</head>

test/data/offset/table.html

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
$("#marker").css( $(this).offset() );
1818
return false;
1919
});
20+
window.parent.iframeCallback( jQuery, window, document );
2021
});
2122
</script>
2223
</head>

test/data/selector/html5_selector.html

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
document.createElement('audio');
1616
document.createElement('article');
1717
document.createElement('details');
18+
19+
jQuery( function() {
20+
window.parent.iframeCallback( jQuery, window, document );
21+
} );
1822
</script>
1923
</head>
2024
<body>

test/data/selector/sizzle_cache.html

+5
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
<div class="test">
1818
<a href="#" id="collision">Worlds collide</a>
1919
</div>
20+
<script>
21+
jQuery( function() {
22+
window.parent.iframeCallback( jQuery, window, document );
23+
} );
24+
</script>
2025
</body>
2126
</html>

test/data/testinit.js

-31
Original file line numberDiff line numberDiff line change
@@ -232,37 +232,6 @@ this.ajaxTest = function( title, expect, options ) {
232232
} );
233233
};
234234

235-
this.testIframe = function( fileName, name, fn ) {
236-
QUnit.test( name, function( assert ) {
237-
var done = assert.async();
238-
239-
// load fixture in iframe
240-
var iframe = loadFixture(),
241-
win = iframe.contentWindow,
242-
interval = setInterval( function() {
243-
if ( win && win.jQuery && win.jQuery.isReady ) {
244-
clearInterval( interval );
245-
246-
// call actual tests passing the correct jQuery instance to use
247-
fn.call( this, win.jQuery, win, win.document, assert );
248-
done();
249-
document.body.removeChild( iframe );
250-
iframe = null;
251-
}
252-
}, 15 );
253-
} );
254-
255-
function loadFixture() {
256-
var src = url( "./data/" + fileName + ".html" ),
257-
iframe = jQuery( "<iframe />" ).appendTo( "body" )[ 0 ];
258-
iframe.style.cssText = "width: 500px; height: 500px; position: absolute; " +
259-
"top: -600px; left: -600px; visibility: hidden;";
260-
261-
iframe.contentWindow.location = src;
262-
return iframe;
263-
}
264-
};
265-
266235
this.testIframeWithCallback = function( title, fileName, func ) {
267236
QUnit.test( title, 1, function( assert ) {
268237
var iframe;

test/unit/dimensions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,9 @@ QUnit.test( "setters with and without box-sizing:border-box", function( assert )
469469
} );
470470
} );
471471

472-
testIframe(
473-
"dimensions/documentLarge",
472+
testIframeWithCallback(
474473
"window vs. large document",
474+
"dimensions/documentLarge.html",
475475
function( jQuery, window, document, assert ) {
476476
assert.expect( 2 );
477477

test/unit/offset.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ QUnit.module( "offset", { setup: function() {
3535
forceScroll.detach();
3636
}, teardown: moduleTeardown } );
3737

38-
/*
39-
Closure-compiler will roll static methods off of the jQuery object and so they will
40-
not be passed with the jQuery object across the windows. To differentiate this, the
41-
testIframe callbacks use the "$" symbol to refer to the jQuery object passed from
42-
the iframe window and the "jQuery" symbol is used to access any static methods.
43-
*/
44-
4538
QUnit.test( "empty set", function( assert ) {
4639
assert.expect( 2 );
4740
assert.strictEqual( jQuery().offset(), undefined, "offset() returns undefined for empty set (#11962)" );
@@ -75,7 +68,7 @@ QUnit.test( "hidden (display: none) element", function( assert ) {
7568
assert.equal( result.left, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
7669
} );
7770

78-
testIframe( "offset/absolute", "absolute", function( $, iframe, document, assert ) {
71+
testIframeWithCallback( "absolute", "offset/absolute.html", function( $, iframe, document, assert ) {
7972
assert.expect( 4 );
8073

8174
var doc = iframe.document,
@@ -100,7 +93,7 @@ testIframe( "offset/absolute", "absolute", function( $, iframe, document, assert
10093
} );
10194
} );
10295

103-
testIframe( "offset/absolute", "absolute", function( $, window, document, assert ) {
96+
testIframeWithCallback( "absolute", "offset/absolute.html", function( $, window, document, assert ) {
10497
assert.expect( 178 );
10598

10699
var tests, offset;
@@ -185,7 +178,7 @@ testIframe( "offset/absolute", "absolute", function( $, window, document, assert
185178
} );
186179
} );
187180

188-
testIframe( "offset/relative", "relative", function( $, window, document, assert ) {
181+
testIframeWithCallback( "relative", "offset/relative.html", function( $, window, document, assert ) {
189182
assert.expect( 64 );
190183

191184
// get offset
@@ -243,7 +236,7 @@ testIframe( "offset/relative", "relative", function( $, window, document, assert
243236
} );
244237
} );
245238

246-
testIframe( "offset/static", "static", function( $, window, document, assert ) {
239+
testIframeWithCallback( "static", "offset/static.html", function( $, window, document, assert ) {
247240
assert.expect( 80 );
248241

249242
// get offset
@@ -305,7 +298,7 @@ testIframe( "offset/static", "static", function( $, window, document, assert ) {
305298
} );
306299
} );
307300

308-
testIframe( "offset/fixed", "fixed", function( $, window, document, assert ) {
301+
testIframeWithCallback( "fixed", "offset/fixed.html", function( $, window, document, assert ) {
309302
assert.expect( 34 );
310303

311304
var tests, $noTopLeft;
@@ -395,7 +388,7 @@ testIframe( "offset/fixed", "fixed", function( $, window, document, assert ) {
395388
}
396389
} );
397390

398-
testIframe( "offset/table", "table", function( $, window, document, assert ) {
391+
testIframeWithCallback( "table", "offset/table.html", function( $, window, document, assert ) {
399392
assert.expect( 4 );
400393

401394
assert.equal( $( "#table-1" ).offset().top, 6, "jQuery('#table-1').offset().top" );
@@ -405,7 +398,7 @@ testIframe( "offset/table", "table", function( $, window, document, assert ) {
405398
assert.equal( $( "#th-1" ).offset().left, 10, "jQuery('#th-1').offset().left" );
406399
} );
407400

408-
testIframe( "offset/scroll", "scroll", function( $, win, doc, assert ) {
401+
testIframeWithCallback( "scroll", "offset/scroll.html", function( $, win, doc, assert ) {
409402
assert.expect( 26 );
410403

411404
assert.equal( $( "#scroll-1" ).offset().top, 7, "jQuery('#scroll-1').offset().top" );
@@ -464,7 +457,7 @@ testIframe( "offset/scroll", "scroll", function( $, win, doc, assert ) {
464457
assert.strictEqual( $().scrollLeft(), undefined, "jQuery().scrollLeft() testing getter on empty jquery object" );
465458
} );
466459

467-
testIframe( "offset/body", "body", function( $, window, document, assert ) {
460+
testIframeWithCallback( "body", "offset/body.html", function( $, window, document, assert ) {
468461
assert.expect( 4 );
469462

470463
assert.equal( $( "body" ).offset().top, 1, "jQuery('#body').offset().top" );

test/unit/selector.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ QUnit[ jQuery.find.compile ? "test" : "skip" ]( "disconnected nodes", function(
289289
assert.equal( $opt.is( ":selected" ), true, "selected option" );
290290
} );
291291

292-
testIframe(
293-
"selector/html5_selector",
292+
testIframeWithCallback(
294293
"attributes - jQuery.attr",
294+
"selector/html5_selector.html",
295295
function( jQuery, window, document, assert ) {
296296
assert.expect( 38 );
297297

@@ -489,9 +489,9 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
489489
assert.strictEqual( jQuery.unique, jQuery.uniqueSort, "jQuery.unique() is an alias for jQuery.uniqueSort()" );
490490
} );
491491

492-
testIframe(
493-
"selector/sizzle_cache",
492+
testIframeWithCallback(
494493
"Sizzle cache collides with multiple Sizzles on a page",
494+
"selector/sizzle_cache.html",
495495
function( jQuery, window, document, assert ) {
496496
var $cached = window[ "$cached" ];
497497

0 commit comments

Comments
 (0)