|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite' |
| 2 | +import { expect, userEvent, within } from 'storybook/test' |
| 3 | + |
| 4 | +import { EnumSize } from '@/types/enums' |
| 5 | +import { Button } from './Button' |
| 6 | +import { EnumButtonVariant } from './help' |
| 7 | + |
| 8 | +const meta: Meta<typeof Button> = { |
| 9 | + title: 'Components/Button', |
| 10 | + component: Button, |
| 11 | + tags: ['autodocs'], |
| 12 | + argTypes: { |
| 13 | + onClick: { action: 'clicked' }, |
| 14 | + variant: { |
| 15 | + control: { type: 'select' }, |
| 16 | + options: Object.values(EnumButtonVariant), |
| 17 | + }, |
| 18 | + size: { |
| 19 | + control: { type: 'select' }, |
| 20 | + options: Object.values(EnumSize), |
| 21 | + }, |
| 22 | + type: { |
| 23 | + control: { type: 'select' }, |
| 24 | + options: ['button', 'submit', 'reset'], |
| 25 | + }, |
| 26 | + disabled: { |
| 27 | + control: 'boolean', |
| 28 | + }, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +export default meta |
| 33 | + |
| 34 | +type Story = StoryObj<typeof Button> |
| 35 | + |
| 36 | +export const Default: Story = { |
| 37 | + args: { |
| 38 | + children: 'Default Button', |
| 39 | + }, |
| 40 | + play: async ({ canvasElement }) => { |
| 41 | + const canvas = within(canvasElement) |
| 42 | + await expect(canvas.getByText('Default Button')).toBeInTheDocument() |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +export const Variants: Story = { |
| 47 | + render: args => ( |
| 48 | + <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}> |
| 49 | + {Object.values(EnumButtonVariant).map(variant => ( |
| 50 | + <Button |
| 51 | + key={variant} |
| 52 | + {...args} |
| 53 | + variant={variant} |
| 54 | + > |
| 55 | + {variant} |
| 56 | + </Button> |
| 57 | + ))} |
| 58 | + </div> |
| 59 | + ), |
| 60 | +} |
| 61 | + |
| 62 | +export const Sizes: Story = { |
| 63 | + render: args => ( |
| 64 | + <div |
| 65 | + style={{ |
| 66 | + display: 'flex', |
| 67 | + gap: 12, |
| 68 | + flexWrap: 'wrap', |
| 69 | + alignItems: 'center', |
| 70 | + }} |
| 71 | + > |
| 72 | + {Object.values(EnumSize).map(size => ( |
| 73 | + <Button |
| 74 | + key={size} |
| 75 | + {...args} |
| 76 | + size={size} |
| 77 | + > |
| 78 | + {size} |
| 79 | + </Button> |
| 80 | + ))} |
| 81 | + </div> |
| 82 | + ), |
| 83 | +} |
| 84 | + |
| 85 | +export const Disabled: Story = { |
| 86 | + args: { |
| 87 | + children: 'Disabled Button', |
| 88 | + disabled: true, |
| 89 | + }, |
| 90 | + play: async ({ canvasElement }) => { |
| 91 | + const canvas = within(canvasElement) |
| 92 | + const button = canvas.getByRole('button') |
| 93 | + await expect(button).toBeDisabled() |
| 94 | + await expect(button).toHaveTextContent('Disabled Button') |
| 95 | + }, |
| 96 | +} |
| 97 | + |
| 98 | +export const AsChild: Story = { |
| 99 | + render: args => ( |
| 100 | + <Button |
| 101 | + asChild |
| 102 | + {...args} |
| 103 | + > |
| 104 | + <a href="#">Link as Button</a> |
| 105 | + </Button> |
| 106 | + ), |
| 107 | + play: async ({ canvasElement }) => { |
| 108 | + const canvas = within(canvasElement) |
| 109 | + const linkElement = canvas.getByText('Link as Button') |
| 110 | + await expect(linkElement.tagName).toBe('A') |
| 111 | + await expect(linkElement).toHaveAttribute('href', '#') |
| 112 | + }, |
| 113 | +} |
| 114 | + |
| 115 | +export const Types: Story = { |
| 116 | + render: args => ( |
| 117 | + <div style={{ display: 'flex', gap: 12 }}> |
| 118 | + <Button |
| 119 | + {...args} |
| 120 | + type="button" |
| 121 | + > |
| 122 | + Button |
| 123 | + </Button> |
| 124 | + <Button |
| 125 | + {...args} |
| 126 | + type="submit" |
| 127 | + > |
| 128 | + Submit |
| 129 | + </Button> |
| 130 | + <Button |
| 131 | + {...args} |
| 132 | + type="reset" |
| 133 | + > |
| 134 | + Reset |
| 135 | + </Button> |
| 136 | + </div> |
| 137 | + ), |
| 138 | +} |
| 139 | + |
| 140 | +export const InteractiveClick: Story = { |
| 141 | + args: { |
| 142 | + children: 'Click Me', |
| 143 | + }, |
| 144 | + play: async ({ canvasElement, args }) => { |
| 145 | + const canvas = within(canvasElement) |
| 146 | + const user = userEvent.setup() |
| 147 | + |
| 148 | + const button = canvas.getByRole('button') |
| 149 | + await expect(button).toBeInTheDocument() |
| 150 | + |
| 151 | + await user.click(button) |
| 152 | + await expect(args.onClick).toHaveBeenCalledTimes(1) |
| 153 | + |
| 154 | + await user.click(button) |
| 155 | + await expect(args.onClick).toHaveBeenCalledTimes(2) |
| 156 | + }, |
| 157 | +} |
0 commit comments