Skip to content

Commit

Permalink
clients(psi): add LH.Result object support to prepareLabData (#6432)
Browse files Browse the repository at this point in the history
  • Loading branch information
exterkamp authored and paulirish committed Oct 31, 2018
1 parent 9615070 commit 5a113dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lighthouse-core/report/html/renderer/psi.js
Expand Up @@ -29,12 +29,14 @@
* document
* );
*
* @param {string} LHResultJsonString The stringified version of {LH.Result}
* @param {LH.Result | string} LHResult The stringified version of {LH.Result}
* @param {Document} document The host page's window.document
* @return {{scoreGaugeEl: Element, perfCategoryEl: Element, finalScreenshotDataUri: string|null}}
*/
function prepareLabData(LHResultJsonString, document) {
const lhResult = /** @type {LH.Result} */ (JSON.parse(LHResultJsonString));
function prepareLabData(LHResult, document) {
const lhResult = (typeof LHResult === 'string') ?
/** @type {LH.Result} */ (JSON.parse(LHResult)) : LHResult;

const dom = new DOM(document);

// Assume fresh styles needed on every call, so mark all template styles as unused.
Expand Down
29 changes: 23 additions & 6 deletions lighthouse-core/test/report/html/renderer/psi-test.js
Expand Up @@ -20,8 +20,11 @@ const CriticalRequestChainRenderer =
require('../../../../report/html/renderer/crc-details-renderer');

const sampleResultsStr = fs.readFileSync(__dirname + '/../../../results/sample_v2.json', 'utf-8');
const sampleResults = JSON.parse(sampleResultsStr)
;
const sampleResultsRoundtripStr = fs.readFileSync(
__dirname + '/../../../../../proto/sample_v2_round_trip.json',
'utf-8'
);

const TEMPLATE_FILE = fs.readFileSync(
__dirname + '/../../../../report/html/templates.html',
'utf8'
Expand Down Expand Up @@ -60,7 +63,22 @@ describe('DOM', () => {

describe('psi prepareLabData helpers', () => {
describe('prepareLabData', () => {
it('reports expected data', () => {
it('succeeds with LHResult object (roundtrip) input', () => {
const roundTripLHResult = /** @type {LH.Result} */ JSON.parse(sampleResultsRoundtripStr);
const result = prepareLabData(roundTripLHResult, document);

// sanity check that the report exists and has some content
assert.ok(result.perfCategoryEl instanceof document.defaultView.Element);
assert.ok(result.perfCategoryEl.outerHTML.length > 50000, 'perfCategory HTML is populated');
assert.ok(!result.perfCategoryEl.outerHTML.includes('lh-permalink'),
'PSI\'s perfCategory HTML doesn\'t include a lh-permalink element');
// Assume using default locale.
const titleEl = result.perfCategoryEl.querySelector('.lh-audit-group--metrics')
.querySelector('.lh-audit-group__header');
assert.equal(titleEl.textContent, Util.UIStrings.labDataTitle);
});

it('succeeds with stringified LHResult input', () => {
const result = prepareLabData(sampleResultsStr, document);
assert.ok(result.scoreGaugeEl instanceof document.defaultView.Element);
assert.equal(result.scoreGaugeEl.querySelector('.lh-gauge__wrapper').href, '');
Expand Down Expand Up @@ -115,14 +133,13 @@ describe('DOM', () => {

describe('_getFinalScreenshot', () => {
it('gets a datauri as a string', () => {
const LHResultJsonString = JSON.stringify(sampleResults);
const datauri = prepareLabData(LHResultJsonString, document).finalScreenshotDataUri;
const datauri = prepareLabData(sampleResultsStr, document).finalScreenshotDataUri;
assert.equal(typeof datauri, 'string');
assert.ok(datauri.startsWith('data:image/jpeg;base64,'));
});

it('returns null if there is no final-screenshot audit', () => {
const clonedResults = JSON.parse(JSON.stringify(sampleResults));
const clonedResults = JSON.parse(sampleResultsStr);
delete clonedResults.audits['final-screenshot'];
const LHResultJsonString = JSON.stringify(clonedResults);
const datauri = prepareLabData(LHResultJsonString, document).finalScreenshotDataUri;
Expand Down

0 comments on commit 5a113dc

Please sign in to comment.