Skip to content

Commit 4c9f318

Browse files
committed
Merge branch 'master' into issue/4047
2 parents dce26f4 + e263c7a commit 4c9f318

File tree

3 files changed

+81
-177
lines changed

3 files changed

+81
-177
lines changed

lib/browser/progress.js

Lines changed: 0 additions & 138 deletions
This file was deleted.

lib/reporters/html.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
var Base = require('./base');
1212
var utils = require('../utils');
13-
var Progress = require('../browser/progress');
1413
var escapeRe = require('escape-string-regexp');
1514
var constants = require('../runner').constants;
1615
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
@@ -38,7 +37,7 @@ exports = module.exports = HTML;
3837

3938
var statsTemplate =
4039
'<ul id="mocha-stats">' +
41-
'<li class="progress"><canvas width="40" height="40"></canvas></li>' +
40+
'<li class="progress-contain"><progress class="progress-element" max="100" value="0"></progress><svg class="progress-ring"><circle class="ring-flatlight" stroke-dasharray="100%,0%"/><circle class="ring-highlight" stroke-dasharray="0%,100%"/></svg><div class="progress-text">0%</div></li>' +
4241
'<li class="passes"><a href="javascript:void(0);">passes:</a> <em>0</em></li>' +
4342
'<li class="failures"><a href="javascript:void(0);">failures:</a> <em>0</em></li>' +
4443
'<li class="duration">duration: <em>0</em>s</li>' +
@@ -68,24 +67,16 @@ function HTML(runner, options) {
6867
var failures = items[2].getElementsByTagName('em')[0];
6968
var failuresLink = items[2].getElementsByTagName('a')[0];
7069
var duration = items[3].getElementsByTagName('em')[0];
71-
var canvas = stat.getElementsByTagName('canvas')[0];
7270
var report = fragment('<ul id="mocha-report"></ul>');
7371
var stack = [report];
74-
var progress;
75-
var ctx;
72+
var progressText = items[0].getElementsByTagName('div')[0];
73+
var progressBar = items[0].getElementsByTagName('progress')[0];
74+
var progressRing = [
75+
items[0].getElementsByClassName('ring-flatlight')[0],
76+
items[0].getElementsByClassName('ring-highlight')[0]];
77+
var progressRingRadius = null; // computed CSS unavailable now, so set later
7678
var root = document.getElementById('mocha');
7779

78-
if (canvas.getContext) {
79-
var ratio = window.devicePixelRatio || 1;
80-
canvas.style.width = canvas.width;
81-
canvas.style.height = canvas.height;
82-
canvas.width *= ratio;
83-
canvas.height *= ratio;
84-
ctx = canvas.getContext('2d');
85-
ctx.scale(ratio, ratio);
86-
progress = new Progress();
87-
}
88-
8980
if (!root) {
9081
return error('#mocha div missing, add it to your document');
9182
}
@@ -115,10 +106,6 @@ function HTML(runner, options) {
115106
root.appendChild(stat);
116107
root.appendChild(report);
117108

118-
if (progress) {
119-
progress.size(40);
120-
}
121-
122109
runner.on(EVENT_SUITE_BEGIN, function (suite) {
123110
if (suite.root) {
124111
return;
@@ -234,8 +221,26 @@ function HTML(runner, options) {
234221
function updateStats() {
235222
// TODO: add to stats
236223
var percent = ((stats.tests / runner.total) * 100) | 0;
237-
if (progress) {
238-
progress.update(percent).draw(ctx);
224+
progressBar.value = percent;
225+
if (progressText) {
226+
// setting a toFixed that is too low, makes small changes to progress not shown
227+
// setting it too high, makes the progress text longer then it needs to
228+
// to address this, calculate the toFixed based on the magnitude of total
229+
var decimalPlaces = Math.ceil(Math.log10(runner.total / 100));
230+
text(
231+
progressText,
232+
percent.toFixed(Math.min(Math.max(decimalPlaces, 0), 100)) + '%'
233+
);
234+
}
235+
if (progressRing) {
236+
var radius = parseFloat(getComputedStyle(progressRing[0]).getPropertyValue('r'));
237+
var wholeArc = Math.PI * 2 * radius;
238+
var highlightArc = percent * (wholeArc / 100);
239+
// The progress ring is in 2 parts, the flatlight color and highlight color.
240+
// Rendering both on top of the other, seems to make a 3rd color on the edges.
241+
// To create 1 whole ring with 2 colors, both parts are inverse of the other.
242+
progressRing[0].style['stroke-dasharray'] = `0,${highlightArc}px,${wholeArc}px`;
243+
progressRing[1].style['stroke-dasharray'] = `${highlightArc}px,${wholeArc}px`;
239244
}
240245

241246
// update stats

mocha.css

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
--mocha-stats-color: #888;
2323
--mocha-stats-em-color: #000;
2424
--mocha-stats-hover-color: #eee;
25+
--mocha-progress-ring-color: #eee;
26+
--mocha-progress-ring-highlight-color: #9f9f9f;
27+
--mocha-progress-text-color: #000;
2528
--mocha-error-color: #c00;
2629

2730
--mocha-code-comment: #ddd;
@@ -54,6 +57,9 @@
5457
--mocha-stats-color: #aaa;
5558
--mocha-stats-em-color: #fff;
5659
--mocha-stats-hover-color: #444;
60+
--mocha-progress-ring-color: #444;
61+
--mocha-progress-ring-highlight-color: #888;
62+
--mocha-progress-text-color: #fff;
5763
--mocha-error-color: #f44;
5864

5965
--mocha-code-comment: #ddd;
@@ -325,6 +331,10 @@ body {
325331
}
326332

327333
#mocha-stats {
334+
--ring-container-size: 40px;
335+
--ring-size: 39px;
336+
--ring-radius: calc(var(--ring-size) / 2);
337+
328338
position: fixed;
329339
top: 15px;
330340
right: 10px;
@@ -334,20 +344,52 @@ body {
334344
z-index: 1;
335345
}
336346

337-
#mocha-stats .progress {
347+
#mocha-stats .progress-contain {
338348
float: right;
339-
padding-top: 0;
349+
padding: 0;
350+
}
351+
352+
#mocha-stats :is(.progress-element, .progress-text) {
353+
width: var(--ring-container-size);
354+
display: block;
355+
top: 12px;
356+
position: absolute;
357+
}
358+
359+
#mocha-stats .progress-element {
360+
visibility: hidden;
361+
height: calc(var(--ring-container-size) / 2);
362+
}
363+
364+
#mocha-stats .progress-text {
365+
text-align: center;
366+
text-overflow: clip;
367+
overflow: hidden;
368+
color: var(--mocha-stats-em-color);
369+
font-size: 11px;
370+
}
340371

341-
/**
342-
* Set safe initial values, so mochas .progress does not inherit these
343-
* properties from Bootstrap .progress (which causes .progress height to
344-
* equal line height set in Bootstrap).
345-
*/
346-
height: auto;
347-
-webkit-box-shadow: none;
348-
-moz-box-shadow: none;
349-
box-shadow: none;
350-
background-color: initial;
372+
#mocha-stats .progress-ring {
373+
width: var(--ring-container-size);
374+
height: var(--ring-container-size);
375+
}
376+
377+
#mocha-stats :is(.ring-flatlight, .ring-highlight) {
378+
--stroke-thickness: 1.65px;
379+
--center: calc(var(--ring-container-size) / 2);
380+
cx: var(--center);
381+
cy: var(--center);
382+
r: calc(var(--ring-radius) - calc(var(--stroke-thickness) / 2));
383+
fill: hsla(0, 0%, 0%, 0);
384+
stroke-width: var(--stroke-thickness);
385+
}
386+
387+
#mocha-stats .ring-flatlight {
388+
stroke: var(--mocha-progress-ring-color);
389+
}
390+
391+
#mocha-stats .ring-highlight {
392+
stroke: var(--mocha-progress-ring-highlight-color);
351393
}
352394

353395
#mocha-stats em {
@@ -370,11 +412,6 @@ body {
370412
padding-top: 11px;
371413
}
372414

373-
#mocha-stats canvas {
374-
width: 40px;
375-
height: 40px;
376-
}
377-
378415
#mocha code .comment { color: var(--mocha-code-comment); }
379416
#mocha code .init { color: var(--mocha-code-init); }
380417
#mocha code .string { color: var(--mocha-code-string); }

0 commit comments

Comments
 (0)