From 0030d1ef81fd19e57566fe83551d96c5a1648c09 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 3 Oct 2023 12:50:29 +1100 Subject: [PATCH] Ensure story shows we switch to latest build on new story --- .../VisualTests/VisualTests.stories.tsx | 133 +++++++++--------- 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/src/screens/VisualTests/VisualTests.stories.tsx b/src/screens/VisualTests/VisualTests.stories.tsx index 697a9a0b..1bfa0b4e 100644 --- a/src/screens/VisualTests/VisualTests.stories.tsx +++ b/src/screens/VisualTests/VisualTests.stories.tsx @@ -1,4 +1,4 @@ -import type { ResultOf } from "@graphql-typed-document-node/core"; +import type { ResultOf, VariablesOf } from "@graphql-typed-document-node/core"; import { action } from "@storybook/addon-actions"; import { expect } from "@storybook/jest"; import type { API, State } from "@storybook/manager-api"; @@ -64,18 +64,21 @@ const withManagerApi: Decorator = (Story, { argsByTarget }) => ( ); -const withGraphQLQuery = (...args: Parameters) => ({ +const withGraphQLQuery = >( + ...args: Parameters>> +) => ({ msw: { - handlers: [graphql.query(...args)], + handlers: [graphql.query>(...args)], }, }); function withGraphQLQueryResult>( query: TQuery, - result: ResultOf + result: (variables: VariablesOf) => ResultOf ) { const queryName = getOperationAST(query)?.name?.value; - if (queryName) return withGraphQLQuery(queryName, (req, res, ctx) => res(ctx.data(result))); + if (queryName) + return withGraphQLQuery(queryName, (req, res, ctx) => res(ctx.data(result(req.variables)))); throw new Error(`Couldn't determine query name from query`); } @@ -87,24 +90,30 @@ const withGraphQLMutation = (...args: Parameters) => ({ const withBuilds = ({ lastBuildOnBranch, - selectedBuild, + selectedBuild: getSelectedBuild, userCanReview = true, }: { - selectedBuild?: SelectedBuildFieldsFragment; + selectedBuild?: + | SelectedBuildFieldsFragment + | ((selectedBuildId: string | undefined) => SelectedBuildFieldsFragment); lastBuildOnBranch?: LastBuildOnBranchBuildFieldsFragment; userCanReview?: boolean; }) => { - return withGraphQLQueryResult(QueryBuild, { - project: { - name: "acme", - lastBuildOnBranch: lastBuildOnBranch || selectedBuild, - }, - selectedBuild, - viewer: { - projectMembership: { - userCanReview, + return withGraphQLQueryResult(QueryBuild, ({ selectedBuildId }) => { + const selectedBuild = + typeof getSelectedBuild === "function" ? getSelectedBuild(selectedBuildId) : getSelectedBuild; + return { + project: { + name: "acme", + lastBuildOnBranch: lastBuildOnBranch || selectedBuild, }, - }, + selectedBuild, + viewer: { + projectMembership: { + userCanReview, + }, + }, + }; }); }; @@ -155,7 +164,7 @@ type Story = StoryObj; export const Loading = { parameters: { ...withGraphQLQuery("AddonVisualTestsBuild", (req, res, ctx) => - res(ctx.status(200), ctx.data({}), ctx.delay("infinite")) + res(ctx.status(200), ctx.data({} as any), ctx.delay("infinite")) ), ...withFigmaDesign( "https://www.figma.com/file/GFEbCgCVDtbZhngULbw2gP/Visual-testing-in-Storybook?type=design&node-id=508-304933&t=0rxMQnkxsVpVj1qy-4" @@ -163,19 +172,6 @@ export const Loading = { }, } satisfies Story; -const pendingBuildNewStory = withTests( - { ...pendingBuild }, - pendingTests.map((test) => ({ - ...test, - result: TestResult.Added, - comparisons: test.comparisons.map((comparison) => ({ - ...comparison, - result: ComparisonResult.Added, - baseCapture: null, - })), - })) -); - const newStoryNoTests = withTests({ ...pendingBuild }, []); export const GraphQLError = { @@ -264,6 +260,40 @@ export const EmptyBranchLocalBuildCapturing = { }, } satisfies Story; +/** At this point, we should switch to the next build */ +export const EmptyBranchLocalBuildCapturedCurrentStory = { + parameters: { + ...withBuilds({ + selectedBuild: undefined, + lastBuildOnBranch: withTests(pendingBuild, pendingTests), + }), + }, + args: { + localBuildProgress: { + ...EmptyBranchLocalBuildCapturing.args.localBuildProgress, + buildProgressPercentage: 90, + stepProgress: { + ...EmptyBranchLocalBuildCapturing.args.localBuildProgress.stepProgress, + snapshot: { + ...EmptyBranchLocalBuildCapturing.args.localBuildProgress.stepProgress.snapshot, + numerator: 310, + }, + }, + }, + }, +} satisfies Story; + +/** Complete builds should always be switched to */ +export const EmptyBranchCIBuildPending = { + parameters: { + ...withBuilds({ + selectedBuild: undefined, + lastBuildOnBranch: withTests(pendingBuild, pendingTests), + }), + }, + // In theory we might have a complete running build here, it should behave the same either way +} satisfies Story; + // There is a build, but this story is new (not on the build at all) export const StoryAddedNotInBuild: Story = { parameters: { @@ -327,8 +357,11 @@ export const StoryAddedInSelectedBuild: Story = { export const StoryAddedInLastBuildOnBranchNotInSelected: Story = { parameters: { ...withBuilds({ - selectedBuild: withTests(pendingBuild, []), - lastBuildOnBranch: withTests(pendingBuild, pendingTestsNewStory), + selectedBuild: (selectedBuildId) => + selectedBuildId === "2" + ? withTests({ ...pendingBuild, id: "2" }, pendingTestsNewStory) + : withTests(pendingBuild, []), + lastBuildOnBranch: withTests({ ...pendingBuild, id: "2" }, pendingTestsNewStory), }), }, }; @@ -418,40 +451,6 @@ export const BrowserAddedAndAccepted: Story = { }, }; -/** At this point, we should switch to the next build */ -export const EmptyBranchLocalBuildCapturedCurrentStory = { - parameters: { - ...withBuilds({ - selectedBuild: undefined, - lastBuildOnBranch: withTests(pendingBuild, pendingTests), - }), - }, - args: { - localBuildProgress: { - ...EmptyBranchLocalBuildCapturing.args.localBuildProgress, - buildProgressPercentage: 90, - stepProgress: { - ...EmptyBranchLocalBuildCapturing.args.localBuildProgress.stepProgress, - snapshot: { - ...EmptyBranchLocalBuildCapturing.args.localBuildProgress.stepProgress.snapshot, - numerator: 310, - }, - }, - }, - }, -} satisfies Story; - -/** Complete builds should always be switched to */ -export const EmptyBranchCIBuildPending = { - parameters: { - ...withBuilds({ - selectedBuild: undefined, - lastBuildOnBranch: withTests(pendingBuild, pendingTests), - }), - }, - // In theory we might have a complete running build here, it should behave the same either way -} satisfies Story; - export const NoChanges = { parameters: { ...withBuilds({