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

Update selectedTest whenever tests change (e.g. when switching stories) #51

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/ProjectItem-Chrome-1200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/ProjectItem-Chrome-320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/ProjectItem-Chrome-600.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/ProjectItem-Safari-1200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/ProjectItem-Safari-320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/ProjectItem-Safari-600.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/components/BrowserSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { StatusDot, StatusDotWrapper } from "./StatusDot";
import { TooltipMenu } from "./TooltipMenu";

const browserIcons = {
[Browser.Chrome]: <ChromeIcon alt="Chrome" />,
[Browser.Firefox]: <FirefoxIcon alt="Firefox" />,
[Browser.Safari]: <SafariIcon alt="Safari" />,
[Browser.Edge]: <EdgeIcon alt="Edge" />,
[Browser.Chrome]: <ChromeIcon alt="Chrome" aria-label="Chrome" />,
[Browser.Firefox]: <FirefoxIcon alt="Firefox" aria-label="Firefox" />,
[Browser.Safari]: <SafariIcon alt="Safari" aria-label="Safari" />,
[Browser.Edge]: <EdgeIcon alt="Edge" aria-label="Edge" />,
} as const;

type BrowserData = Pick<BrowserInfo, "id" | "key" | "name">;
Expand Down
2 changes: 0 additions & 2 deletions src/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,11 @@ export type ProjectBranchNamesArgs = {

export type ProjectLastBuildArgs = {
branches?: InputMaybe<Array<Scalars['String']['input']>>;
commit?: InputMaybe<Scalars['String']['input']>;
defaultBranch?: InputMaybe<Scalars['Boolean']['input']>;
localBuilds?: InputMaybe<LocalBuildsSpecifierInput>;
results?: InputMaybe<Array<BuildResult>>;
slug?: InputMaybe<Scalars['String']['input']>;
statuses?: InputMaybe<Array<BuildStatus>>;
uncommittedHash?: InputMaybe<Scalars['String']['input']>;
};

export type PublicAccountInfo = {
Expand Down
56 changes: 56 additions & 0 deletions src/screens/VisualTests/SnapshotComparison.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { action } from "@storybook/addon-actions";
import { useState } from "@storybook/preview-api";
import type { Meta, StoryObj } from "@storybook/react";
import { screen, userEvent, within } from "@storybook/testing-library";
import React, { ComponentProps } from "react";

import { Browser, ComparisonResult, TestStatus } from "../../gql/graphql";
import { playAll } from "../../utils/playAll";
import { makeTest, makeTests } from "../../utils/storyData";
import { SnapshotComparison } from "./SnapshotComparison";

Expand Down Expand Up @@ -89,3 +93,55 @@ export const WithSingleTestShowingBaseline: Story = {
baselineImageVisible: true,
},
};

export const SwitchingViewport: Story = {
args: {
tests: makeTests({
browsers: [Browser.Chrome, Browser.Safari],
viewports: [
{ status: TestStatus.Passed, viewport: 320 },
{ status: TestStatus.Passed, viewport: 600 },
{ status: TestStatus.Passed, viewport: 1200 },
],
}).map((test) => ({
...test,
comparisons: test.comparisons.map((comparison) => ({
...comparison,
headCapture: {
...comparison.headCapture,
captureImage: {
imageUrl: `/ProjectItem-${comparison.browser.name}-${comparison.viewport.width}.png`,
imageWidth: comparison.viewport.width,
},
},
})),
})),
},
play: playAll(async ({ canvasElement, canvasIndex }) => {
const canvas = within(canvasElement);
const menu = await canvas.findByRole("button", { name: "320px" });
await userEvent.click(menu);
const items = await screen.findAllByText("1200px");
await userEvent.click(items[canvasIndex]);
}),
};

export const SwitchingBrowser: Story = {
args: SwitchingViewport.args,
play: playAll(async ({ canvasElement, canvasIndex }) => {
const canvas = within(canvasElement);
const menu = await canvas.findByRole("button", { name: "Chrome" });
await userEvent.click(menu);
const items = await screen.findAllByText("Safari");
await userEvent.click(items[canvasIndex]);
}),
};

export const SwitchingTests: Story = {
args: SwitchingViewport.args,
render: function RenderSwitchingTests({ ...props }: ComponentProps<typeof SnapshotComparison>) {
const [tests, setTests] = React.useState(null);
if (!tests) setTimeout(() => setTests([makeTest({})]), 0);
return <SnapshotComparison {...props} tests={tests || props.tests} />;
},
};
4 changes: 3 additions & 1 deletion src/utils/playAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export function playAll<Story extends StoryObj>(
await Promise.all(
sequence.flatMap((storyOrPlay) =>
typeof storyOrPlay === "function"
? canvasElements.map((canvasElement) => storyOrPlay({ ...context, canvasElement }))
? canvasElements.map((canvasElement, canvasIndex) =>
storyOrPlay({ ...context, canvasElement, canvasIndex })
)
: storyOrPlay?.play?.(context)
)
);
Expand Down
91 changes: 0 additions & 91 deletions src/utils/useTests.test.ts

This file was deleted.

36 changes: 14 additions & 22 deletions src/utils/useTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,23 @@ type BrowserData = Pick<BrowserInfo, "id" | "key" | "name">;
* for the same story.
*/
export function useTests(tests: StoryTestFieldsFragment[]) {
const [selectedTest, onSelectTest] = useState<StoryTestFieldsFragment>(tests[0]);
const [selectedBrowserId, onSelectBrowserId] = useState<BrowserInfo["id"]>(
selectedTest.comparisons[0].browser.id
const [selectedBrowserId, onSelectBrowserId] = useState<BrowserData["id"]>(
tests[0].comparisons[0].browser.id
);
const selectedComparison = selectedTest.comparisons.find(
(comparison) => comparison.browser.id === selectedBrowserId
const [selectedViewportId, onSelectViewportId] = useState<ViewportData["id"]>(
tests[0].comparisons[0].viewport.id
);
if (!selectedComparison) {
throw new Error(`No comparison found with browser id ${selectedBrowserId}`);
}

const onSelectViewport = useCallback(
(viewport: ViewportData) => {
const newSelectedTest = tests.find(
({ parameters }) => parameters.viewport.id === viewport.id
);
if (!newSelectedTest) throw new Error(`No test found with viewport ${viewport.id}`);
onSelectTest(newSelectedTest);
},
[tests]
);
const onSelectBrowser = useCallback(
(browser: BrowserData) => onSelectBrowserId(browser.id),
[onSelectBrowserId]
);
const onSelectBrowser = useCallback(({ id }: BrowserData) => onSelectBrowserId(id), []);
const onSelectViewport = useCallback(({ id }: ViewportData) => onSelectViewportId(id), []);

const selectedTest =
tests.find(({ parameters }) => parameters.viewport.id === selectedViewportId) || tests[0];

const selectedComparison =
selectedTest.comparisons.find(({ browser }) => browser.id === selectedBrowserId) ||
selectedTest.comparisons[0];

return {
selectedTest,
selectedComparison,
Expand Down