In general, I have problems targeting elements if they do not have a label/role. For example,
If I have a textarea like this
<>
{label && (
<label htmlFor={id} className={styles.label}>
{label}
</label>
)}
<textarea
id={id}
name={name}
placeholder={placeholder}
className={styles.textarea}
{...props}
>
{content}
</textarea>
</>
How do I target it with the play-function?
My current solution is:
const meta: Meta<typeof TextInput> = {
component: TextInput,
title: 'TextInput',
tags: ['autodocs'],
args: {
id: 'foo',
label: 'Label',
placeholder: 'Placeholder',
},
} satisfies Meta<typeof TextInput>;
export default meta;
type Story = StoryObj<typeof TextInput>;
export const WriteText: Story = {
args: {
content: 'hallo',
},
name: 'User Input: Click Textarea and Write',
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const textarea = canvas.getByText('hallo');
await userEvent.click(textarea);
await userEvent.keyboard('Hello');
},
};
but doesn't feel right. Isn't there something like passing a test-id in the props so I can target the element? I don't want the data-test-id in my native component though. So yeah, generally, how do you target simple elements like textareas, inputs etc.?