import type { Meta, StoryObj } from '@storybook/react-webpack5'; import { expect, screen, userEvent, waitFor, within } from 'storybook/test'; import { useState } from 'react'; import { withVariantConfig } from '../../../../.storybook/helpers'; import Select, { SelectOptionItem } from '../Select'; const meta = { title: 'Forms/Select/Tests', component: Select, tags: ['!autodocs', '!manifest', 'deprecated'], } satisfies Meta; export default meta; type Story = StoryObj; const options: SelectOptionItem[] = Array.from({ length: 10 }, (_, i) => ({ value: i + 1, label: `Option ${i + 1}`, })); const wait = async (duration = 500) => new Promise((resolve) => { setTimeout(resolve, duration); }); /** * Visual regression story: Select placed at the bottom of the viewport so the dropdown * flips to the top when opened. The play function opens the dropdown automatically. */ export const DropdownFlipsToTop: Story = { render: function Render() { const [selected, setSelected] = useState(options[0]); return (
{ if (option && 'value' in option) { setSelected(option as SelectOptionItem); } }} />

); }, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement); const trigger = canvas.getByRole('button', { name: /Option 1/i }); await step('Open the combobox', async () => { await userEvent.click(trigger); }); await step('Check if options are displayed', async () => { await waitFor(async () => expect((await screen.findAllByRole('option')).length).toBeGreaterThan(0), ); }); await step('Close the combobox', async () => { await userEvent.keyboard('{Escape}'); await wait(); }); await step('Verify the page has not scrolled', async () => { const scrollParent = canvasElement.ownerDocument.documentElement; await expect(scrollParent.scrollTop).toBe(0); }); }, }); export const NoScrollAfterCloseTop: Story = generateNoScrollTest(); export const NoScrollAfterCloseBottom: Story = generateNoScrollTest(true);