Skip to content

Commit cbead49

Browse files
committed
test(dialog): add play functions
1 parent 7aa8700 commit cbead49

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { useState } from "react";
2+
import { StoryObj } from "@storybook/react";
3+
import { userEvent, within, expect, waitFor } from "@storybook/test";
4+
import Dialog from ".";
5+
import Button from "../button";
6+
import { allowInteractions } from "../../../.storybook/interaction-toggle/reduced-motion";
7+
8+
type Story = StoryObj<typeof Dialog>;
9+
10+
export default {
11+
title: "Dialog/Interactions",
12+
component: Dialog,
13+
parameters: {
14+
layout: "padded",
15+
chromatic: { disableSnapshot: true },
16+
},
17+
decorators: [],
18+
};
19+
20+
export const FocusManagement: Story = {
21+
render: function FocusManagementRender() {
22+
const [isOpen, setIsOpen] = useState(false);
23+
return (
24+
<>
25+
<Button onClick={() => setIsOpen(true)} data-role="open-dialog-btn">
26+
Open Dialog
27+
</Button>
28+
<Dialog
29+
open={isOpen}
30+
title="Title"
31+
subtitle="Subtitle"
32+
onCancel={() => setIsOpen(false)}
33+
showCloseIcon
34+
>
35+
<p>Content</p>
36+
</Dialog>
37+
</>
38+
);
39+
},
40+
41+
play: async ({ canvasElement }) => {
42+
if (!allowInteractions()) return;
43+
const canvas = within(canvasElement);
44+
const portal = within(canvasElement.ownerDocument.body);
45+
46+
const openButton = canvas.getByRole("button", { name: /open dialog/i });
47+
48+
openButton.focus();
49+
await waitFor(() => expect(openButton).toHaveFocus());
50+
51+
await userEvent.click(openButton);
52+
53+
const closeButton = await portal.findByRole("button", { name: /close/i });
54+
await waitFor(() => expect(closeButton).toBeVisible());
55+
56+
await userEvent.tab();
57+
await expect(closeButton).toHaveFocus();
58+
},
59+
};
60+
61+
FocusManagement.storyName = "Focus Management";
62+
FocusManagement.parameters = {
63+
themeProvider: {
64+
chromatic: { theme: "sage" },
65+
},
66+
};

0 commit comments

Comments
 (0)