-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
report(flow): category tooltips (#13043)
- Loading branch information
Showing
8 changed files
with
285 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters