mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 11:11:53 +08:00
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
/* eslint-disable jest/no-standalone-expect */
|
|
import globalThis from 'global';
|
|
import { within, waitFor, fireEvent, userEvent } from '@storybook/testing-library';
|
|
import { expect } from '@storybook/jest';
|
|
|
|
export default {
|
|
component: globalThis.Components.Form,
|
|
argTypes: {
|
|
onSuccess: { type: 'function' },
|
|
},
|
|
};
|
|
|
|
export const Type = {
|
|
play: async ({ canvasElement }) => {
|
|
const canvas = within(canvasElement);
|
|
await userEvent.type(canvas.getByTestId('value'), 'test');
|
|
},
|
|
};
|
|
|
|
export const Step = {
|
|
play: async ({ step }) => {
|
|
await step('Enter value', Type.play);
|
|
},
|
|
};
|
|
|
|
export const Callback = {
|
|
play: async ({ args, canvasElement, step }) => {
|
|
const canvas = within(canvasElement);
|
|
await step('Enter value', Type.play);
|
|
|
|
await step('Submit', async () => {
|
|
await fireEvent.click(canvas.getByRole('button'));
|
|
});
|
|
|
|
await expect(args.onSuccess).toHaveBeenCalled();
|
|
},
|
|
};
|
|
|
|
// NOTE: of course you can use `findByText()` to implicitly waitFor, but we want
|
|
// an explicit test here
|
|
export const SyncWaitFor = {
|
|
play: async ({ canvasElement, step }) => {
|
|
const canvas = within(canvasElement);
|
|
await step('Setup', Callback.play);
|
|
await waitFor(() => canvas.getByText('Completed!!'));
|
|
},
|
|
};
|
|
|
|
export const AsyncWaitFor = {
|
|
play: async ({ args, canvasElement, step }) => {
|
|
const canvas = within(canvasElement);
|
|
await step('Setup', Callback.play);
|
|
await waitFor(async () => canvas.getByText('Completed!!'));
|
|
},
|
|
};
|
|
|
|
export const Validation = {
|
|
play: async (context) => {
|
|
const { args, canvasElement, step } = context;
|
|
const canvas = within(canvasElement);
|
|
|
|
await step('Submit', async () => fireEvent.click(canvas.getByRole('button')));
|
|
|
|
await expect(args.onSuccess).not.toHaveBeenCalled();
|
|
},
|
|
};
|