Skip to content

Commit 23e3619

Browse files
test(storybook): ActionIcon click DEV-98 (#5669)
### 💭 Notes I was trying to come up with some "proper" way of having types in `args`, but searching Storybook docs and thier GH gave me nothing useful. I made an issue to kindly point this out to Storybook team: storybookjs/storybook#31106. Other than that, it's just a simple test to show how it's done ;) ### 👀 Preview steps Testing: 1. ℹ️ run storybook 2. go to Design System → ActionIcon → Test Click story 3. open "Interactions" tab 4. 🟢 notice that test PASS --------- Co-authored-by: Akuukis <Akuukis@users.noreply.github.com>
1 parent 3456eb7 commit 23e3619

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

jsapp/js/components/common/ActionIcon.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { forwardRef } from 'react'
2-
3-
import { ActionIcon as ActionIconMantine } from '@mantine/core'
1+
import { ActionIcon as ActionIconMantine, createPolymorphicComponent } from '@mantine/core'
42
import type { ActionIconProps as ActionIconPropsMantine } from '@mantine/core/lib/components'
3+
import { forwardRef } from 'react'
54
import type { IconName } from '#/k-icons'
65
import Icon, { type IconSize } from './icon'
76

@@ -20,5 +19,6 @@ const ActionIcon = forwardRef<HTMLButtonElement, ActionIconProps>(({ iconName, .
2019
</ActionIconMantine>
2120
)
2221
})
22+
ActionIcon.displayName = 'ActionIcon'
2323

24-
export default ActionIcon
24+
export default createPolymorphicComponent<'button', ActionIconProps>(ActionIcon)

jsapp/js/components/common/actionIcon.stories.tsx

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
2-
1+
import type { ElementProps } from '@mantine/core'
32
import type { Meta, StoryObj } from '@storybook/react'
3+
import { expect, fn, userEvent, within } from '@storybook/test'
44
import { IconNames } from '#/k-icons'
55
import ActionIcon, { type ActionIconProps } from './ActionIcon'
66

@@ -16,7 +16,7 @@ const actionIconVariants: Array<ActionIconProps['variant']> = [
1616

1717
const actionIconSizes: Array<ActionIconProps['size']> = ['sm', 'md', 'lg']
1818

19-
export default {
19+
const meta = {
2020
title: 'Design system/ActionIcon',
2121
component: ActionIcon,
2222
argTypes: {
@@ -38,51 +38,20 @@ export default {
3838
disabled: { control: 'boolean' },
3939
loading: { control: 'boolean' },
4040
},
41-
} as Meta<typeof ActionIcon>
42-
43-
type Story = StoryObj<typeof ActionIcon>
44-
45-
export const Filled: Story = {
46-
args: {
47-
variant: 'filled',
48-
size: 'md',
49-
iconName: 'edit',
50-
},
51-
}
52-
53-
export const Light: Story = {
54-
args: {
55-
variant: 'light',
56-
size: 'md',
57-
iconName: 'edit',
58-
},
59-
}
60-
61-
export const Transparent: Story = {
62-
args: {
63-
variant: 'transparent',
64-
size: 'md',
65-
iconName: 'more',
66-
},
67-
}
41+
} satisfies Meta<typeof ActionIcon>
6842

69-
export const Danger: Story = {
70-
args: {
71-
variant: 'danger',
72-
size: 'md',
73-
iconName: 'trash',
74-
},
75-
}
43+
export default meta
44+
type StoryArgs = ActionIconProps & ElementProps<'button', keyof ActionIconProps> & { 'data-testid'?: string }
45+
type Story = StoryObj<typeof ActionIcon> & { args: StoryArgs }
7646

77-
export const DangerSecondary: Story = {
47+
export const Default: Story = {
7848
args: {
79-
variant: 'danger-secondary',
49+
iconName: 'document',
8050
size: 'lg',
81-
iconName: 'trash',
8251
},
8352
}
8453

85-
export const AllIconStyles = () => (
54+
export const Preview = () => (
8655
<div
8756
style={{
8857
display: 'grid',
@@ -111,3 +80,21 @@ export const AllIconStyles = () => (
11180
)}
11281
</div>
11382
)
83+
84+
export const TestClick: Story = {
85+
args: {
86+
variant: 'filled',
87+
size: 'md',
88+
iconName: 'edit',
89+
onClick: fn(),
90+
'data-testid': 'ActionIcon-click-test',
91+
},
92+
play: async ({ args, canvasElement }) => {
93+
// Unfortunately Storybook doesn't pass proper types for `args`, so we need to cast it.
94+
// TODO: I made an issue to point this out to Storybook team: https://github.com/storybookjs/storybook/issues/31106
95+
// let's fix this when they fix it.
96+
const canvas = within(canvasElement)
97+
await userEvent.click(canvas.getByTestId((args as StoryArgs)['data-testid']!))
98+
await expect((args as StoryArgs).onClick).toHaveBeenCalledTimes(1)
99+
},
100+
}

0 commit comments

Comments
 (0)