Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon Test: Make component tests status row link to the story's tests panel #29992

Merged
merged 7 commits into from
Dec 10, 2024
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 @@ -145,6 +147,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 @@ -244,6 +252,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 @@ -278,6 +296,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);
};
ghengeveld marked this conversation as resolved.
Show resolved Hide resolved
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
Loading