import { Meta, StoryObj } from '@storybook/react-webpack5'; import { Menu, Plus, Settings, Star, Travel, Cross, Edit, Briefcase, Bank, Freeze, } from '@transferwise/icons'; import { expect, fn, userEvent, within } from 'storybook/test'; import IconButton from './IconButton'; import { withVariantConfig } from '../../.storybook/helpers'; import SentimentSurface from '../sentimentSurface'; const wait = async (ms: number) => new Promise((resolve) => { setTimeout(resolve, ms); }); export default { title: 'Actions/IconButton/Tests', component: IconButton, tags: ['!autodocs', '!manifest'], } satisfies Meta; type Story = StoryObj; /** * IconButton displayed across all themes and sentiments for visual regression testing. */ export const Variants: Story = { render: () => { const size = 32; const priorities = [ { key: 'primary', label: 'primary' }, { key: 'secondary', label: 'secondary' }, { key: 'tertiary', label: 'tertiary' }, { key: 'minimal', label: 'minimal' }, { key: 'neg-primary', label: 'negative\nprimary' }, { key: 'neg-secondary', label: 'negative\nsecondary' }, { key: 'disabled', label: 'disabled' }, ] as const; const sentiments = ['success', 'warning', 'negative', 'neutral', 'proposition'] as const; return (
{/* Header row with priority labels */}
{priorities.map((priority) => (
{priority.label}
))}
{/* Rows for each sentiment with base and elevated emphasis */} {sentiments.flatMap((sentiment) => [
{sentiment} (base)
,
{sentiment} (elevated)
, ])}
); }, ...withVariantConfig(['default', 'dark', 'bright-green', 'forest-green']), }; /** Tab through each priority variant and activate with Space. */ export const KeyboardInteraction: Story = { render: () => (
), play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); const buttons = canvas.getAllByRole('button'); await step('tab to Primary and press Space', async () => { await userEvent.tab(); await expect(buttons[0]).toHaveFocus(); await wait(400); await userEvent.keyboard(' '); }); await wait(400); await step('tab to Secondary and press Space', async () => { await userEvent.tab(); await expect(buttons[1]).toHaveFocus(); await wait(400); await userEvent.keyboard(' '); }); await wait(400); await step('tab to Tertiary and press Space', async () => { await userEvent.tab(); await expect(buttons[2]).toHaveFocus(); await wait(400); await userEvent.keyboard(' '); }); await wait(400); await step('tab to Minimal and press Space', async () => { await userEvent.tab(); await expect(buttons[3]).toHaveFocus(); await wait(400); await userEvent.keyboard(' '); }); await wait(400); await step('tab skips Disabled button', async () => { await userEvent.tab(); // Disabled button should be skipped await expect(buttons[4]).not.toHaveFocus(); }); }, }; /** IconButton at 400% zoom for accessibility testing. */ export const Zoom400: Story = { render: () => (
), ...withVariantConfig(['400%']), };