-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathButton.stories.tsx
More file actions
120 lines (109 loc) · 2.69 KB
/
Button.stories.tsx
File metadata and controls
120 lines (109 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import type { Meta, StoryObj } from '@storybook/react-vite'
import type { Size } from '@sqlmesh-common/types'
import { Button, type ButtonVariant } from './Button'
import { fn, expect, userEvent, within } from 'storybook/test'
const buttonVariants: ButtonVariant[] = [
'primary',
'secondary',
'alternative',
'destructive',
'danger',
'transparent',
]
const meta: Meta<typeof Button> = {
title: 'Components/Button',
component: Button,
}
export default meta
type Story = StoryObj<typeof Button>
export const Default: Story = {
args: {
children: 'Default Button',
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement)
await expect(canvas.getByText('Default Button')).toBeInTheDocument()
},
}
export const Variants: Story = {
render: args => (
<div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
{Object.values(buttonVariants).map(variant => (
<Button
key={variant}
{...args}
variant={variant}
>
{variant}
</Button>
))}
</div>
),
}
const sizes: Size[] = ['2xs', 'xs', 's', 'm', 'l', 'xl', '2xl']
export const Sizes: Story = {
render: args => (
<div
style={{
display: 'flex',
gap: 12,
flexWrap: 'wrap',
alignItems: 'center',
}}
>
{sizes.map(size => (
<Button
key={size}
{...args}
size={size}
>
{size}
</Button>
))}
</div>
),
}
export const Disabled: Story = {
args: {
children: 'Disabled Button',
disabled: true,
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement)
const button = canvas.getByRole('button')
await expect(button).toBeDisabled()
await expect(button).toHaveTextContent('Disabled Button')
},
}
export const AsChild: Story = {
render: args => (
<Button
asChild
{...args}
>
<a href="#">Link as Button</a>
</Button>
),
play: async ({ canvasElement }) => {
const canvas = within(canvasElement)
const linkElement = canvas.getByText('Link as Button')
await expect(linkElement.tagName).toBe('A')
await expect(linkElement).toHaveAttribute('href', '#')
},
}
export const InteractiveClick: Story = {
args: {
children: 'Click Me',
onClick: fn(),
},
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement)
const user = userEvent.setup()
const button = canvas.getByRole('button')
await expect(button).toBeInTheDocument()
await user.click(button)
await expect(args.onClick).toHaveBeenCalledTimes(1)
await user.click(button)
await expect(args.onClick).toHaveBeenCalledTimes(2)
},
}