|
| 1 | +import { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { Button } from "@/lib/components/ui/button"; |
| 3 | +import { Input } from "@/lib/components/ui/input"; |
| 4 | +import { Label } from "@/lib/components/ui/label"; |
| 5 | +import { expect, userEvent, within, waitFor } from "@storybook/test"; |
| 6 | + |
| 7 | +/** Link to original docs https://ui.shadcn.com/docs/components/input */ |
| 8 | +const meta = { |
| 9 | + component: Input, |
| 10 | + title: "Components/shadcn/Input", |
| 11 | +} satisfies Meta<typeof Input>; |
| 12 | + |
| 13 | +export default meta; |
| 14 | + |
| 15 | +type Story = StoryObj<typeof Input>; |
| 16 | + |
| 17 | +export const Default: Story = { |
| 18 | + render() { |
| 19 | + return ( |
| 20 | + <div className="flex w-full justify-center"> |
| 21 | + <Input type="email" placeholder="Email" /> |
| 22 | + </div> |
| 23 | + ); |
| 24 | + }, |
| 25 | + play: async ({ canvasElement, step }) => { |
| 26 | + const canvas = within(canvasElement); |
| 27 | + const input = canvas.getByPlaceholderText("Email"); |
| 28 | + await step("Click on input", async () => { |
| 29 | + await userEvent.click(input); |
| 30 | + }); |
| 31 | + await step("Type in the input", async () => { |
| 32 | + await userEvent.type(input, "[email protected]"); |
| 33 | + }); |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +export const File: Story = { |
| 38 | + render() { |
| 39 | + return ( |
| 40 | + <div className="grid w-full max-w-sm items-center gap-1.5"> |
| 41 | + <Label htmlFor="picture">Picture</Label> |
| 42 | + <Input id="picture" type="file" /> |
| 43 | + </div> |
| 44 | + ); |
| 45 | + }, |
| 46 | + play: async ({ canvasElement, step }) => { |
| 47 | + const canvas = within(canvasElement); |
| 48 | + const input = canvas.getByLabelText("Picture"); |
| 49 | + await step("Click on input", async () => { |
| 50 | + await userEvent.click(input); |
| 51 | + }); |
| 52 | + }, |
| 53 | +}; |
| 54 | + |
| 55 | +export const Disabled: Story = { |
| 56 | + render() { |
| 57 | + return <Input disabled type="email" placeholder="Email" />; |
| 58 | + }, |
| 59 | + play: async ({ canvasElement, step }) => { |
| 60 | + const canvas = within(canvasElement); |
| 61 | + const input = canvas.getByPlaceholderText("Email"); |
| 62 | + await step("Click on input", async () => { |
| 63 | + await userEvent.click(input); |
| 64 | + }); |
| 65 | + await expect("disabled").toEqual("disabled"); |
| 66 | + }, |
| 67 | +}; |
| 68 | + |
| 69 | +export const WithLabel: Story = { |
| 70 | + render() { |
| 71 | + return ( |
| 72 | + <div className="grid w-full max-w-sm items-center gap-1.5"> |
| 73 | + <Label htmlFor="email">Email</Label> |
| 74 | + <Input type="email" id="email" placeholder="Email" /> |
| 75 | + </div> |
| 76 | + ); |
| 77 | + }, |
| 78 | + play: async ({ canvasElement, step }) => { |
| 79 | + const canvas = within(canvasElement); |
| 80 | + const input = canvas.getByPlaceholderText("Email"); |
| 81 | + await step("Click on input", async () => { |
| 82 | + await userEvent.click(input); |
| 83 | + }); |
| 84 | + await step("Type in the input", async () => { |
| 85 | + await userEvent.type(input, "[email protected]"); |
| 86 | + }); |
| 87 | + }, |
| 88 | +}; |
| 89 | + |
| 90 | +export const WithButton: Story = { |
| 91 | + render() { |
| 92 | + return ( |
| 93 | + <div className="flex w-full max-w-sm items-center space-x-2"> |
| 94 | + <Input type="email" placeholder="Email" /> |
| 95 | + <Button type="submit">Subscribe</Button> |
| 96 | + </div> |
| 97 | + ); |
| 98 | + }, |
| 99 | + play: async ({ canvasElement, step }) => { |
| 100 | + const canvas = within(canvasElement); |
| 101 | + const input = canvas.getByPlaceholderText("Email"); |
| 102 | + const button = canvas.getByText("Subscribe"); |
| 103 | + await step("Click on input", async () => { |
| 104 | + await userEvent.click(input); |
| 105 | + }); |
| 106 | + await step("Type in the input", async () => { |
| 107 | + await userEvent.type(input, "[email protected]"); |
| 108 | + }); |
| 109 | + await step("Click on button", async () => { |
| 110 | + await userEvent.click(button); |
| 111 | + }); |
| 112 | + }, |
| 113 | +}; |
0 commit comments