-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
105 lines (82 loc) · 1.78 KB
/
index.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
/**
* Expose `Matrix`.
*/
module.exports = Matrix;
/**
* Initialize a new `Matrix` reporter.
*
* @param {Runner} runner
* @api public
*/
function Matrix(runner) {
var root = document.getElementById('mocha');
var errors = document.createElement('ul');
errors.className = 'errors';
var n = 0;
var els = boxes(runner.total);
for (var i = 0; i < els.length; i++) {
root.appendChild(els[i]);
}
var i = 0;
runner.on('pass', function(test){
show(els[i++], 'test pass');
});
runner.on('fail', function(test, err){
var id = 'error-' + ++n;
var el = els[i++];
el.href = '#' + id;
show(el, 'test fail');
var msg = err.stack || err.message;
var pre = document.createElement('pre');
text(pre, msg);
var title = document.createElement('h3');
title.setAttribute('id', id);
text(title, test.fullTitle());
var li = document.createElement('li');
li.appendChild(title);
li.appendChild(pre);
errors.appendChild(li);
});
runner.on('end', function(){
root.appendChild(errors);
show(errors, 'errors');
});
function show(el, classname) {
el.className = classname;
setTimeout(function(){
el.className = classname + ' show';
}, 0);
}
}
/**
* Create `n` boxes.
*/
function boxes(n) {
var els = [];
for (var i = 0; i < n; i++) {
var el = document.createElement('a');
el.className = 'test';
els.push(el);
}
return els;
}
/**
* Set `el` text to `str`.
*/
function text(el, str) {
if (el.textContent) {
el.textContent = str;
} else {
el.innerText = str;
}
}
/**
* Listen on `event` with callback `fn`.
*/
function on(el, event, fn) {
if (el.addEventListener) {
el.addEventListener(event, fn, false);
} else {
el.attachEvent('on' + event, fn);
}
}