Skip to content

Commit bedc762

Browse files
committed
Tests: Backport the testIframe infra from jQuery Core
Also: * Migrate the new wheel + mousewheel spinner test to use it * Migrate from `QUnit.reset` to `QUnit.done` (see qunitjs/qunit#354)
1 parent bddff7f commit bedc762

File tree

5 files changed

+159
-100
lines changed

5 files changed

+159
-100
lines changed

tests/lib/helper.js

+59
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,65 @@ exports.moduleAfterEach = function( assert ) {
5151
}
5252
};
5353

54+
exports.testIframe = function( title, fileName, func, wrapper, iframeStyles ) {
55+
if ( !wrapper ) {
56+
wrapper = QUnit.test;
57+
}
58+
wrapper.call( QUnit, title, function( assert ) {
59+
var done = assert.async(),
60+
$iframe = jQuery( "<iframe></iframe>" )
61+
.css( {
62+
position: "absolute",
63+
top: "0",
64+
left: "-600px",
65+
width: "500px",
66+
zIndex: 1,
67+
background: "white"
68+
} )
69+
.attr( { id: "qunit-fixture-iframe", src: fileName } );
70+
71+
// Add other iframe styles
72+
if ( iframeStyles ) {
73+
$iframe.css( iframeStyles );
74+
}
75+
76+
// Test iframes are expected to invoke this via startIframeTest
77+
// (cf. iframeTest.js)
78+
window.iframeCallback = function() {
79+
var args = Array.prototype.slice.call( arguments );
80+
81+
args.unshift( assert );
82+
83+
setTimeout( function() {
84+
var result;
85+
86+
this.iframeCallback = undefined;
87+
88+
result = func.apply( this, args );
89+
90+
function finish() {
91+
func = function() {};
92+
$iframe.remove();
93+
done();
94+
}
95+
96+
// Wait for promises returned by `func`.
97+
if ( result && result.then ) {
98+
result.then( finish );
99+
} else {
100+
finish();
101+
}
102+
} );
103+
};
104+
105+
// Attach iframe to the body for visibility-dependent code.
106+
// It will be removed by either the above code, or the testDone
107+
// callback in qunit.js.
108+
$iframe.prependTo( document.body );
109+
} );
110+
};
111+
window.iframeCallback = undefined;
112+
54113
return exports;
55114

56115
} );

tests/lib/qunit.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ define( [
77
], function( QUnit, $ ) {
88
"use strict";
99

10+
var ajaxSettings = $.ajaxSettings;
11+
1012
QUnit.config.autostart = false;
1113
QUnit.config.requireExpects = true;
1214

@@ -34,16 +36,21 @@ QUnit.config.urlConfig.push( {
3436
label: "Enable jquery-migrate"
3537
} );
3638

37-
QUnit.reset = ( function( reset ) {
38-
return function() {
39+
QUnit.testDone( function() {
40+
41+
// Ensure jQuery events and data on the fixture are properly removed
42+
$( "#qunit-fixture" ).empty();
3943

40-
// Ensure jQuery events and data on the fixture are properly removed
41-
$( "#qunit-fixture" ).empty();
44+
// Remove the iframe fixture
45+
$( "#qunit-fixture-iframe" ).remove();
4246

43-
// Let QUnit reset the fixture
44-
reset.apply( this, arguments );
45-
};
46-
} )( QUnit.reset );
47+
// Reset internal $ state
48+
if ( ajaxSettings ) {
49+
$.ajaxSettings = $.extend( true, {}, ajaxSettings );
50+
} else {
51+
delete $.ajaxSettings;
52+
}
53+
} );
4754

4855
return QUnit;
4956

tests/lib/testIframe.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
window.startIframeTest = function() {
2+
var args = Array.prototype.slice.call( arguments );
3+
4+
// Note: jQuery may be undefined if page did not load it
5+
args.unshift( window.jQuery, window, document );
6+
window.parent.iframeCallback.apply( null, args );
7+
};

tests/unit/spinner/core.js

+6-92
Original file line numberDiff line numberDiff line change
@@ -239,105 +239,19 @@ QUnit.test( "mousewheel on input (DEPRECATED)", function( assert ) {
239239
}
240240
} );
241241

242-
QUnit.test( "wheel & mousewheel conflicts", function( assert ) {
243-
var ready = assert.async();
244-
assert.expect( 5 );
245-
246-
var iframe = $( "<iframe />" ).appendTo( "#qunit-fixture" ),
247-
iframeDoc = ( iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument ).document;
248-
249-
window.iframeCallback = function( values ) {
250-
window.iframeCallback = undefined;
242+
helper.testIframe(
243+
"wheel & mousewheel conflicts",
244+
"mousewheel-wheel.html",
245+
function( assert, jQuery, window, document, values ) {
246+
assert.expect( 5 );
251247

252248
assert.equal( values[ 0 ], 0, "wheel event without delta does not change value" );
253249
assert.equal( values[ 1 ], 2, "delta -1" );
254250
assert.equal( values[ 2 ], 0, "delta 0.2" );
255251
assert.equal( values[ 3 ], -2, "delta 15" );
256252
assert.equal( values[ 4 ], -2, "wheel when not focused" );
257-
258-
ready();
259-
};
260-
261-
function runTest() {
262-
var values = [],
263-
element = $( "#spin" ).val( 0 ).spinner( {
264-
step: 2
265-
} );
266-
267-
element.focus();
268-
setTimeout( step1 );
269-
270-
function dispatchWheelEvent( elem, deltaY ) {
271-
elem[ 0 ].dispatchEvent( new WheelEvent( "wheel", {
272-
deltaY: deltaY
273-
} ) );
274-
}
275-
276-
function step1() {
277-
dispatchWheelEvent( element );
278-
values.push( element.val() );
279-
280-
dispatchWheelEvent( element, -1 );
281-
values.push( element.val() );
282-
283-
dispatchWheelEvent( element, 0.2 );
284-
values.push( element.val() );
285-
286-
dispatchWheelEvent( element, 15 );
287-
values.push( element.val() );
288-
289-
element.blur();
290-
setTimeout( step2 );
291-
}
292-
293-
function step2() {
294-
dispatchWheelEvent( element, -1 );
295-
values.push( element.val() );
296-
297-
window.parent.iframeCallback( values );
298-
}
299253
}
300-
301-
iframeDoc.write( "" +
302-
"<!doctype html>\n" +
303-
"<html lang='en'>\n" +
304-
"<head>\n" +
305-
" <meta charset='utf-8'>\n" +
306-
" <title>jQuery UI Spinner Test Suite</title>\n" +
307-
"\n" +
308-
" <script src='../../../external/requirejs/require.js'></script>\n" +
309-
" <script src='../../../external/jquery/jquery.js'></script>\n" +
310-
" <script src='../../lib/css.js' data-modules='core button spinner'></script>\n" +
311-
"</head>\n" +
312-
"<body>\n" +
313-
"\n" +
314-
"<div id='qunit'></div>\n" +
315-
"<div id='qunit-fixture'>\n" +
316-
"\n" +
317-
"<input id='spin' class='foo'>\n" +
318-
"<input id='spin2' value='2'>\n" +
319-
"\n" +
320-
"</div>\n" +
321-
"<script>\n" +
322-
" " + runTest + "\n" +
323-
" requirejs.config( {\n" +
324-
" paths: {\n" +
325-
" 'jquery-mousewheel': '../../../external/jquery-mousewheel/jquery.mousewheel',\n" +
326-
" 'ui': '../../../ui'\n" +
327-
" },\n" +
328-
"} );\n" +
329-
" require( [\n" +
330-
" 'jquery-mousewheel',\n" +
331-
" 'ui/widgets/spinner'\n" +
332-
" ], function() {\n" +
333-
" runTest();\n" +
334-
" } );\n" +
335-
"</script>\n" +
336-
"</body>\n" +
337-
"</html>\n" +
338-
"" );
339-
iframeDoc.close();
340-
} );
254+
);
341255

342256
QUnit.test( "reading HTML5 attributes", function( assert ) {
343257
assert.expect( 6 );
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>jQuery UI Spinner Test Suite</title>
6+
7+
<script src="../../../external/requirejs/require.js"></script>
8+
<script src="../../../external/jquery/jquery.js"></script>
9+
<script src="../../lib/css.js" data-modules="core button spinner theme"></script>
10+
<script src="../../lib/testIframe.js"></script>
11+
</head>
12+
<body>
13+
14+
<input id="spin" class="foo">
15+
16+
<script>
17+
function runTest() {
18+
var values = [],
19+
element = $( "#spin" ).val( 0 ).spinner( {
20+
step: 2
21+
} );
22+
23+
element.focus();
24+
setTimeout( step1 );
25+
26+
function dispatchWheelEvent( elem, deltaY ) {
27+
elem[ 0 ].dispatchEvent( new WheelEvent( "wheel", {
28+
deltaY: deltaY
29+
} ) );
30+
}
31+
32+
function step1() {
33+
dispatchWheelEvent( element );
34+
values.push( element.val() );
35+
36+
dispatchWheelEvent( element, -1 );
37+
values.push( element.val() );
38+
39+
dispatchWheelEvent( element, 0.2 );
40+
values.push( element.val() );
41+
42+
dispatchWheelEvent( element, 15 );
43+
values.push( element.val() );
44+
45+
element.blur();
46+
setTimeout( step2 );
47+
}
48+
49+
function step2() {
50+
dispatchWheelEvent( element, -1 );
51+
values.push( element.val() );
52+
53+
startIframeTest( values );
54+
}
55+
}
56+
57+
requirejs.config( {
58+
paths: {
59+
"jquery-mousewheel": "../../../external/jquery-mousewheel/jquery.mousewheel",
60+
"ui": "../../../ui"
61+
},
62+
} );
63+
64+
require( [
65+
"jquery-mousewheel",
66+
"ui/widgets/spinner"
67+
], function() {
68+
runTest();
69+
} );
70+
</script>
71+
</body>
72+
</html>

0 commit comments

Comments
 (0)