Skip to content

Commit

Permalink
report: handle underscored not_applicable scoreDisplayMode (#6549)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored Nov 13, 2018
1 parent f1d6bdc commit b70d1ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lighthouse-core/report/html/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class Util {
if (typeof clone.categories !== 'object') throw new Error('No categories provided.');
clone.reportCategories = Object.values(clone.categories);

// The proto process turns 'not-applicable' into 'not_applicable'. Correct this to support both.
// TODO: remove when underscore/hyphen proto issue is resolved. See #6371, #6201.
for (const audit of Object.values(clone.audits)) {
// @ts-ignore tsc rightly flags that this value shouldn't occur.
if (audit.scoreDisplayMode === 'not_applicable') {
audit.scoreDisplayMode = 'not-applicable';
}
}

// For convenience, smoosh all AuditResults into their auditDfn (which has just weight & group)
for (const category of clone.reportCategories) {
category.auditRefs.forEach(auditMeta => {
Expand Down
21 changes: 21 additions & 0 deletions lighthouse-core/test/report/html/renderer/report-renderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,25 @@ describe('ReportRenderer', () => {
renderer.setTemplateContext(otherDocument);
assert.equal(renderer._templateContext, otherDocument);
});

it('renders `not_applicable` audits as `not-applicable`', () => {
const clonedSampleResult = JSON.parse(JSON.stringify(sampleResultsOrig));

let notApplicableCount = 0;
Object.values(clonedSampleResult.audits).forEach(audit => {
if (audit.scoreDisplayMode === 'not-applicable') {
notApplicableCount++;
audit.scoreDisplayMode = 'not_applicable';
}
});

assert.ok(notApplicableCount > 20); // Make sure something's being tested.

const container = renderer._dom._document.body;
const reportElement = renderer.renderReport(sampleResults, container);
const notApplicableElementCount = reportElement
.querySelectorAll('.lh-audit--not-applicable').length;

assert.strictEqual(notApplicableCount, notApplicableElementCount);
});
});
21 changes: 21 additions & 0 deletions lighthouse-core/test/report/html/renderer/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const assert = require('assert');
const Util = require('../../../../report/html/renderer/util.js');
const sampleResult = require('../../../results/sample_v2.json');

const NBSP = '\xa0';

Expand Down Expand Up @@ -168,4 +169,24 @@ describe('util helpers', () => {
Util.formatDisplayValue(displayValue);
assert.deepStrictEqual(displayValue, cloned, 'displayValue was mutated');
});

describe('#prepareReportResult', () => {
it('corrects underscored `not-applicable` scoreDisplayMode', () => {
const clonedSampleResult = JSON.parse(JSON.stringify(sampleResult));

let notApplicableCount = 0;
Object.values(clonedSampleResult.audits).forEach(audit => {
if (audit.scoreDisplayMode === 'not-applicable') {
notApplicableCount++;
audit.scoreDisplayMode = 'not_applicable';
}
});

assert.ok(notApplicableCount > 20); // Make sure something's being tested.

// Original audit results should be restored.
const preparedResult = Util.prepareReportResult(clonedSampleResult);
assert.deepStrictEqual(preparedResult.audits, sampleResult.audits);
});
});
});

0 comments on commit b70d1ca

Please sign in to comment.