Skip to content

Commit 1876f4e

Browse files
authored
Fallback for progress when canvas isn't available
1 parent 37deed2 commit 1876f4e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/reporters/html.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function HTML(runner, options) {
7373
var stack = [report];
7474
var progress;
7575
var ctx;
76+
var progressText;
7677
var root = document.getElementById('mocha');
7778

7879
if (canvas.getContext) {
@@ -84,6 +85,12 @@ function HTML(runner, options) {
8485
ctx = canvas.getContext('2d');
8586
ctx.scale(ratio, ratio);
8687
progress = new Progress();
88+
} else {
89+
// On some broswers, canvas might be unavailable for whatever reason.
90+
// As such, we need a text version as a fallback
91+
var progressTextFallback = fragment('<em>0</em>%');
92+
progressText = stat.getElementsByTagName('em')[0];
93+
items[0].appendChild(progressTextFallback);
8794
}
8895

8996
if (!root) {
@@ -236,6 +243,12 @@ function HTML(runner, options) {
236243
var percent = ((stats.tests / runner.total) * 100) | 0;
237244
if (progress) {
238245
progress.update(percent).draw(ctx);
246+
} else if (progressText) {
247+
// setting a toFixed that is too low, makes small changes to progress not shown
248+
// setting it too high, makes the progress text longer then it needs to
249+
// to address this, calculate the toFixed based on the magnitude of total
250+
var decmalPlaces = Math.ceil(Math.log10(runner.total/100));
251+
text(progressText, percent.toFixed(Math.min(Math.max(decmalPlaces), 0), 100));
239252
}
240253

241254
// update stats

0 commit comments

Comments
 (0)