Skip to content

Commit f365683

Browse files
mikesherovdmethvin
authored andcommitted
fixes #10693, generalizes the "test something in an iframe" code when a callback isn't needed
1 parent a619cb3 commit f365683

File tree

3 files changed

+44
-84
lines changed

3 files changed

+44
-84
lines changed

test/data/testinit.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,36 @@ function url(value) {
119119
oldActive = jQuery.active;
120120
}
121121
};
122+
123+
this.testIframe = function( fileName, name, fn ) {
124+
125+
test(name, function() {
126+
// pause execution for now
127+
stop();
128+
129+
// load fixture in iframe
130+
var iframe = loadFixture(),
131+
win = iframe.contentWindow,
132+
interval = setInterval( function() {
133+
if ( win && win.jQuery && win.jQuery.isReady ) {
134+
clearInterval( interval );
135+
// continue
136+
start();
137+
// call actual tests passing the correct jQuery instance to use
138+
fn.call( this, win.jQuery, win, win.document );
139+
document.body.removeChild( iframe );
140+
iframe = null;
141+
}
142+
}, 15 );
143+
});
144+
145+
function loadFixture() {
146+
var src = "./data/" + fileName + ".html?" + parseInt( Math.random()*1000, 10 ),
147+
iframe = jQuery("<iframe />").css({
148+
width: 500, height: 500, position: "absolute", top: -600, left: -600, visibility: "hidden"
149+
}).appendTo("body")[0];
150+
iframe.contentWindow.location = src;
151+
return iframe;
152+
}
153+
};
122154
}());

test/unit/offset.js

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test("disconnected node", function() {
1111

1212
var supportsScroll = false;
1313

14-
testoffset("absolute", function($, iframe) {
14+
testIframe("offset/absolute", "absolute", function($, iframe) {
1515
expect(4);
1616

1717
var doc = iframe.document, tests;
@@ -51,7 +51,7 @@ testoffset("absolute", function($, iframe) {
5151
forceScroll.remove();
5252
});
5353

54-
testoffset("absolute", function( jQuery ) {
54+
testIframe("offset/absolute", "absolute", function( jQuery ) {
5555
expect(178);
5656

5757
// get offset tests
@@ -136,7 +136,7 @@ testoffset("absolute", function( jQuery ) {
136136
});
137137
});
138138

139-
testoffset("relative", function( jQuery ) {
139+
testIframe("offset/relative", "relative", function( jQuery ) {
140140
expect(60);
141141

142142
// IE is collapsing the top margin of 1px
@@ -197,7 +197,7 @@ testoffset("relative", function( jQuery ) {
197197
});
198198
});
199199

200-
testoffset("static", function( jQuery ) {
200+
testIframe("offset/static", "static", function( jQuery ) {
201201
expect(80);
202202

203203
// IE is collapsing the top margin of 1px
@@ -264,7 +264,7 @@ testoffset("static", function( jQuery ) {
264264
});
265265
});
266266

267-
testoffset("fixed", function( jQuery ) {
267+
testIframe("offset/fixed", "fixed", function( jQuery ) {
268268
expect(30);
269269

270270
var tests = [
@@ -331,7 +331,7 @@ testoffset("fixed", function( jQuery ) {
331331
}
332332
});
333333

334-
testoffset("table", function( jQuery ) {
334+
testIframe("offset/table", "table", function( jQuery ) {
335335
expect(4);
336336

337337
equal( jQuery("#table-1").offset().top, 6, "jQuery('#table-1').offset().top" );
@@ -341,7 +341,7 @@ testoffset("table", function( jQuery ) {
341341
equal( jQuery("#th-1").offset().left, 10, "jQuery('#th-1').offset().left" );
342342
});
343343

344-
testoffset("scroll", function( jQuery, win ) {
344+
testIframe("offset/scroll", "scroll", function( jQuery, win ) {
345345
expect(24);
346346

347347
var ie = jQuery.browser.msie && parseInt( jQuery.browser.version, 10 ) < 8;
@@ -399,7 +399,7 @@ testoffset("scroll", function( jQuery, win ) {
399399
strictEqual( jQuery().scrollLeft(), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
400400
});
401401

402-
testoffset("body", function( jQuery ) {
402+
testIframe("offset/body", "body", function( jQuery ) {
403403
expect(2);
404404

405405
equal( jQuery("body").offset().top, 1, "jQuery('#body').offset().top" );
@@ -466,35 +466,3 @@ test("fractions (see #7730 and #7885)", function() {
466466

467467
div.remove();
468468
});
469-
470-
function testoffset(name, fn) {
471-
472-
test(name, function() {
473-
// pause execution for now
474-
stop();
475-
476-
// load fixture in iframe
477-
var iframe = loadFixture(),
478-
win = iframe.contentWindow,
479-
interval = setInterval( function() {
480-
if ( win && win.jQuery && win.jQuery.isReady ) {
481-
clearInterval( interval );
482-
// continue
483-
start();
484-
// call actual tests passing the correct jQuery isntance to use
485-
fn.call( this, win.jQuery, win );
486-
document.body.removeChild( iframe );
487-
iframe = null;
488-
}
489-
}, 15 );
490-
});
491-
492-
function loadFixture() {
493-
var src = "./data/offset/" + name + ".html?" + parseInt( Math.random()*1000, 10 ),
494-
iframe = jQuery("<iframe />").css({
495-
width: 500, height: 500, position: "absolute", top: -600, left: -600, visibility: "hidden"
496-
}).appendTo("body")[0];
497-
iframe.contentWindow.location = src;
498-
return iframe;
499-
}
500-
}

test/unit/selector.js

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,7 @@
66

77
module("selector - jQuery only", { teardown: moduleTeardown });
88

9-
/**
10-
* Loads an iframe for the selector context
11-
* @param {String} fileName - Name of the html file to load
12-
* @param {String} name - Test name
13-
* @param {Function} fn - Test callback containing the tests to run
14-
*/
15-
var testIframe = function( fileName, name, fn ) {
16-
17-
var loadFixture = function() {
18-
19-
// Creates iframe with cache disabled
20-
var src = "./data/selector/" + fileName + ".html?" + parseInt( Math.random()*1000, 10 ),
21-
iframe = jQuery("<iframe />").css({
22-
width: 500, height: 500, position: "absolute", top: -600, left: -600, visibility: "hidden"
23-
}).appendTo("body")[0];
24-
iframe.contentWindow.location = src;
25-
return iframe;
26-
};
27-
28-
test(name, function() {
29-
// pause execution for now
30-
stop();
31-
32-
// load fixture in iframe
33-
var iframe = loadFixture(),
34-
win = iframe.contentWindow,
35-
interval = setInterval( function() {
36-
if ( win && win.jQuery && win.jQuery.isReady ) {
37-
clearInterval( interval );
38-
// continue
39-
start();
40-
// call actual tests passing the correct jQuery instance to use
41-
fn.call( this, win.jQuery, win, win.document );
42-
document.body.removeChild( iframe );
43-
iframe = null;
44-
}
45-
}, 15 );
46-
});
47-
};
48-
49-
testIframe("html5_selector", "attributes - jQuery.attr", function( jQuery, window, document ) {
9+
testIframe("selector/html5_selector", "attributes - jQuery.attr", function( jQuery, window, document ) {
5010
expect(34);
5111

5212
/**
@@ -61,7 +21,7 @@ testIframe("html5_selector", "attributes - jQuery.attr", function( jQuery, windo
6121
}
6222
return r;
6323
}
64-
24+
6525
/**
6626
* Asserts that a select matches the given IDs * @example t("Check for something", "//[a]", ["foo", "baar"]);
6727
* @param {String} a - Assertion name
@@ -72,7 +32,7 @@ testIframe("html5_selector", "attributes - jQuery.attr", function( jQuery, windo
7232
var f = jQuery(b).get(),
7333
s = "",
7434
i = 0;
75-
35+
7636
for ( ; i < f.length; i++ ) {
7737
s += (s && ",") + '"' + f[i].id + '"';
7838
}
@@ -132,7 +92,7 @@ testIframe("html5_selector", "attributes - jQuery.attr", function( jQuery, windo
13292
t( "Improperly named form elements do not interfere with form selections (#9570)", "form[name='formName']", ["form1"]);
13393
});
13494

135-
testIframe("sizzle_cache", "Sizzle cache collides with multiple Sizzles on a page", function( jQuery, window, document ) {
95+
testIframe("selector/sizzle_cache", "Sizzle cache collides with multiple Sizzles on a page", function( jQuery, window, document ) {
13696
var $cached = window.$cached;
13797

13898
expect(3);

0 commit comments

Comments
 (0)