Skip to content

Commit 0ff8786

Browse files
committed
refactor: add progress bar
1 parent 7d41462 commit 0ff8786

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

lib/reporters/html.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports = module.exports = HTML;
3737

3838
var statsTemplate =
3939
'<ul id="mocha-stats">' +
40-
'<li class="progress"></li>' +
40+
'<li class="progress-contain"><em>0</em>% <progress class="progress-element" id="progress-element" max="100" value="0" /></li>' +
4141
'<li class="passes"><a href="javascript:void(0);">passes:</a> <em>0</em></li>' +
4242
'<li class="failures"><a href="javascript:void(0);">failures:</a> <em>0</em></li>' +
4343
'<li class="duration">duration: <em>0</em>s</li>' +
@@ -69,17 +69,10 @@ function HTML(runner, options) {
6969
var duration = items[3].getElementsByTagName('em')[0];
7070
var report = fragment('<ul id="mocha-report"></ul>');
7171
var stack = [report];
72-
var progressText;
72+
var progressText = items[0].getElementsByTagName('em')[0];
73+
var progressBar = items[0].getElementsByTagName('progress')[0];
7374
var root = document.getElementById('mocha');
7475

75-
{
76-
// On some broswers, canvas might be unavailable for whatever reason.
77-
// As such, we need text and progress
78-
var progressTextFallback = fragment('<li class="progress-text">progress: <em>0</em>%</li>');
79-
progressText = progressTextFallback.getElementsByTagName('em')[0];
80-
items[0].replaceWith(progressTextFallback);
81-
}
82-
8376
if (!root) {
8477
return error('#mocha div missing, add it to your document');
8578
}
@@ -224,12 +217,16 @@ function HTML(runner, options) {
224217
function updateStats() {
225218
// TODO: add to stats
226219
var percent = ((stats.tests / runner.total) * 100) | 0;
220+
progressBar.value = percent;
227221
if (progressText) {
228222
// setting a toFixed that is too low, makes small changes to progress not shown
229223
// setting it too high, makes the progress text longer then it needs to
230224
// to address this, calculate the toFixed based on the magnitude of total
231-
var decmalPlaces = Math.ceil(Math.log10(runner.total/100));
232-
text(progressText, percent.toFixed(Math.min(Math.max(decmalPlaces, 0), 100)));
225+
var decmalPlaces = Math.ceil(Math.log10(runner.total / 100));
226+
text(
227+
progressText,
228+
percent.toFixed(Math.min(Math.max(decmalPlaces, 0), 100))
229+
);
233230
}
234231

235232
// update stats

0 commit comments

Comments
 (0)