-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathmocha-htmltest.js
70 lines (60 loc) · 1.79 KB
/
mocha-htmltest.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
/*
* Copyright 2013 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
(function() {
var thisFile = 'lib/mocha-htmltest.js';
var base = '';
mocha.htmlbase = function(htmlbase) {
base = htmlbase;
};
(function() {
var s$ = document.querySelectorAll('script[src]');
Array.prototype.forEach.call(s$, function(s) {
var src = s.getAttribute('src');
var re = new RegExp(thisFile + '[^\\\\]*');
var match = src.match(re);
if (match) {
base = src.slice(0, -match[0].length);
}
});
})();
var next, iframe;
var listener = function(event) {
if (event.data === 'ok') {
next();
} else if (event.data && event.data.error) {
// errors cannot be cloned via postMessage according to spec, so we re-errorify them
throw new Error(event.data.error);
}
};
function htmlSetup() {
window.addEventListener("message", listener);
iframe = document.createElement('iframe');
iframe.style.cssText = 'position: absolute; left: -9000em; width:768px; height: 1024px';
document.body.appendChild(iframe);
}
function htmlTeardown() {
window.removeEventListener('message', listener);
document.body.removeChild(iframe);
}
function htmlTest(src) {
test(src, function(done) {
next = done;
var url = base + src;
var delimiter = url.indexOf('?') < 0 ? '?' : '&';
var docSearch = location.search.slice(1);
iframe.src = url + delimiter + Math.random() + '&' + docSearch;
});
};
function htmlSuite(inName, inFn) {
suite(inName, function() {
setup(htmlSetup);
teardown(htmlTeardown);
inFn();
});
};
window.htmlTest = htmlTest;
window.htmlSuite = htmlSuite;
})();