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