Skip to content

Commit

Permalink
Add onClick handler for story statuses to open VTA panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Aug 20, 2024
1 parent 0f06c8e commit e5021bd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/screens/VisualTests/VisualTests.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ export const Pending = {
expect(args.updateBuildStatus).toHaveBeenCalledWith({
"button--primary": {
status: "warn",
onClick: expect.anything(),
title: "Visual Tests",
description: "Chromatic Visual Tests",
},
Expand Down
3 changes: 2 additions & 1 deletion src/screens/VisualTests/VisualTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ export const VisualTestsWithoutSelectedBuildId = ({
"testsForStatus" in lastBuildOnBranch &&
lastBuildOnBranch.testsForStatus?.nodes &&
getFragment(FragmentStatusTestFields, lastBuildOnBranch.testsForStatus.nodes);
const statusUpdate = lastBuildOnBranchIsSelectable && testsToStatusUpdate(testsForStatus || []);
const statusUpdate =
lastBuildOnBranchIsSelectable && testsToStatusUpdate(managerApi, testsForStatus || []);
useEffect(() => {
updateBuildStatus((state) => ({
...createEmptyStoryStatusUpdate(state),
Expand Down
23 changes: 17 additions & 6 deletions src/utils/testsToStatusUpdate.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { expect, it } from "vitest";
import type { API } from "@storybook/manager-api";
import { expect, it, vi } from "vitest";

import { TestStatus } from "../gql/graphql";
import { testsToStatusUpdate } from "./testsToStatusUpdate";

const api: API = {
setSelectedPanel: vi.fn(),
togglePanel: vi.fn(),
} as any;

it("handles single test with no changes", () => {
expect(
testsToStatusUpdate([
testsToStatusUpdate(api, [
{
id: "1",
status: TestStatus.Passed,
Expand All @@ -23,7 +29,7 @@ it("handles single test with no changes", () => {

it("handles single test with changes", () => {
expect(
testsToStatusUpdate([
testsToStatusUpdate(api, [
{
id: "1",
status: TestStatus.Pending,
Expand All @@ -36,6 +42,7 @@ it("handles single test with changes", () => {
{
"story--id": {
"description": "Chromatic Visual Tests",
"onClick": [Function],
"status": "warn",
"title": "Visual Tests",
},
Expand All @@ -45,7 +52,7 @@ it("handles single test with changes", () => {

it("handles multiple tests", () => {
expect(
testsToStatusUpdate([
testsToStatusUpdate(api, [
{
id: "1",
status: TestStatus.Pending,
Expand All @@ -65,11 +72,13 @@ it("handles multiple tests", () => {
{
"story--id": {
"description": "Chromatic Visual Tests",
"onClick": [Function],
"status": "warn",
"title": "Visual Tests",
},
"story2--id": {
"description": "Chromatic Visual Tests",
"onClick": [Function],
"status": "error",
"title": "Visual Tests",
},
Expand All @@ -79,7 +88,7 @@ it("handles multiple tests", () => {

it("handles multiple viewports", () => {
expect(
testsToStatusUpdate([
testsToStatusUpdate(api, [
{
id: "1",
status: TestStatus.Broken,
Expand All @@ -99,6 +108,7 @@ it("handles multiple viewports", () => {
{
"story--id": {
"description": "Chromatic Visual Tests",
"onClick": [Function],
"status": "error",
"title": "Visual Tests",
},
Expand All @@ -108,7 +118,7 @@ it("handles multiple viewports", () => {

it("handles multiple viewports, reverse order", () => {
expect(
testsToStatusUpdate([
testsToStatusUpdate(api, [
{
id: "1",
status: TestStatus.Pending,
Expand All @@ -128,6 +138,7 @@ it("handles multiple viewports, reverse order", () => {
{
"story--id": {
"description": "Chromatic Visual Tests",
"onClick": [Function],
"status": "error",
"title": "Visual Tests",
},
Expand Down
8 changes: 8 additions & 0 deletions src/utils/testsToStatusUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { API } from "@storybook/manager-api";
import type { API_StatusUpdate, API_StatusValue, StoryId } from "@storybook/types";

import { PANEL_ID } from "../constants";
import { StatusTestFieldsFragment, TestStatus } from "../gql/graphql";

export const statusMap: Partial<Record<TestStatus, API_StatusValue>> = {
Expand All @@ -22,6 +24,7 @@ function chooseWorseStatus(status: API_StatusValue | null, oldStatus: API_Status
}

export function testsToStatusUpdate<T extends StatusTestFieldsFragment>(
api: API,
tests: readonly T[]
): API_StatusUpdate {
const storyIdToStatus: Record<StoryId, API_StatusValue | null> = {};
Expand All @@ -34,13 +37,18 @@ export function testsToStatusUpdate<T extends StatusTestFieldsFragment>(
storyIdToStatus[test.story.storyId]
);
});
const openAddonPanel = () => {
api.setSelectedPanel(PANEL_ID);
api.togglePanel(true);
};
const update = Object.fromEntries(
Object.entries(storyIdToStatus).map(([storyId, status]) => [
storyId,
status && {
status,
title: "Visual Tests",
description: "Chromatic Visual Tests",
onClick: openAddonPanel,
},
])
);
Expand Down

0 comments on commit e5021bd

Please sign in to comment.