Skip to content

Commit

Permalink
HTML Reporter: Optimize toolbar setup by reusing beginDetails.modules
Browse files Browse the repository at this point in the history
`config.modules` is an undocumented internal property with lots of
other internal state in it. Relying on it here is suboptimal, and
creating a copy of it is inefficient.

Use `beginDetails.modules` instead which exists precisely for this
purpose. It was added by commit 168b048 in QUnit 1.16 and initially
used by HTML Reporter. However, commit 43a3d87 in QUnit 2.7 removed
its use again and in c494fb8 I removed the unused variables that
were left begin there.
  • Loading branch information
Krinkle committed Apr 18, 2022
1 parent 7bdbc28 commit e31c8d3
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/html-reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,34 +363,31 @@ export function escapeText (str) {
return html;
}

function toolbarModuleFilter () {
function toolbarModuleFilter (beginDetails) {
let initialSelected = null;
dropdownData = {
options: [],
options: beginDetails.modules.slice(),
selectedMap: new StringMap(),
isDirty: function () {
return [...dropdownData.selectedMap.keys()].sort().join(',') !==
[...initialSelected.keys()].sort().join(',');
}
};
for (let i = 0; i < config.modules.length; i++) {
const mod = config.modules[i];
if (mod.name === '') {
// Ignore the implicit and unnamed "global" test module.
continue;
}
dropdownData.options.push({
moduleId: mod.moduleId,
name: mod.name
});

if (config.moduleId.length) {
// The module dropdown is seeded with the runtime configuration of the last run.
//
// We keep our own copy, because:
// We don't reference `config.moduleId` directly after this and keep our own
// copy because:
// 1. This naturaly filters out unknown moduleIds.
// 2. Gives us a place to manage and remember unsubmitted checkbox changes.
if (config.moduleId.indexOf(mod.moduleId) !== -1) {
dropdownData.selectedMap.set(mod.moduleId, mod.name);
// 3. Gives us an efficient way to map a selected moduleId to module name
// during rendering.
for (let i = 0; i < beginDetails.modules.length; i++) {
const mod = beginDetails.modules[i];
if (config.moduleId.indexOf(mod.moduleId) !== -1) {
dropdownData.selectedMap.set(mod.moduleId, mod.name);
}
}
}
initialSelected = new StringMap(dropdownData.selectedMap);
Expand Down Expand Up @@ -565,23 +562,21 @@ export function escapeText (str) {
return moduleFilter;
}

function toolbarFilters () {
const toolbarFilters = document.createElement('span');

toolbarFilters.id = 'qunit-toolbar-filters';
toolbarFilters.appendChild(toolbarLooseFilter());
toolbarFilters.appendChild(toolbarModuleFilter());

return toolbarFilters;
}

function appendToolbar () {
function appendToolbar (beginDetails) {
const toolbar = id('qunit-testrunner-toolbar');

if (toolbar) {
toolbar.appendChild(toolbarUrlConfigContainer());
toolbar.appendChild(toolbarFilters());
toolbar.appendChild(document.createElement('div')).className = 'clearfix';

const toolbarFilters = document.createElement('span');
toolbarFilters.id = 'qunit-toolbar-filters';
toolbarFilters.appendChild(toolbarLooseFilter());
toolbarFilters.appendChild(toolbarModuleFilter(beginDetails));

const clearfix = document.createElement('div');
clearfix.className = 'clearfix';

toolbar.appendChild(toolbarFilters);
toolbar.appendChild(document.createElement('div'));
}
}

Expand Down Expand Up @@ -653,7 +648,7 @@ export function escapeText (str) {
}
}

function appendInterface () {
function appendInterface (beginDetails) {
const qunit = id('qunit');

// For compat with QUnit 1.2, and to support fully custom theme HTML,
Expand Down Expand Up @@ -682,7 +677,7 @@ export function escapeText (str) {
appendBanner();
appendTestResults();
appendUserAgent();
appendToolbar();
appendToolbar(beginDetails);
}

function appendTest (name, testId, moduleName) {
Expand Down Expand Up @@ -722,15 +717,15 @@ export function escapeText (str) {
stats.defined = runStart.testCounts.total;
});

QUnit.begin(function () {
QUnit.begin(function (beginDetails) {
// Initialize QUnit elements
// This is done from begin() instead of runStart, because
// urlparams.js uses begin(), which we need to wait for.
// urlparams.js in turn uses begin() to allow plugins to
// add entries to QUnit.config.urlConfig, which may be done
// asynchronously.
// <https://github.com/qunitjs/qunit/issues/1657>
appendInterface();
appendInterface(beginDetails);
});

function getRerunFailedHtml (failedTests) {
Expand Down

0 comments on commit e31c8d3

Please sign in to comment.