Convert what's left of HtmlReporter to an ES6 class

This commit is contained in:
Steve Gravrock
2025-10-07 06:31:50 -07:00
parent 5b3e12e4c5
commit 1f521f2a7f
2 changed files with 218 additions and 172 deletions

View File

@@ -118,72 +118,86 @@ jasmineRequire.HtmlReporter = function(j$) {
* @param options Options object. See lib/jasmine-core/boot1.js for details. * @param options Options object. See lib/jasmine-core/boot1.js for details.
* @since 1.2.0 * @since 1.2.0
*/ */
function HtmlReporter(options) { class HtmlReporter {
function config() { #env;
return (options.env && options.env.configuration()) || {}; #getContainer;
} #domContext;
#navigateWithNewParam;
#urlBuilder;
#filterSpecs;
#stateBuilder;
#htmlReporterMain;
const getContainer = options.getContainer; // Sub-views
const domContext = new j$.private.DomContext({ #alerts;
createElement: options.createElement, #symbols;
createTextNode: options.createTextNode #banner;
}); #failures;
const navigateWithNewParam = options.navigateWithNewParam || function() {};
const addToExistingQueryString = constructor(options) {
options.addToExistingQueryString || defaultQueryString; this.#env = options.env;
const urlBuilder = new UrlBuilder(addToExistingQueryString);
const filterSpecs = options.filterSpecs; this.#getContainer = options.getContainer;
let htmlReporterMain; this.#domContext = new j$.private.DomContext({
let alerts; createElement: options.createElement,
let symbols; createTextNode: options.createTextNode
let banner; });
let failures; this.#navigateWithNewParam =
options.navigateWithNewParam || function() {};
this.#urlBuilder = new UrlBuilder(
options.addToExistingQueryString || defaultQueryString
);
this.#filterSpecs = options.filterSpecs;
}
/** /**
* Initializes the reporter. Should be called before {@link Env#execute}. * Initializes the reporter. Should be called before {@link Env#execute}.
* @function * @function
* @name HtmlReporter#initialize * @name HtmlReporter#initialize
*/ */
this.initialize = function() { initialize() {
clearPrior(); this.#clearPrior();
alerts = new AlertsView(domContext, urlBuilder); this.#stateBuilder = new ResultsStateBuilder();
symbols = new SymbolsView(domContext, config);
banner = new Banner(domContext, navigateWithNewParam); this.#alerts = new AlertsView(this.#domContext, this.#urlBuilder);
failures = new FailuresView(domContext, urlBuilder); this.#symbols = new SymbolsView(
htmlReporterMain = domContext.create( this.#domContext,
this.#config.bind(this)
);
this.#banner = new Banner(this.#domContext, this.#navigateWithNewParam);
this.#failures = new FailuresView(this.#domContext, this.#urlBuilder);
this.#htmlReporterMain = this.#domContext.create(
'div', 'div',
{ className: 'jasmine_html-reporter' }, { className: 'jasmine_html-reporter' },
banner.rootEl, this.#banner.rootEl,
symbols.rootEl, this.#symbols.rootEl,
alerts.rootEl, this.#alerts.rootEl,
failures.rootEl this.#failures.rootEl
); );
getContainer().appendChild(htmlReporterMain); this.#getContainer().appendChild(this.#htmlReporterMain);
}; }
this.jasmineStarted = function(options) { jasmineStarted(options) {
stateBuilder.jasmineStarted(options); this.#stateBuilder.jasmineStarted(options);
}; }
const stateBuilder = new ResultsStateBuilder(); suiteStarted(result) {
this.#stateBuilder.suiteStarted(result);
}
this.suiteStarted = function(result) { suiteDone(result) {
stateBuilder.suiteStarted(result); this.#stateBuilder.suiteDone(result);
};
this.suiteDone = function(result) {
stateBuilder.suiteDone(result);
if (result.status === 'failed') { if (result.status === 'failed') {
failures.append(result, stateBuilder.currentParent); this.#failures.append(result, this.#stateBuilder.currentParent);
} }
}; }
this.specStarted = function(result) {}; specStarted() {}
this.specDone = function(result) { specDone(result) {
stateBuilder.specDone(result); this.#stateBuilder.specDone(result);
symbols.append(result, config()); this.#symbols.append(result, this.#config());
if (noExpectations(result)) { if (noExpectations(result)) {
const noSpecMsg = "Spec '" + result.fullName + "' has no expectations."; const noSpecMsg = "Spec '" + result.fullName + "' has no expectations.";
@@ -197,70 +211,79 @@ jasmineRequire.HtmlReporter = function(j$) {
} }
if (result.status === 'failed') { if (result.status === 'failed') {
failures.append(result, stateBuilder.currentParent); this.#failures.append(result, this.#stateBuilder.currentParent);
} }
}; }
this.jasmineDone = function(doneResult) { jasmineDone(doneResult) {
stateBuilder.jasmineDone(doneResult); this.#stateBuilder.jasmineDone(doneResult);
alerts.addDuration(doneResult.totalTime); this.#alerts.addDuration(doneResult.totalTime);
banner.showOptionsMenu(config()); this.#banner.showOptionsMenu(this.#config());
if (stateBuilder.specsExecuted < stateBuilder.totalSpecsDefined) { if (
alerts.addSkipped( this.#stateBuilder.specsExecuted < this.#stateBuilder.totalSpecsDefined
stateBuilder.specsExecuted, ) {
stateBuilder.totalSpecsDefined this.#alerts.addSkipped(
this.#stateBuilder.specsExecuted,
this.#stateBuilder.totalSpecsDefined
); );
} }
alerts.addSeedBar(doneResult, stateBuilder, doneResult.order); this.#alerts.addSeedBar(doneResult, this.#stateBuilder, doneResult.order);
if (doneResult.failedExpectations) { if (doneResult.failedExpectations) {
for (const f of doneResult.failedExpectations) { for (const f of doneResult.failedExpectations) {
alerts.addGlobalFailure(f); this.#alerts.addGlobalFailure(f);
} }
} }
for (const dw of stateBuilder.deprecationWarnings) { for (const dw of this.#stateBuilder.deprecationWarnings) {
alerts.addDeprecationWarning(dw); this.#alerts.addDeprecationWarning(dw);
} }
const results = find('.jasmine-results'); const results = this.#find('.jasmine-results');
const summary = new SummaryTreeView(domContext, urlBuilder, filterSpecs); const summary = new SummaryTreeView(
summary.addResults(stateBuilder.topResults); this.#domContext,
this.#urlBuilder,
this.#filterSpecs
);
summary.addResults(this.#stateBuilder.topResults);
results.appendChild(summary.rootEl); results.appendChild(summary.rootEl);
if (failures.any()) { if (this.#failures.any()) {
alerts.addFailureToggle( this.#alerts.addFailureToggle(
function() { () => this.#setMenuModeTo('jasmine-failure-list'),
setMenuModeTo('jasmine-failure-list'); () => this.#setMenuModeTo('jasmine-spec-list')
},
function() {
setMenuModeTo('jasmine-spec-list');
}
); );
setMenuModeTo('jasmine-failure-list'); this.#setMenuModeTo('jasmine-failure-list');
failures.show(); this.#failures.show();
} }
};
return this;
function find(selector) {
return getContainer().querySelector('.jasmine_html-reporter ' + selector);
} }
function clearPrior() { #config() {
const oldReporter = find(''); return (this.#env && this.#env.configuration()) || {};
}
#find(selector) {
return this.#getContainer().querySelector(
'.jasmine_html-reporter ' + selector
);
}
#clearPrior() {
const oldReporter = this.#find('');
if (oldReporter) { if (oldReporter) {
getContainer().removeChild(oldReporter); this.#getContainer().removeChild(oldReporter);
} }
} }
function setMenuModeTo(mode) { #setMenuModeTo(mode) {
htmlReporterMain.setAttribute('class', 'jasmine_html-reporter ' + mode); this.#htmlReporterMain.setAttribute(
'class',
'jasmine_html-reporter ' + mode
);
} }
} }

View File

@@ -83,72 +83,86 @@ jasmineRequire.HtmlReporter = function(j$) {
* @param options Options object. See lib/jasmine-core/boot1.js for details. * @param options Options object. See lib/jasmine-core/boot1.js for details.
* @since 1.2.0 * @since 1.2.0
*/ */
function HtmlReporter(options) { class HtmlReporter {
function config() { #env;
return (options.env && options.env.configuration()) || {}; #getContainer;
} #domContext;
#navigateWithNewParam;
#urlBuilder;
#filterSpecs;
#stateBuilder;
#htmlReporterMain;
const getContainer = options.getContainer; // Sub-views
const domContext = new j$.private.DomContext({ #alerts;
createElement: options.createElement, #symbols;
createTextNode: options.createTextNode #banner;
}); #failures;
const navigateWithNewParam = options.navigateWithNewParam || function() {};
const addToExistingQueryString = constructor(options) {
options.addToExistingQueryString || defaultQueryString; this.#env = options.env;
const urlBuilder = new UrlBuilder(addToExistingQueryString);
const filterSpecs = options.filterSpecs; this.#getContainer = options.getContainer;
let htmlReporterMain; this.#domContext = new j$.private.DomContext({
let alerts; createElement: options.createElement,
let symbols; createTextNode: options.createTextNode
let banner; });
let failures; this.#navigateWithNewParam =
options.navigateWithNewParam || function() {};
this.#urlBuilder = new UrlBuilder(
options.addToExistingQueryString || defaultQueryString
);
this.#filterSpecs = options.filterSpecs;
}
/** /**
* Initializes the reporter. Should be called before {@link Env#execute}. * Initializes the reporter. Should be called before {@link Env#execute}.
* @function * @function
* @name HtmlReporter#initialize * @name HtmlReporter#initialize
*/ */
this.initialize = function() { initialize() {
clearPrior(); this.#clearPrior();
alerts = new AlertsView(domContext, urlBuilder); this.#stateBuilder = new ResultsStateBuilder();
symbols = new SymbolsView(domContext, config);
banner = new Banner(domContext, navigateWithNewParam); this.#alerts = new AlertsView(this.#domContext, this.#urlBuilder);
failures = new FailuresView(domContext, urlBuilder); this.#symbols = new SymbolsView(
htmlReporterMain = domContext.create( this.#domContext,
this.#config.bind(this)
);
this.#banner = new Banner(this.#domContext, this.#navigateWithNewParam);
this.#failures = new FailuresView(this.#domContext, this.#urlBuilder);
this.#htmlReporterMain = this.#domContext.create(
'div', 'div',
{ className: 'jasmine_html-reporter' }, { className: 'jasmine_html-reporter' },
banner.rootEl, this.#banner.rootEl,
symbols.rootEl, this.#symbols.rootEl,
alerts.rootEl, this.#alerts.rootEl,
failures.rootEl this.#failures.rootEl
); );
getContainer().appendChild(htmlReporterMain); this.#getContainer().appendChild(this.#htmlReporterMain);
}; }
this.jasmineStarted = function(options) { jasmineStarted(options) {
stateBuilder.jasmineStarted(options); this.#stateBuilder.jasmineStarted(options);
}; }
const stateBuilder = new ResultsStateBuilder(); suiteStarted(result) {
this.#stateBuilder.suiteStarted(result);
}
this.suiteStarted = function(result) { suiteDone(result) {
stateBuilder.suiteStarted(result); this.#stateBuilder.suiteDone(result);
};
this.suiteDone = function(result) {
stateBuilder.suiteDone(result);
if (result.status === 'failed') { if (result.status === 'failed') {
failures.append(result, stateBuilder.currentParent); this.#failures.append(result, this.#stateBuilder.currentParent);
} }
}; }
this.specStarted = function(result) {}; specStarted() {}
this.specDone = function(result) { specDone(result) {
stateBuilder.specDone(result); this.#stateBuilder.specDone(result);
symbols.append(result, config()); this.#symbols.append(result, this.#config());
if (noExpectations(result)) { if (noExpectations(result)) {
const noSpecMsg = "Spec '" + result.fullName + "' has no expectations."; const noSpecMsg = "Spec '" + result.fullName + "' has no expectations.";
@@ -162,70 +176,79 @@ jasmineRequire.HtmlReporter = function(j$) {
} }
if (result.status === 'failed') { if (result.status === 'failed') {
failures.append(result, stateBuilder.currentParent); this.#failures.append(result, this.#stateBuilder.currentParent);
} }
}; }
this.jasmineDone = function(doneResult) { jasmineDone(doneResult) {
stateBuilder.jasmineDone(doneResult); this.#stateBuilder.jasmineDone(doneResult);
alerts.addDuration(doneResult.totalTime); this.#alerts.addDuration(doneResult.totalTime);
banner.showOptionsMenu(config()); this.#banner.showOptionsMenu(this.#config());
if (stateBuilder.specsExecuted < stateBuilder.totalSpecsDefined) { if (
alerts.addSkipped( this.#stateBuilder.specsExecuted < this.#stateBuilder.totalSpecsDefined
stateBuilder.specsExecuted, ) {
stateBuilder.totalSpecsDefined this.#alerts.addSkipped(
this.#stateBuilder.specsExecuted,
this.#stateBuilder.totalSpecsDefined
); );
} }
alerts.addSeedBar(doneResult, stateBuilder, doneResult.order); this.#alerts.addSeedBar(doneResult, this.#stateBuilder, doneResult.order);
if (doneResult.failedExpectations) { if (doneResult.failedExpectations) {
for (const f of doneResult.failedExpectations) { for (const f of doneResult.failedExpectations) {
alerts.addGlobalFailure(f); this.#alerts.addGlobalFailure(f);
} }
} }
for (const dw of stateBuilder.deprecationWarnings) { for (const dw of this.#stateBuilder.deprecationWarnings) {
alerts.addDeprecationWarning(dw); this.#alerts.addDeprecationWarning(dw);
} }
const results = find('.jasmine-results'); const results = this.#find('.jasmine-results');
const summary = new SummaryTreeView(domContext, urlBuilder, filterSpecs); const summary = new SummaryTreeView(
summary.addResults(stateBuilder.topResults); this.#domContext,
this.#urlBuilder,
this.#filterSpecs
);
summary.addResults(this.#stateBuilder.topResults);
results.appendChild(summary.rootEl); results.appendChild(summary.rootEl);
if (failures.any()) { if (this.#failures.any()) {
alerts.addFailureToggle( this.#alerts.addFailureToggle(
function() { () => this.#setMenuModeTo('jasmine-failure-list'),
setMenuModeTo('jasmine-failure-list'); () => this.#setMenuModeTo('jasmine-spec-list')
},
function() {
setMenuModeTo('jasmine-spec-list');
}
); );
setMenuModeTo('jasmine-failure-list'); this.#setMenuModeTo('jasmine-failure-list');
failures.show(); this.#failures.show();
} }
};
return this;
function find(selector) {
return getContainer().querySelector('.jasmine_html-reporter ' + selector);
} }
function clearPrior() { #config() {
const oldReporter = find(''); return (this.#env && this.#env.configuration()) || {};
}
#find(selector) {
return this.#getContainer().querySelector(
'.jasmine_html-reporter ' + selector
);
}
#clearPrior() {
const oldReporter = this.#find('');
if (oldReporter) { if (oldReporter) {
getContainer().removeChild(oldReporter); this.#getContainer().removeChild(oldReporter);
} }
} }
function setMenuModeTo(mode) { #setMenuModeTo(mode) {
htmlReporterMain.setAttribute('class', 'jasmine_html-reporter ' + mode); this.#htmlReporterMain.setAttribute(
'class',
'jasmine_html-reporter ' + mode
);
} }
} }