Refactor test code.

This commit is contained in:
Lars Jung 2015-04-26 21:37:41 +02:00
parent 21c976bb3d
commit b95aa583a9

View file

@ -1,63 +1,68 @@
(function () {
'use strict';
var showOnlyFailures = false;
function toggleFailureFilter(ev) {
ev.stopImmediatePropagation();
showOnlyFailures = !showOnlyFailures;
if (showOnlyFailures) {
$('.suite, .test').hide();
$('.suite.fail, .test.fail').show();
} else {
$('.suite, .test').show();
}
}
function addSuiteStats() {
var $suite = $(this);
var tests = $suite.find('.test').length;
var passed = $suite.find('.test.pass').length;
var failed = tests - passed;
var $header = $suite.find('> h1 a');
var $count = $('<span class="count"><span class="passed">' + passed + '</span><span class="failed">' + failed + '</span></span>');
if (!failed) {
$count.find('.failed').remove();
}
$suite.addClass(tests === passed ? 'pass' : 'fail');
$header.append($count);
}
function fixCodeFormatting() {
var $code = $(this);
$code.text($code.text().trim().replace(/;\n\s*/g, ';\n'));
}
function onEnd() {
var runner = this;
var failed = runner.stats.failures > 0;
var stats = (runner.stats.duration / 1000.0).toFixed(2) + 's';
var stats = (runner.stats.duration / 1000.0).toFixed(3) + 's';
if (failed) {
var onlyFailures = false;
$('#report .stats').on('click', function (ev) {
onlyFailures = !onlyFailures;
if (onlyFailures) {
$('.suite, .test').hide();
$('.suite.fail, .test.fail').show();
} else {
$('.suite, .test').show();
}
ev.stopImmediatePropagation();
});
$('#report .stats').on('click', toggleFailureFilter);
}
$('#report').addClass(failed ? 'fail' : 'pass');
$('#report .progress').hide();
$('#report .stats').text(stats);
$('#mocha-overlay .suite').each(function () {
var $suite = $(this);
var tests = $suite.find('.test').length;
var passed = $suite.find('.test.pass').length;
var failed = tests - passed;
var $header = $suite.find('> h1 a');
var $count = $('<span class="count"><span class="passed">' + passed + '</span><span class="failed">' + failed + '</span></span>');
if (!failed) {
$count.find('.failed').remove();
}
$suite.addClass(tests === passed ? 'pass' : 'fail');
$header.append($count);
});
$('#mocha-overlay code').each(function () {
var $code = $(this);
$code.text($code.text().trim().replace(/;\n\s*/g, ';\n'));
});
$('#mocha-overlay .suite').each(addSuiteStats);
$('#mocha-overlay code').each(fixCodeFormatting);
}
function onTest() {
var runner = this;
var complete = runner.stats.tests;
var total = runner.total;
var percent = 100.0 - 100.0 * complete / total;
var percent = 100.0 - 100.0 * runner.stats.tests / runner.total;
if (runner.stats.failures) {
$('#report').addClass('fail');