Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some basic interactions stories #19153

Merged
merged 12 commits into from
Sep 13, 2022
75 changes: 75 additions & 0 deletions code/addons/interactions/template/stories/basics.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* 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,
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
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 (context) => {
await context.step('Enter value', async () => Type.play(context));
},
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
};

export const Click = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await fireEvent.click(canvas.getByTestId('succeed'));
},
};

export const Callback = {
play: async (context) => {
const { args, canvasElement, step } = context;
const canvas = within(canvasElement);
await step('Enter value', async () => Type.play(context));

await step('Click checkbox', async () => Click.play(context));

await step('Submit', async () => {
await fireEvent.click(canvas.getByRole('button'));
});

await expect(args.onSuccess).toHaveBeenCalled();
},
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
};

export const WaitFor = {
play: async (context) => {
const { args, canvasElement, step } = context;
const canvas = within(canvasElement);
await step('Enter value', async () => Type.play(context));

await step('Submit', async () => {
await fireEvent.click(canvas.getByRole('button'));
});

await expect(args.onSuccess).not.toHaveBeenCalled();

await waitFor(
async function () {
await canvas.getByTestId("Submitted 'test' when not allowed!");
},
{ timeout: 2000 }
);
},
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
};

export const Hover = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.hover(canvas.getByRole('button'));
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
},
};
54 changes: 54 additions & 0 deletions code/renderers/react/template/components/Form.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';

export const Form = ({ onSuccess }) => {
const [value, setValue] = useState('');
const [succeed, setSucceed] = useState(false);
const [error, setError] = useState(null);

function onSubmit(event) {
event.preventDefault();
if (succeed) {
setError(null);
onSuccess(value);
} else {
setTimeout(() => {
setError(`Submitted '${value}' when not allowed!`);
}, 1000);
}
}

return (
<form id="interaction-test-form" onSubmit={onSubmit}>
<style>{`
#interaction-test-form button:hover {
background: red;
}
`}</style>
tmeasday marked this conversation as resolved.
Show resolved Hide resolved
<label>
Enter Value
<input
type="text"
data-testid="value"
value={value}
onChange={(event) => setValue(event.target.value)}
/>
</label>
<label>
Should succeed?
<input
type="checkbox"
data-testid="succeed"
value={succeed}
onChange={(event) => setSucceed(event.target.checked)}
/>
</label>
<button type="submit">Submit</button>
{error && <p data-testid="error">{error}</p>}
</form>
);
};

Form.propTypes = {
onSuccess: PropTypes.func.isRequired,
};
3 changes: 2 additions & 1 deletion code/renderers/react/template/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import globalThis from 'global';

import { Button } from './Button.jsx';
import { Pre } from './Pre.jsx';
import { Form } from './Form.jsx';

globalThis.Components = { Button, Pre };
globalThis.Components = { Button, Pre, Form };