fbredius/storybook

View on GitHub
docs/snippets/react/my-component-play-function-waitfor.ts.mdx

Summary

Maintainability
Test Coverage
```ts
// MyComponent.stories.ts|tsx

import React from 'react';

import { ComponentStory, ComponentMeta } from '@storybook/react';

import { screen, userEvent, waitFor } from '@storybook/testing-library';

import { MyComponent } from './MyComponent';

export default {
  /* 👇 The title prop is optional.
  * See https://storybook.js.org/docsreact/configure/overview#configure-story-loading
  * to learn how to generate automatic titles
  */
  title: 'WithAsync',
  component: MyComponent,
} as ComponentMeta<typeof MyComponent>;

const Template: ComponentStory<typeof MyComponent> = (args) => <MyComponent {...args} />;

export const ExampleAsyncStory = Template.bind({});
ExampleAsyncStory.play = async () => {
  const Input = screen.getByLabelText('Username', {
    selector: 'input',
  });

  await userEvent.type(Input, 'WrongInput', {
    delay: 100,
  });
  // See https://storybook.js.org/docs/react/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
  const Submit = screen.getByRole('button');

  await userEvent.click(Submit);

  await waitFor(async () => {
    await userEvent.hover(screen.getByTestId('error'));
  });
};
```