import { Meta, StoryObj } from '@storybook/react-webpack5'; import { ArrowEnd, ChevronEnd, Freeze } from '@transferwise/icons'; import { Flag } from '@wise/art'; import { expect, userEvent, within } from 'storybook/test'; import Button from '../Button.resolver'; import { ButtonPriority } from '../Button.types'; import { withVariantConfig } from '../../../.storybook/helpers'; import { useRtlDiffI18n, withRTLDiff } from '../../../.storybook/decorators/RtlDiffDecorator'; const meta: Meta = { component: Button, title: 'Actions/Button/Tests', tags: ['!autodocs', '!manifest'], }; export default meta; type Story = StoryObj; export const FocusTertiary: Story = { render: () => ( ), play: async ({ canvasElement }) => { await userEvent.tab(); const canvas = within(canvasElement); const buttonElement: HTMLButtonElement = canvas.getByRole('button'); await expect(buttonElement).toHaveFocus(); }, }; const buttonPriorities = ['primary', 'secondary', 'secondary-neutral', 'tertiary'] as const; const buttonSizes = ['sm', 'md', 'lg'] as const; export const AllVariants: Story = { tags: ['!autodocs'], ...withVariantConfig(['default', 'dark', 'bright-green', 'forest-green'], { parameters: { padding: '0', }, }), render: () => (
{buttonPriorities.map((priority) => buttonSizes.map((size) => (
)), )} {['primary', 'secondary'].map((priority) => buttonSizes.map((size) => (
)), )}
), }; export const Disabled: Story = { args: { v2: true, disabled: true, children: 'Disabled button', }, }; export const Loading: Story = { args: { v2: true, loading: true, children: 'Loading button', }, }; export const AccessibilityAddons: Story = { args: { v2: true, addonStart: { type: 'avatar', value: [{ asset: }, { asset: }], }, 'aria-label': 'Convert Real to Yen', children: 'Convert', size: 'md', }, }; type RTLDiffLabels = { startIcon: string; endIcon: string; bothIcons: string; avatarStart: string; doubleAvatar: string; smStartIcon: string; smEndIcon: string; smBothIcons: string; loading: string; blockWithStartIcon: string; }; export const RTLDiff: Story = { render: () => { const i18n = useRtlDiffI18n(); return ( <> ); }, ...withRTLDiff({ fixedRows: { heights: 'auto' }, i18n: { startIcon: { en: 'Start icon', ar: 'أيقونة البداية' }, endIcon: { en: 'End icon', ar: 'أيقونة النهاية' }, bothIcons: { en: 'Both icons', ar: 'كلتا الأيقونتين' }, avatarStart: { en: 'Avatar start', ar: 'صورة رمزية في البداية' }, doubleAvatar: { en: 'Double avatar', ar: 'صورتان رمزيتان' }, smStartIcon: { en: 'Sm start icon', ar: 'أيقونة بداية صغيرة' }, smEndIcon: { en: 'Sm end icon', ar: 'أيقونة نهاية صغيرة' }, smBothIcons: { en: 'Sm both icons', ar: 'كلتا الأيقونتين صغيرة' }, loading: { en: 'Loading', ar: 'جارٍ التحميل' }, blockWithStartIcon: { en: 'Block with start icon', ar: 'زر بعرض كامل مع أيقونة البداية' }, }, }), }; export const IconsOnNarrowViewports: Story = { parameters: { chromatic: { viewports: [300, 320] }, }, render: () => (
{(['sm', 'md'] as const).map((size) => (['start', 'end'] as const).map((position) => ( )), )}
), play: async ({ canvasElement }) => { await document.fonts.ready; const icons = canvasElement.querySelectorAll('.wds-Button-icon'); await expect(icons).toHaveLength(4); for (const icon of icons) { const size = icon.classList.contains('wds-Button-icon--sm') ? 16 : 18; await expect(getComputedStyle(icon).flexShrink).toBe('0'); await expect(icon.getBoundingClientRect().width).toBeCloseTo(size, 1); const svg = icon.querySelector('svg'); await expect(svg).not.toBeNull(); await expect(svg?.getBoundingClientRect().width).toBeCloseTo(size, 1); await expect(svg?.getBoundingClientRect().height).toBeCloseTo(size, 1); } const avatars = canvasElement.querySelector('.wds-Button-avatars'); await expect(avatars && getComputedStyle(avatars).flexShrink).toBe('0'); }, };