| 1 |
/** |
| 2 |
* QUnit Composite |
| 3 |
* |
| 4 |
* With custom modifications - all are marked with |
| 5 |
* a "Custom modification" comment. |
| 6 |
* |
| 7 |
* https://github.com/JamesMGreene/qunit-composite |
| 8 |
* |
| 9 |
* Copyright jQuery Foundation and other contributors |
| 10 |
* Released under the MIT license. |
| 11 |
* https://jquery.org/license/ |
| 12 |
*/ |
| 13 |
(function( factory ) { |
| 14 |
if ( typeof define === "function" && define.amd ) { |
| 15 |
define( [ "qunit" ], factory ); |
| 16 |
} else { |
| 17 |
factory( QUnit ); |
| 18 |
} |
| 19 |
}(function( QUnit ) { |
| 20 |
var iframe, hasBound, resumeTests, suiteAssert, |
| 21 |
modules = 1, |
| 22 |
executingComposite = false; |
| 23 |
|
| 24 |
function hasClass( elem, name ) { |
| 25 |
return ( " " + elem.className + " " ).indexOf( " " + name + " " ) > -1; |
| 26 |
} |
| 27 |
|
| 28 |
function addClass( elem, name ) { |
| 29 |
if ( !hasClass( elem, name ) ) { |
| 30 |
elem.className += ( elem.className ? " " : "" ) + name; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
function addEvent( elem, type, fn ) { |
| 35 |
if ( elem.addEventListener ) { |
| 36 |
// Standards-based browsers |
| 37 |
elem.addEventListener( type, fn, false ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
function runSuite( suite ) { |
| 42 |
var path; |
| 43 |
|
| 44 |
if ( QUnit.is( "object", suite ) ) { |
| 45 |
path = suite.path; |
| 46 |
suite = suite.name; |
| 47 |
} else { |
| 48 |
path = suite; |
| 49 |
} |
| 50 |
|
| 51 |
QUnit.test( suite, function( assert ) { |
| 52 |
resumeTests = assert.async(); |
| 53 |
suiteAssert = assert; |
| 54 |
iframe.setAttribute( "src", path ); |
| 55 |
// QUnit.start is called from the child iframe's QUnit.done hook. |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
function initIframe() { |
| 60 |
var iframeWin, |
| 61 |
body = document.body; |
| 62 |
|
| 63 |
function onIframeLoad() { |
| 64 |
var moduleName, testName, |
| 65 |
count = 0; |
| 66 |
|
| 67 |
if ( !iframe.src ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
// Deal with QUnit being loaded asynchronously via AMD |
| 72 |
if ( !iframeWin.QUnit && iframeWin.define && iframeWin.define.amd ) { |
| 73 |
return iframeWin.require( [ "qunit" ], onIframeLoad ); |
| 74 |
} |
| 75 |
|
| 76 |
iframeWin.QUnit.moduleStart(function( data ) { |
| 77 |
// Capture module name for messages |
| 78 |
moduleName = data.name; |
| 79 |
}); |
| 80 |
|
| 81 |
iframeWin.QUnit.testStart(function( data ) { |
| 82 |
// Capture test name for messages |
| 83 |
testName = data.name; |
| 84 |
}); |
| 85 |
iframeWin.QUnit.testDone(function() { |
| 86 |
testName = undefined; |
| 87 |
}); |
| 88 |
|
| 89 |
iframeWin.QUnit.log(function( data ) { |
| 90 |
if (testName === undefined) { |
| 91 |
return; |
| 92 |
} |
| 93 |
// Pass all test details through to the main page |
| 94 |
var message = ( moduleName ? moduleName + ": " : "" ) + testName + ": " + ( data.message || ( data.result ? "okay" : "failed" ) ); |
| 95 |
suiteAssert.expect( ++count ); |
| 96 |
suiteAssert.pushResult( { |
| 97 |
result: data.result, |
| 98 |
actual: data.actual, |
| 99 |
expected: data.expected, |
| 100 |
message: message |
| 101 |
} ); |
| 102 |
}); |
| 103 |
|
| 104 |
// Continue the outer test when the iframe's test is done |
| 105 |
iframeWin.QUnit.done(function() { |
| 106 |
resumeTests(); |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
iframe = document.createElement( "iframe" ); |
| 111 |
iframe.className = "qunit-composite-suite"; |
| 112 |
body.appendChild( iframe ); |
| 113 |
|
| 114 |
addEvent( iframe, "load", onIframeLoad ); |
| 115 |
|
| 116 |
iframeWin = iframe.contentWindow; |
| 117 |
} |
| 118 |
|
| 119 |
function appendSuitesToHeader( suites ) { |
| 120 |
var i, suitesLen, suite, path, name, suitesEl, testResultEl, |
| 121 |
newSuiteListItemEl, newSuiteLinkEl; |
| 122 |
|
| 123 |
suitesEl = document.getElementById("qunit-testsuites"); |
| 124 |
|
| 125 |
if (!suitesEl) { |
| 126 |
testResultEl = document.getElementById("qunit-testresult"); |
| 127 |
|
| 128 |
if (!testResultEl) { |
| 129 |
// QUnit has not been set up yet. Defer until QUnit is ready. |
| 130 |
QUnit.begin(function () { |
| 131 |
appendSuitesToHeader(suites); |
| 132 |
}); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
suitesEl = document.createElement("ul"); |
| 137 |
suitesEl.id = "qunit-testsuites"; |
| 138 |
testResultEl.parentNode.insertBefore(suitesEl, testResultEl); |
| 139 |
} |
| 140 |
|
| 141 |
for (i = 0, suitesLen = suites.length; i < suitesLen; ++i) { |
| 142 |
suite = suites[i]; |
| 143 |
newSuiteLinkEl = document.createElement("a"); |
| 144 |
newSuiteLinkEl.innerHTML = suite.name || suite; |
| 145 |
newSuiteLinkEl.href = suite.path || suite; |
| 146 |
|
| 147 |
newSuiteListItemEl = document.createElement("li"); |
| 148 |
newSuiteListItemEl.appendChild(newSuiteLinkEl); |
| 149 |
|
| 150 |
suitesEl.appendChild(newSuiteListItemEl); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param {string} [name] Module name to group these test suites. |
| 156 |
* @param {Array} suites List of suites where each suite |
| 157 |
* may either be a string (path to the html test page), |
| 158 |
* or an object with a path and name property. |
| 159 |
*/ |
| 160 |
QUnit.testSuites = function( name, suites ) { |
| 161 |
var i, suitesLen; |
| 162 |
|
| 163 |
if ( arguments.length === 1 ) { |
| 164 |
suites = name; |
| 165 |
name = "Composition #" + modules++; |
| 166 |
} |
| 167 |
suitesLen = suites.length; |
| 168 |
|
| 169 |
appendSuitesToHeader(suites); |
| 170 |
|
| 171 |
if ( !hasBound ) { |
| 172 |
hasBound = true; |
| 173 |
QUnit.begin( initIframe ); |
| 174 |
|
| 175 |
// TODO: Would be better to use something like QUnit.once( 'moduleDone' ) |
| 176 |
// after the last test suite. |
| 177 |
QUnit.moduleDone( function () { |
| 178 |
executingComposite = false; |
| 179 |
} ); |
| 180 |
|
| 181 |
QUnit.done(function() { |
| 182 |
iframe.style.display = "none"; |
| 183 |
}); |
| 184 |
} |
| 185 |
|
| 186 |
QUnit.module( name, { |
| 187 |
beforeEach: function () { |
| 188 |
executingComposite = true; |
| 189 |
} |
| 190 |
}); |
| 191 |
|
| 192 |
for ( i = 0; i < suitesLen; i++ ) { |
| 193 |
runSuite( suites[ i ] ); |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
QUnit.testDone(function( data ) { |
| 198 |
if ( !executingComposite ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
var i, len, |
| 203 |
testId = data.testId, |
| 204 |
current = document.getElementById( "qunit-test-output-" + testId ), |
| 205 |
children = current && current.children, |
| 206 |
src = iframe.src; |
| 207 |
|
| 208 |
if (!(current && children)) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
addEvent( current, "dblclick", function( e ) { |
| 213 |
var target = e && e.target ? e.target : window.event.srcElement; |
| 214 |
if ( target.nodeName.toLowerCase() === "span" || target.nodeName.toLowerCase() === "b" ) { |
| 215 |
target = target.parentNode; |
| 216 |
} |
| 217 |
if ( window.location && target.nodeName.toLowerCase() === "strong" ) { |
| 218 |
window.location = src; |
| 219 |
} |
| 220 |
}); |
| 221 |
|
| 222 |
// Undo QUnit's auto-expansion for bad tests |
| 223 |
for ( i = 0, len = children.length; i < len; i++ ) { |
| 224 |
if ( children[ i ].nodeName.toLowerCase() === "ol" ) { |
| 225 |
addClass( children[ i ], "qunit-collapsed" ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// Update Rerun link to point to the standalone test suite page |
| 230 |
current.getElementsByTagName( "a" )[ 0 ].href = src; |
| 231 |
}); |
| 232 |
|
| 233 |
})); |