Debian Codegraph @

tests/lib/helper.js

raw symbols SCIP
1 define( [
2 "jquery"
3 ], function( $ ) {
4
5 var exports = {},
6
7 // Store the old count so that we only assert on tests that have actually leaked,
8 // instead of asserting every time a test has leaked sometime in the past
9 oldActive = 0,
10 splice = [].splice;
11
12 exports.forceScrollableWindow = function( appendTo ) {
13
14 // The main testable area is 10000x10000 so to enforce scrolling,
15 // this DIV must be greater than 10000 to work
16 return $( "<div>" )
17 .css( {
18 height: "11000px",
19 width: "11000px"
20 } )
21 .appendTo( appendTo || "#qunit-fixture" );
22 };
23
24 exports.onFocus = function( element, onFocus ) {
25 var fn = function( event ) {
26 if ( !event.originalEvent ) {
27 return;
28 }
29 element.off( "focus", fn );
30 onFocus();
31 };
32
33 element.on( "focus", fn )[ 0 ].focus();
34 };
35
36 /**
37 * Ensures that tests have cleaned up properly after themselves. Should be passed as the
38 * afterEach function on all modules' lifecycle object.
39 */
40 exports.moduleAfterEach = function( assert ) {
41
42 // Check for (and clean up, if possible) incomplete animations/requests/etc.
43 if ( jQuery.timers && jQuery.timers.length !== 0 ) {
44 assert.equal( jQuery.timers.length, 0, "No timers are still running" );
45 splice.call( jQuery.timers, 0, jQuery.timers.length );
46 jQuery.fx.stop();
47 }
48 if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
49 assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
50 oldActive = jQuery.active;
51 }
52 };
53
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
113 return exports;
114
115 } );