Skip to content

Commit

Permalink
Merge pull request #29992 from storybookjs/link-to-tests-panel
Browse files Browse the repository at this point in the history
Addon Test: Make component tests status row link to the story's tests panel
  • Loading branch information
valentinpalkovic authored Dec 10, 2024
2 parents 2c17aef + a1082cd commit 7389774
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
38 changes: 35 additions & 3 deletions code/addons/test/src/components/TestProviderRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
import { isEqual } from 'es-toolkit';
import { debounce } from 'es-toolkit/compat';

// Relatively importing from a11y to get the ADDON_ID
import { ADDON_ID as A11Y_ADDON_ID } from '../../../a11y/src/constants';
import { type Config, type Details } from '../constants';
import {
ADDON_ID as A11Y_ADDON_ID,
PANEL_ID as A11y_ADDON_PANEL_ID,
} from '../../../a11y/src/constants';
import { type Config, type Details, PANEL_ID } from '../constants';
import { type TestStatus } from '../node/reporter';
import { Description } from './Description';
import { TestStatusIcon } from './TestStatusIcon';
Expand Down Expand Up @@ -153,6 +155,12 @@ export const TestProviderRender: FC<

const status = (state.failed ? 'failed' : results[0]?.status) || 'unknown';

const openPanel = (id: string, panelId: string) => {
api.selectStory(id);
api.setSelectedPanel(panelId);
api.togglePanel(true);
};

return (
<Container {...props}>
<Heading>
Expand Down Expand Up @@ -254,6 +262,16 @@ export const TestProviderRender: FC<
<Extras>
<ListItem
title="Component tests"
onClick={
(status === 'failed' || status === 'warning') && results.length
? () => {
const firstNotPassed = results.find(
(r) => r.status === 'failed' || r.status === 'warning'
);
openPanel(firstNotPassed.storyId, PANEL_ID);
}
: null
}
icon={
state.crashed ? (
<TestStatusIcon status="critical" aria-label="status: crashed" />
Expand Down Expand Up @@ -288,6 +306,20 @@ export const TestProviderRender: FC<
{isA11yAddon && (
<ListItem
title="Accessibility"
onClick={
(a11yStatus === 'negative' || a11yStatus === 'warning') && a11yResults.length
? () => {
const firstNotPassed = results.find((r) =>
r.reports
.filter((report) => report.type === 'a11y')
.find(
(report) => report.status === 'failed' || report.status === 'warning'
)
);
openPanel(firstNotPassed.storyId, A11y_ADDON_PANEL_ID);
}
: null
}
icon={<TestStatusIcon status={a11yStatus} aria-label={`status: ${a11yStatus}`} />}
right={a11yNotPassedAmount || null}
/>
Expand Down
14 changes: 7 additions & 7 deletions code/addons/test/src/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const statusMap: Record<TestStatus, API_StatusValue> = {
addons.register(ADDON_ID, (api) => {
const storybookBuilder = (globalThis as any).STORYBOOK_BUILDER || '';
if (storybookBuilder.includes('vite')) {
const openAddonPanel = () => {
const openTestsPanel = () => {
api.setSelectedPanel(PANEL_ID);
api.togglePanel(true);
};
Expand Down Expand Up @@ -94,9 +94,9 @@ addons.register(ADDON_ID, (api) => {
? rest.failureMessages.join('\n')
: '',
data: { testRunId },
onClick: openAddonPanel,
onClick: openTestsPanel,
sidebarContextMenu: false,
} as API_StatusObject,
} satisfies API_StatusObject,
])
)
)
Expand All @@ -108,12 +108,12 @@ addons.register(ADDON_ID, (api) => {
update.details.testResults.flatMap((testResult) =>
testResult.results
.filter(({ storyId }) => storyId)
.map(({ storyId, status, testRunId, reports, ...rest }) => {
.map(({ storyId, testRunId, reports }) => {
const a11yReport = reports.find((r: any) => r.type === 'a11y');
return [
storyId,
a11yReport
? {
? ({
title: 'Accessibility tests',
description: '',
status: statusMap[a11yReport.status],
Expand All @@ -123,9 +123,9 @@ addons.register(ADDON_ID, (api) => {
api.togglePanel(true);
},
sidebarContextMenu: false,
}
} satisfies API_StatusObject)
: null,
] as const;
];
})
)
)
Expand Down
4 changes: 2 additions & 2 deletions code/addons/test/src/node/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type TestResultResult =
reports: Report[];
}
| {
status: Extract<TestStatus, 'failed'>;
status: Extract<TestStatus, 'failed' | 'warning'>;
storyId: string;
duration: number;
testRunId: string;
Expand All @@ -39,7 +39,7 @@ export type TestResult = {
results: TestResultResult[];
startTime: number;
endTime: number;
status: Extract<TestStatus, 'passed' | 'failed'>;
status: Extract<TestStatus, 'passed' | 'failed' | 'warning'>;
message?: string;
};

Expand Down

0 comments on commit 7389774

Please sign in to comment.