Skip to content

Commit

Permalink
feat(utils): use table.title as heading when formatting audit details
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed May 21, 2024
1 parent 5b29ebd commit 3814b3d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ exports[`auditDetails > should render complete details section 1`] = `
"<details>
<summary>🟩 <b>190ms</b> (score: 99)</summary>
#### Additional Information
|Class Names|Element|
|:--:|:--:|
|.btn, .icon|button|
Expand Down Expand Up @@ -106,8 +104,6 @@ exports[`generateMdReport > should render complete md report 1`] = `
<details>
<summary>🟨 <b>2,7 s</b> (score: 67)</summary>
#### Additional Information
|Phase|% of LCP|Timing|
|:--:|:--|--:|
|TTFB|27%|620 ms|
Expand Down
28 changes: 11 additions & 17 deletions packages/utils/src/lib/reports/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@ import { Hierarchy, NEW_LINE, SPACE, md } from '../text-formats';
const { headline, lines, link, section, table } = md;

export function tableSection(
tableData: Table | undefined,
options?:
| {
heading?: string;
level?: Hierarchy | 0;
}
| string,
): string {
if (tableData == null) {
return '';
}
tableData: Table,
options?: {
level?: Hierarchy | 0;
},
) {
if (tableData.rows.length === 0) {
return '';
}
const { heading, level = 4 } =
typeof options === 'string'
? { heading: options, level: 0 }
: options ?? {};
const { level = 4 } = options ?? {};
// if hierarchy is 0 do not apply heading styles
const render = (h: string, l: Hierarchy | 0) =>
l === 0 ? heading : headline(h, l);
return lines(heading ? render(heading, level) : false, table(tableData));
l === 0 ? h : headline(h, l);
return lines(
tableData.title && render(tableData.title, level),
table(tableData),
);
}

// @TODO extract `Pick<AuditReport, 'docsUrl' | 'description'>` to a reusable schema and type
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/lib/reports/formatting.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('tableSection', () => {
expect(
tableSection(
{
title: 'LCP Breakdown',
columns: [
{ key: 'phase', label: 'Phase' },
{ key: 'percentageLcp', label: '% of LCP', align: 'left' },
Expand Down Expand Up @@ -35,7 +36,7 @@ describe('tableSection', () => {
},
],
},
{ heading: 'LCP Breakdown', level: 3 },
{ level: 3 },
),
).toMatchSnapshot();
});
Expand Down
8 changes: 3 additions & 5 deletions packages/utils/src/lib/reports/generate-md-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function auditDetailsIssues(issues: Issue[] = []) {
return '';
}
const detailsTableData = {
title: 'Issues',
columns: issuesTableHeadings,
rows: issues.map(
({ severity: severityVal, message, source: sourceVal }: Issue) => {
Expand All @@ -73,7 +74,7 @@ export function auditDetailsIssues(issues: Issue[] = []) {
),
};

return tableSection(detailsTableData, { heading: 'Issues' });
return tableSection(detailsTableData);
}

export function auditDetails(audit: AuditReport) {
Expand All @@ -85,10 +86,7 @@ export function auditDetails(audit: AuditReport) {
return section(detailsValue);
}

const tableSectionContent =
table == null
? ''
: tableSection(table, { heading: 'Additional Information' });
const tableSectionContent = table == null ? '' : tableSection(table);
const issuesSectionContent =
issues.length > 0 ? auditDetailsIssues(issues) : '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ describe('auditDetails', () => {
displayValue: '190ms',
details: {
table: {
title: 'Elements',
rows: [
{
element: 'button',
Expand All @@ -269,7 +270,7 @@ describe('auditDetails', () => {
},
} as AuditReport);
expect(md).toMatch('<details>');
expect(md).toMatch('#### Additional Information');
expect(md).toMatch('#### Elements');
expect(md).toMatch('|button|');
expect(md).toMatch('|div|');
expect(md).not.toMatch('#### Issues');
Expand Down Expand Up @@ -364,7 +365,6 @@ describe('auditsSection', () => {
} as unknown as ScoredReport);
expect(md).toMatch('#### Issues');
expect(md).toMatch('|Severity|Message|Source file|Line(s)|');
expect(md).toMatch('#### Additional Information');
expect(md).toMatch('|value|');
});

Expand Down

0 comments on commit 3814b3d

Please sign in to comment.