Skip to content

Commit

Permalink
#4 - test: reduce flakyness of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvandescheur committed Jan 15, 2024
1 parent a444195 commit 6b7c5bd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
27 changes: 27 additions & 0 deletions .storybook/test-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { getStoryContext } = require("@storybook/test-runner");
const { MINIMAL_VIEWPORTS } = require("@storybook/addon-viewport");

const DEFAULT_VP_SIZE = { width: 1280, height: 720 };

module.exports = {
/**
* This makes sure the test runner respects the viewport.
* @see {@link https://github.com/storybookjs/test-runner/issues/85#issuecomment-1576465128|[Bug] Tests always run in the default viewport}
* @param page
* @param story
*/
async preRender(page, story) {
const context = await getStoryContext(page, story);
const vpName =
context.parameters?.viewport?.defaultViewport ?? "responsive";
const vpParams = MINIMAL_VIEWPORTS[vpName];

if (vpParams) {
const width = parseInt(vpParams.styles.width);
const height = parseInt(vpParams.styles.height);
page.setViewportSize({ width, height });
} else {
page.setViewportSize(DEFAULT_VP_SIZE);
}
},
};
20 changes: 10 additions & 10 deletions src/components/dropdown/dropdown.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from "@storybook/react";
import { expect, userEvent, within } from "@storybook/test";
import { expect, userEvent, waitFor, within } from "@storybook/test";
import React from "react";

import { Button, ButtonLink } from "../button";
Expand All @@ -19,11 +19,11 @@ const meta = {

// Click opens, escape closes.
await userEvent.click(button, { delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();
userEvent.keyboard("{Escape}");
expect(await canvas.findByRole("dialog")).not.toBeVisible();
await waitFor(() => expect(canvas.queryByRole("dialog")).toBeNull());
await userEvent.click(button, { delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();

// Tab focuses items.
await userEvent.tab({ delay: 10 });
Expand Down Expand Up @@ -102,11 +102,11 @@ export const ActivateOnHover: Story = {

// Click opens, escape closes.
await userEvent.hover(button, { delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();
userEvent.keyboard("{Escape}");
expect(await canvas.findByRole("dialog")).not.toBeVisible();
await waitFor(() => expect(canvas.queryByRole("dialog")).toBeNull());
await userEvent.hover(button, { delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();

// Tab focuses items.
await userEvent.tab({ delay: 10 });
Expand All @@ -125,12 +125,12 @@ export const ActivateOnFocus: Story = {

// Click opens, escape closes.
await userEvent.tab({ delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();
userEvent.keyboard("{Escape}");
expect(await canvas.findByRole("dialog")).not.toBeVisible();
await waitFor(() => expect(canvas.queryByRole("dialog")).toBeNull());
await userEvent.tab({ shift: true, delay: 10 });
await userEvent.tab({ delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();

// Tab focuses items.
await userEvent.tab({ delay: 10 });
Expand Down
10 changes: 5 additions & 5 deletions src/components/navbar/navbar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from "@storybook/react";
import { expect, userEvent, within } from "@storybook/test";
import { expect, userEvent, waitFor, within } from "@storybook/test";
import React from "react";

import { Button, ButtonLink } from "../button";
Expand Down Expand Up @@ -57,11 +57,11 @@ export const NavbarOnMobile: Story = {

// Click opens, escape closes.
await userEvent.click(button, { delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
userEvent.keyboard("{Escape}");
expect(await canvas.findByRole("dialog")).not.toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();
userEvent.keyboard("{Escape}", { delay: 10 });
await waitFor(() => expect(canvas.queryByRole("dialog")).toBeNull());
await userEvent.click(button, { delay: 10 });
expect(await canvas.findByRole("dialog")).toBeVisible();
await expect(canvas.getByRole("dialog")).toBeVisible();

// Tab focuses items.
await userEvent.tab({ delay: 10 });
Expand Down
3 changes: 1 addition & 2 deletions src/components/navbar/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export type NavbarProps = ToolbarProps;
*/
export const Navbar: React.FC<NavbarProps> = ({ children, ...props }) => {
const [isMobile, setIsMobile] = useState(
window?.matchMedia("(max-width: 767px)").matches,
window?.matchMedia("(max-width: 767px)").matches ?? true,
);

/**
* Updates `isMobile` on resize.
*/
Expand Down

0 comments on commit 6b7c5bd

Please sign in to comment.