Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Aug 1, 2018
1 parent d5f77e7 commit fb85d06
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 45 deletions.
24 changes: 4 additions & 20 deletions lighthouse-core/report/html/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,16 @@ class Util {
// TODO(phulce): we all agree this is technical debt we should fix
if (typeof clone.categories !== 'object') throw new Error('No categories provided.');
clone.reportCategories = Object.values(clone.categories);
Util.smooshAuditResultsIntoCategories(clone.audits, clone.reportCategories);
return clone;
}

/**
* Place the AuditResult into the auditDfn (which has just weight & group)
* @param {Object<string, LH.Audit.Result>} audits
* @param {Array<LH.ReportResult.Category>} reportCategories
*/
static smooshAuditResultsIntoCategories(audits, reportCategories) {
for (const category of reportCategories) {
// For convenience, smoosh all AuditResults into their auditDfn (which has just weight & group)
for (const category of clone.reportCategories) {
category.auditRefs.forEach(auditMeta => {
const result = audits[auditMeta.id];
const result = clone.audits[auditMeta.id];
auditMeta.result = result;
});
}
}

/**
* @param {LH.I18NRendererStrings} rendererFormattedStrings
*/
static updateAllUIStrings(rendererFormattedStrings) {
// TODO(i18n): don't mutate these here but on the LHR and pass that around everywhere
for (const [key, value] of Object.entries(rendererFormattedStrings)) {
Util.UIStrings[key] = value;
}
return clone;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const DetailsRenderer = require('../../../../report/html/renderer/details-render
const CriticalRequestChainRenderer = require(
'../../../../report/html/renderer/crc-details-renderer.js');
const CategoryRenderer = require('../../../../report/html/renderer/category-renderer.js');
const ReportRenderer = require('../../../../report/html/renderer/report-renderer.js');
const sampleResults = require('../../../results/sample_v2.json');
const sampleResultsOrig = require('../../../results/sample_v2.json');

const TEMPLATE_FILE = fs.readFileSync(__dirname +
'/../../../../report/html/templates.html', 'utf8');

describe('CategoryRenderer', () => {
let renderer;
let sampleResults;

beforeAll(() => {
global.URL = URL;
Expand All @@ -36,9 +36,7 @@ describe('CategoryRenderer', () => {
const detailsRenderer = new DetailsRenderer(dom);
renderer = new CategoryRenderer(dom, detailsRenderer);

sampleResults.reportCategories = Object.values(sampleResults.categories);
ReportRenderer.smooshAuditResultsIntoCategories(sampleResults.audits,
sampleResults.reportCategories);
sampleResults = Util.prepareReportResult(sampleResultsOrig);
});

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const DetailsRenderer = require('../../../../report/html/renderer/details-render
const CriticalRequestChainRenderer = require(
'../../../../report/html/renderer/crc-details-renderer.js');
const CategoryRenderer = require('../../../../report/html/renderer/category-renderer.js');
const ReportRenderer = require('../../../../report/html/renderer/report-renderer.js');
const sampleResults = require('../../../results/sample_v2.json');
const sampleResultsOrig = require('../../../results/sample_v2.json');

const TEMPLATE_FILE = fs.readFileSync(__dirname +
'/../../../../report/html/templates.html', 'utf8');

describe('PerfCategoryRenderer', () => {
let category;
let renderer;
let sampleResults;

beforeAll(() => {
global.URL = URL;
Expand All @@ -40,10 +40,9 @@ describe('PerfCategoryRenderer', () => {
const dom = new DOM(document);
const detailsRenderer = new DetailsRenderer(dom);
renderer = new PerformanceCategoryRenderer(dom, detailsRenderer);
sampleResults.reportCategories = Object.values(sampleResults.categories);

sampleResults = Util.prepareReportResult(sampleResultsOrig);
category = sampleResults.reportCategories.find(cat => cat.id === 'performance');
ReportRenderer.smooshAuditResultsIntoCategories(sampleResults.audits,
sampleResults.reportCategories);
});

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ let PerformanceCategoryRenderer = null;
const CriticalRequestChainRenderer = require(
'../../../../report/html/renderer/crc-details-renderer.js');
const ReportRenderer = require('../../../../report/html/renderer/report-renderer.js');
const sampleResults = require('../../../results/sample_v2.json');
const sampleResultsOrig = require('../../../results/sample_v2.json');

const TIMESTAMP_REGEX = /\d+, \d{4}.*\d+:\d+/;
const TEMPLATE_FILE = fs.readFileSync(__dirname +
'/../../../../report/html/templates.html', 'utf8');

describe('ReportRenderer', () => {
let renderer;
let sampleResults;

beforeAll(() => {
global.URL = URL;
Expand Down Expand Up @@ -57,7 +58,7 @@ describe('ReportRenderer', () => {
const detailsRenderer = new DetailsRenderer(dom);
const categoryRenderer = new CategoryRenderer(dom, detailsRenderer);
renderer = new ReportRenderer(dom, categoryRenderer);
sampleResults.reportCategories = Object.values(sampleResults.categories);
sampleResults = Util.prepareReportResult(sampleResultsOrig);
});

afterAll(() => {
Expand Down
20 changes: 7 additions & 13 deletions lighthouse-core/test/report/html/renderer/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,32 +184,26 @@ describe('util helpers', () => {
});

describe('getFinalScreenshot', () => {
const cloneResults = JSON.parse(JSON.stringify(sampleResults));
cloneResults.reportCategories = Object.values(cloneResults.categories);
ReportRenderer.smooshAuditResultsIntoCategories(cloneResults.audits,
cloneResults.reportCategories);

it('gets a datauri as a string', () => {
const cloneResults = Util.prepareReportResult(sampleResults);
const datauri = Util.getFinalScreenshot(cloneResults);
assert.equal(typeof datauri, 'string');
assert.ok(datauri.startsWith('data:image/jpeg;base64,'));
});

it('returns null if there is no perf category', () => {
const lhrWithoutPerf = JSON.parse(JSON.stringify(sampleResults));
delete lhrWithoutPerf.categories.performance;
lhrWithoutPerf.reportCategories = Object.values(lhrWithoutPerf.categories);
const clonedResults = JSON.parse(JSON.stringify(sampleResults));
delete clonedResults.categories.performance;
const lhrWithoutPerf = Util.prepareReportResult(clonedResults);

const datauri = Util.getFinalScreenshot(lhrWithoutPerf);
assert.equal(datauri, null);
});

it('returns null if there is no final-screenshot audit', () => {
const lhrNoFinalSS = JSON.parse(JSON.stringify(sampleResults));
delete lhrNoFinalSS.audits['final-screenshot'];
lhrNoFinalSS.reportCategories = Object.values(lhrNoFinalSS.categories);
ReportRenderer.smooshAuditResultsIntoCategories(lhrNoFinalSS.audits,
lhrNoFinalSS.reportCategories);
const clonedResults = JSON.parse(JSON.stringify(sampleResults));
delete clonedResults.audits['final-screenshot'];
const lhrNoFinalSS = Util.prepareReportResult(clonedResults);

const datauri = Util.getFinalScreenshot(lhrNoFinalSS);
assert.equal(datauri, null);
Expand Down

0 comments on commit fb85d06

Please sign in to comment.