|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite' |
| 2 | +import { Button } from './button' |
| 3 | +import { Plus, Download, Trash2 } from 'lucide-react' |
| 4 | +import { expect, fn, userEvent, within } from 'storybook/test' |
| 5 | + |
| 6 | +const meta: Meta<typeof Button> = { |
| 7 | + title: 'UI/Button', |
| 8 | + component: Button, |
| 9 | + argTypes: { |
| 10 | + variant: { |
| 11 | + control: 'select', |
| 12 | + options: [ |
| 13 | + 'primary', |
| 14 | + 'secondary', |
| 15 | + 'outline', |
| 16 | + 'ghost', |
| 17 | + 'success', |
| 18 | + 'cuation', |
| 19 | + 'information', |
| 20 | + 'warning', |
| 21 | + ], |
| 22 | + }, |
| 23 | + size: { |
| 24 | + control: 'select', |
| 25 | + options: ['xxs', 'xs', 'sm', 'md', 'lg', 'icon'], |
| 26 | + }, |
| 27 | + disabled: { |
| 28 | + control: 'boolean', |
| 29 | + }, |
| 30 | + asChild: { |
| 31 | + control: 'boolean', |
| 32 | + }, |
| 33 | + children: { |
| 34 | + control: 'text', |
| 35 | + }, |
| 36 | + }, |
| 37 | + args: { |
| 38 | + children: 'Button', |
| 39 | + variant: 'primary', |
| 40 | + size: 'md', |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +export default meta |
| 45 | + |
| 46 | +type Story = StoryObj<typeof Button> |
| 47 | + |
| 48 | +export const Primary: Story = { |
| 49 | + args: { |
| 50 | + variant: 'primary', |
| 51 | + children: 'Primary Button', |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +export const Secondary: Story = { |
| 56 | + args: { |
| 57 | + variant: 'secondary', |
| 58 | + children: 'Secondary Button', |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +export const Outline: Story = { |
| 63 | + args: { |
| 64 | + variant: 'outline', |
| 65 | + children: 'Outline Button', |
| 66 | + }, |
| 67 | +} |
| 68 | + |
| 69 | +export const Ghost: Story = { |
| 70 | + args: { |
| 71 | + variant: 'ghost', |
| 72 | + children: 'Ghost Button', |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +export const Success: Story = { |
| 77 | + args: { |
| 78 | + variant: 'success', |
| 79 | + children: 'Success Button', |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +export const Warning: Story = { |
| 84 | + args: { |
| 85 | + variant: 'warning', |
| 86 | + children: 'Warning Button', |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +export const Disabled: Story = { |
| 91 | + args: { |
| 92 | + variant: 'primary', |
| 93 | + children: 'Disabled Button', |
| 94 | + disabled: true, |
| 95 | + }, |
| 96 | +} |
| 97 | + |
| 98 | +export const WithIcon: Story = { |
| 99 | + render: (args) => ( |
| 100 | + <Button {...args}> |
| 101 | + <Plus /> Add Item |
| 102 | + </Button> |
| 103 | + ), |
| 104 | + args: { |
| 105 | + variant: 'primary', |
| 106 | + }, |
| 107 | +} |
| 108 | + |
| 109 | +export const IconOnly: Story = { |
| 110 | + render: (args) => ( |
| 111 | + <Button {...args}> |
| 112 | + <Download /> |
| 113 | + </Button> |
| 114 | + ), |
| 115 | + args: { |
| 116 | + variant: 'ghost', |
| 117 | + size: 'icon', |
| 118 | + }, |
| 119 | +} |
| 120 | + |
| 121 | +export const AllVariants: Story = { |
| 122 | + render: () => ( |
| 123 | + <div className="flex flex-wrap gap-4"> |
| 124 | + <Button variant="primary">Primary</Button> |
| 125 | + <Button variant="secondary">Secondary</Button> |
| 126 | + <Button variant="outline">Outline</Button> |
| 127 | + <Button variant="ghost">Ghost</Button> |
| 128 | + <Button variant="success">Success</Button> |
| 129 | + <Button variant="warning">Warning</Button> |
| 130 | + </div> |
| 131 | + ), |
| 132 | +} |
| 133 | + |
| 134 | +export const AllSizes: Story = { |
| 135 | + render: () => ( |
| 136 | + <div className="flex flex-wrap items-center gap-4"> |
| 137 | + <Button variant="primary" size="xxs"> |
| 138 | + XXS |
| 139 | + </Button> |
| 140 | + <Button variant="primary" size="xs"> |
| 141 | + XS |
| 142 | + </Button> |
| 143 | + <Button variant="primary" size="sm"> |
| 144 | + SM |
| 145 | + </Button> |
| 146 | + <Button variant="primary" size="md"> |
| 147 | + MD |
| 148 | + </Button> |
| 149 | + <Button variant="primary" size="lg"> |
| 150 | + LG |
| 151 | + </Button> |
| 152 | + <Button variant="primary" size="icon"> |
| 153 | + <Trash2 /> |
| 154 | + </Button> |
| 155 | + </div> |
| 156 | + ), |
| 157 | +} |
| 158 | + |
| 159 | +// Interaction test stories |
| 160 | +export const ClickInteraction: Story = { |
| 161 | + args: { |
| 162 | + variant: 'primary', |
| 163 | + children: 'Click Me', |
| 164 | + onClick: fn(), |
| 165 | + }, |
| 166 | + play: async ({ args, canvasElement }) => { |
| 167 | + const canvas = within(canvasElement) |
| 168 | + const button = canvas.getByRole('button', { name: /click me/i }) |
| 169 | + |
| 170 | + // Test that button is visible and enabled |
| 171 | + await expect(button).toBeVisible() |
| 172 | + await expect(button).toBeEnabled() |
| 173 | + |
| 174 | + // Click the button |
| 175 | + await userEvent.click(button) |
| 176 | + |
| 177 | + // Verify the onClick handler was called |
| 178 | + await expect(args.onClick).toHaveBeenCalledTimes(1) |
| 179 | + }, |
| 180 | +} |
| 181 | + |
| 182 | +export const DisabledInteraction: Story = { |
| 183 | + args: { |
| 184 | + variant: 'primary', |
| 185 | + children: 'Disabled Button', |
| 186 | + disabled: true, |
| 187 | + onClick: fn(), |
| 188 | + }, |
| 189 | + play: async ({ args, canvasElement }) => { |
| 190 | + const canvas = within(canvasElement) |
| 191 | + const button = canvas.getByRole('button', { name: /disabled button/i }) |
| 192 | + |
| 193 | + // Test that button is visible but disabled |
| 194 | + await expect(button).toBeVisible() |
| 195 | + await expect(button).toBeDisabled() |
| 196 | + |
| 197 | + // Verify the onClick handler was NOT called (disabled buttons block pointer events) |
| 198 | + await expect(args.onClick).not.toHaveBeenCalled() |
| 199 | + }, |
| 200 | +} |
| 201 | + |
| 202 | +export const HoverInteraction: Story = { |
| 203 | + args: { |
| 204 | + variant: 'outline', |
| 205 | + children: 'Hover Over Me', |
| 206 | + }, |
| 207 | + play: async ({ canvasElement }) => { |
| 208 | + const canvas = within(canvasElement) |
| 209 | + const button = canvas.getByRole('button', { name: /hover over me/i }) |
| 210 | + |
| 211 | + // Test initial state |
| 212 | + await expect(button).toBeVisible() |
| 213 | + |
| 214 | + // Hover over the button |
| 215 | + await userEvent.hover(button) |
| 216 | + |
| 217 | + // The button should still be visible after hover |
| 218 | + await expect(button).toBeVisible() |
| 219 | + |
| 220 | + // Unhover |
| 221 | + await userEvent.unhover(button) |
| 222 | + }, |
| 223 | +} |
0 commit comments