简体   繁体   English

如何使用反应测试库更改蚂蚁设计选择选项

[英]How to change ant design select option using react testing library

I am using ant design select using react testing library am testing my component I wanted to change the select option to Education Loan.我正在使用 ant design select 使用 react 测试库测试我的组件我想将选择选项更改为教育贷款。

<Select
showSearch
placeholder="Select a Loan type"
optionFilterProp="children"
onChange={e => this.handleChangeText(e, "loanType")}
filterOption={(input, option) =>
    option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}
>
<Option value="education">Education Loan</Option>
<Option value="personal">Personal/Home Loan</Option>

The event you need to trigger is a mouseDown() on the first child of the select.您需要触发的事件是选择的第一个子项上的 mouseDown()。

const elt = getByTestId('your-select-test-id').firstElementChild;
fireEvent.mouseDown(elt); // THIS WILL OPEN THE SELECT !

You have 2 options, use toBeInTheDocument() or wait for the animation to be over by using waitFor(...)您有 2 个选项,使用 toBeInTheDocument() 或使用 waitFor(...) 等待动画结束

Option 1: Faster but not totally accurate, I prefer to use this for simple use cases as it makes the tests faster and synchronous选项 1:更快但不完全准确,我更喜欢将它用于简单的用例,因为它使测试更快且同步

expect(getByText('Option from Select')).toBeInTheDocument(); // WORKS !

Option 2: Slower as you need to wait for the animation to finish but more accurate for complex cases选项 2:较慢,因为您需要等待动画完成,但对于复杂情况更准确

await waitFor(() => expect(getByText('Option from Select')).toBeVisible()); // WORK

See depending github antd issue for more details.有关更多详细信息,请参阅取决于github antd 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM