【发布时间】:2021-02-10 20:43:29
【问题描述】:
我正在尝试将剪贴板中已有的文本粘贴到文本框中,但我不明白如何使用“eventInit”来执行此操作。我已阅读有关如何将文本粘贴到文本框中的文档,但不清楚如何使用 eventInit。
如何使用 userEvent 将剪贴板中的文本粘贴到文本框中?
这是我的代码:
test('Copy id button copies correct id', async () => {
const { getAllByLabelText, debug, getByText } = render(
<MockedProvider mocks={mocks} addTypename={false}>
<History />
</MockedProvider>
);
const textbox = <input type="text" />;
await waitFor(() => new Promise((resolve) => setTimeout(resolve, 0)));
const button = getAllByLabelText('click to copy id')[0];
fireEvent.click(button);
// userEvent.paste(textbox,_,) unsure what to do here...
});
【问题讨论】:
-
您能提供您的
copy to clipboard代码吗? -
无需设置输入即可粘贴内容;只需在您正在测试的按钮内模拟您正在调用的函数,并使用 id 值检查它是否被调用,类似于这个问题stackoverflow.com/questions/61807378/…
-
你不需要粘贴你的剪贴板,你可以假设模拟文本作为你的剪贴板文本。
userEvent.paste(getByRole('textbox', { name: /paste your greeting/i }), text)表示将剪贴板文本模拟到目标文本框中。在此粘贴操作之后,您只需检查目标文本框的内容是否包含模拟文本。
标签: reactjs unit-testing testing integration-testing react-testing-library