Skip to content

Commit

Permalink
report(flow): category tooltips (#13043)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine authored Sep 22, 2021
1 parent 9dbaf73 commit 54fff07
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 34 deletions.
53 changes: 52 additions & 1 deletion flow-report/assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
--summary-flow-step-label-font-size: 16px;
--summary-subtitle-font-size: 16px;
--summary-title-font-size: 32px;
--summary-tooltip-box-shadow-color: rgba(0, 0, 0, 0.25);
--topbar-title-font-size: 14px;
}

Expand Down Expand Up @@ -292,9 +293,59 @@
text-decoration: underline;
}

.SummaryCategory {
.SummaryCategory__content {
position: relative;
margin: var(--half-base-spacing);
}
.SummaryCategory__content:hover,
.SummaryCategory__content:focus-within {
background-color: var(--color-gray-100);
}
.SummaryCategory__content:hover .SummaryTooltip,
.SummaryCategory__content:focus-within .SummaryTooltip {
display: block;
}

.SummaryTooltip {
display: none;
position: absolute;
min-width: 200%;
max-width: 300%;
width: max-content;
background-color: var(--report-background-color);
border: 1px solid var(--color-gray-900);
border-radius: 5px;
padding: var(--base-spacing);
right: 0;
box-shadow: 0px 4px 4px var(--summary-tooltip-box-shadow-color);
z-index: 1;
}

.SummaryTooltip__title {
font-weight: bold;
margin-bottom: var(--half-base-spacing);
}

.SummaryTooltip__category {
font-weight: bold;
display: flex;
margin-top: var(--half-base-spacing);
}

.SummaryTooltip__category-title {
flex-grow: 1;
}

.SummaryTooltip__rating--pass {
color: var(--color-pass);
}
.SummaryTooltip__rating--average {
color: var(--color-average);
}
.SummaryTooltip__rating--fail,
.SummaryTooltip__rating--error {
color: var(--color-fail);
}

.SummaryNavigationHeader {
font-size: var(--summary-flow-step-label-font-size);
Expand Down
92 changes: 92 additions & 0 deletions flow-report/src/summary/category.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import {FunctionComponent} from 'preact';

import {Util} from '../../../report/renderer/util';
import {Separator} from '../common';
import {CategoryScore} from '../wrappers/category-score';

const GATHER_MODE_LABELS: Record<LH.Result.GatherMode, string> = {
'navigation': 'Navigation report',
'timespan': 'Timespan report',
'snapshot': 'Snapshot report',
};

const RATING_LABELS: Record<string, string> = {
'pass': 'Good',
'fail': 'Poor',
'average': 'Average',
'error': 'Error',
};

export const SummaryTooltip: FunctionComponent<{
category: LH.ReportResult.Category,
gatherMode: LH.Result.GatherMode
}> = ({category, gatherMode}) => {
const {numPassed, numAudits, totalWeight} = Util.calculateCategoryFraction(category);

const displayAsFraction = Util.shouldDisplayAsFraction(gatherMode);
const rating = displayAsFraction ?
Util.calculateRating(numPassed / numAudits) :
Util.calculateRating(category.score);

return (
<div className="SummaryTooltip">
<div className="SummaryTooltip__title">{GATHER_MODE_LABELS[gatherMode]}</div>
<Separator/>
<div className="SummaryTooltip__category">
<div className="SummaryTooltip__category-title">
{category.title}
</div>
{
totalWeight !== 0 &&
<div
className={`SummaryTooltip__rating SummaryTooltip__rating--${rating}`}
data-testid="SummaryTooltip__rating"
>
<span>{RATING_LABELS[rating]}</span>
{
!displayAsFraction && category.score && <>
<span> · </span>
<span>{category.score * 100}</span>
</>
}
</div>
}
</div>
<div className="SummaryTooltip__fraction">
{`${numPassed} audits passed / ${numAudits} audits run`}
</div>
</div>
);
};

export const SummaryCategory: FunctionComponent<{
category: LH.ReportResult.Category|undefined,
href: string,
gatherMode: LH.Result.GatherMode,
}> = ({category, href, gatherMode}) => {
return (
<div className="SummaryCategory">
{
category ?
<div className="SummaryCategory__content">
<CategoryScore
category={category}
href={href}
gatherMode={gatherMode}
/>
<SummaryTooltip category={category} gatherMode={gatherMode}/>
</div> :
<div
className="SummaryCategory__null"
data-testid="SummaryCategory__null"
/>
}
</div>
);
};
25 changes: 1 addition & 24 deletions flow-report/src/summary/summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {useMemo} from 'preact/hooks';
import {FlowSegment, Separator} from '../common';
import {getScreenDimensions, getScreenshot, useFlowResult} from '../util';
import {Util} from '../../../report/renderer/util';
import {CategoryScore} from '../wrappers/category-score';
import {SummaryCategory} from './category';

const DISPLAYED_CATEGORIES = ['performance', 'accessibility', 'best-practices', 'seo'];
const THUMBNAIL_WIDTH = 50;
Expand All @@ -33,29 +33,6 @@ const SummaryNavigationHeader: FunctionComponent<{url: string}> = ({url}) => {
);
};

const SummaryCategory: FunctionComponent<{
category: LH.ReportResult.Category|undefined,
href: string,
gatherMode: LH.Result.GatherMode,
}> = ({category, href, gatherMode}) => {
return (
<div className="SummaryCategory">
{
category ?
<CategoryScore
category={category}
href={href}
gatherMode={gatherMode}
/> :
<div
className="SummaryCategory__null"
data-testid="SummaryCategory__null"
/>
}
</div>
);
};

/**
* The div should behave like a JSX <>...</>. This still allows us to identify "rows" with CSS selectors.
*/
Expand Down
68 changes: 68 additions & 0 deletions flow-report/test/summary/category-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import fs from 'fs';
import {dirname} from 'path';
import {fileURLToPath} from 'url';

import {render} from '@testing-library/preact';

import {SummaryTooltip} from '../../src/summary/category';
import {Util} from '../../../report/renderer/util';

const flowResult: LH.FlowResult = JSON.parse(
fs.readFileSync(
// eslint-disable-next-line max-len
`${dirname(fileURLToPath(import.meta.url))}/../../../lighthouse-core/test/fixtures/fraggle-rock/reports/sample-lhrs.json`,
'utf-8'
)
);

describe('SummaryTooltip', () => {
it('renders tooltip with rating', async () => {
// Snapshot SEO
const reportResult = Util.prepareReportResult(flowResult.steps[2].lhr);
const category = reportResult.categories['seo'];

const root = render(
<SummaryTooltip category={category} gatherMode={reportResult.gatherMode}/>
);

const rating = root.getByTestId('SummaryTooltip__rating');
expect(rating.classList).toContain('SummaryTooltip__rating--average');

expect(root.getByText('9 audits passed / 11 audits run')).toBeTruthy();
});

it('renders tooltip without rating', async () => {
// Snapshot performance
const reportResult = Util.prepareReportResult(flowResult.steps[2].lhr);
const category = reportResult.categories['performance'];

const root = render(
<SummaryTooltip category={category} gatherMode={reportResult.gatherMode}/>
);

expect(() => root.getByTestId('SummaryTooltip__rating')).toThrow();
expect(root.getByText('2 audits passed / 4 audits run')).toBeTruthy();
});

it('renders scored category tooltip with score', async () => {
// Navigation performance
const reportResult = Util.prepareReportResult(flowResult.steps[0].lhr);
const category = reportResult.categories['performance'];

const root = render(
<SummaryTooltip category={category} gatherMode={reportResult.gatherMode}/>
);

const rating = root.getByTestId('SummaryTooltip__rating');
expect(rating.classList).toContain('SummaryTooltip__rating--pass');
expect(rating.textContent).toEqual('Good · 94');

expect(root.getByText('41 audits passed / 58 audits run')).toBeTruthy();
});
});
22 changes: 22 additions & 0 deletions lighthouse-core/util-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,28 @@ class Util {
static isPluginCategory(categoryId) {
return categoryId.startsWith('lighthouse-plugin-');
}

/**
* @param {LH.Result.GatherMode} gatherMode
*/
static shouldDisplayAsFraction(gatherMode) {
return gatherMode === 'timespan' || gatherMode === 'snapshot';
}

/**
* @param {LH.ReportResult.Category} category
*/
static calculateCategoryFraction(category) {
const numAudits = category.auditRefs.length;

let numPassed = 0;
let totalWeight = 0;
for (const auditRef of category.auditRefs) {
totalWeight += auditRef.weight;
if (Util.showAsPassed(auditRef.result)) numPassed++;
}
return {numPassed, numAudits, totalWeight};
}
}

/**
Expand Down
11 changes: 2 additions & 9 deletions report/renderer/category-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export class CategoryRenderer {
* @return {DocumentFragment}
*/
renderCategoryScore(category, groupDefinitions, options) {
if (options && (options.gatherMode === 'snapshot' || options.gatherMode === 'timespan')) {
if (options && Util.shouldDisplayAsFraction(options.gatherMode)) {
return this.renderCategoryFraction(category);
}
return this.renderScoreGauge(category, groupDefinitions);
Expand Down Expand Up @@ -376,14 +376,7 @@ export class CategoryRenderer {
const wrapper = this.dom.find('a.lh-fraction__wrapper', tmpl);
this.dom.safelySetHref(wrapper, `#${category.id}`);

const numAudits = category.auditRefs.length;

let numPassed = 0;
let totalWeight = 0;
for (const auditRef of category.auditRefs) {
totalWeight += auditRef.weight;
if (Util.showAsPassed(auditRef.result)) numPassed++;
}
const {numPassed, numAudits, totalWeight} = Util.calculateCategoryFraction(category);

const fraction = numPassed / numAudits;
const content = this.dom.find('.lh-fraction__content', tmpl);
Expand Down
22 changes: 22 additions & 0 deletions report/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,28 @@ export class Util {
static isPluginCategory(categoryId) {
return categoryId.startsWith('lighthouse-plugin-');
}

/**
* @param {LH.Result.GatherMode} gatherMode
*/
static shouldDisplayAsFraction(gatherMode) {
return gatherMode === 'timespan' || gatherMode === 'snapshot';
}

/**
* @param {LH.ReportResult.Category} category
*/
static calculateCategoryFraction(category) {
const numAudits = category.auditRefs.length;

let numPassed = 0;
let totalWeight = 0;
for (const auditRef of category.auditRefs) {
totalWeight += auditRef.weight;
if (Util.showAsPassed(auditRef.result)) numPassed++;
}
return {numPassed, numAudits, totalWeight};
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions report/test/renderer/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,30 @@ describe('util helpers', () => {
]);
});
});

describe('#shouldDisplayAsFraction', () => {
it('returns true for timespan and snapshot', () => {
expect(Util.shouldDisplayAsFraction('navigation')).toEqual(false);
expect(Util.shouldDisplayAsFraction('timespan')).toEqual(true);
expect(Util.shouldDisplayAsFraction('snapshot')).toEqual(true);
expect(Util.shouldDisplayAsFraction(undefined)).toEqual(false);
});
});

describe('#calculateCategoryFraction', () => {
it('returns passed audits and total audits', () => {
const category = {
auditRefs: [
{weight: 3, result: {score: 1, scoreDisplayMode: 'binary'}},
{weight: 2, result: {score: 1, scoreDisplayMode: 'binary'}},
{weight: 0, result: {score: 1, scoreDisplayMode: 'binary'}},
{weight: 1, result: {score: 0, scoreDisplayMode: 'binary'}},
],
};
const {numPassed, numAudits, totalWeight} = Util.calculateCategoryFraction(category);
expect(numPassed).toEqual(3);
expect(numAudits).toEqual(4);
expect(totalWeight).toEqual(6);
});
});
});

0 comments on commit 54fff07

Please sign in to comment.