[英]React testing library - function not being called on dropdown select
I have a dropdown menu from ant design in my react component (consoliated code below, only showing relevant details): `我的反应组件中有一个来自 ant design 的下拉菜单(下面的合并代码,只显示相关细节):`
<Select
data-testid="addCompany"
showSearch
style={{ width: '100%' }}
onChange={(value) =>
onAddCompanySelectHandler(periodIndex, value)
}
placeholder="Add Company"
value={companyDropdownValue}
options={companyDropdownList}
/>
` `
This is my test that renders component with dropdown and mocked comanies passed to it as props: `这是我的测试,它呈现带有下拉列表的组件和作为道具传递给它的模拟公司:`
const companyDropdownList = companies; //got this from the mock data
describe('Test BaselineItemDetailsEdit component', () => {
const onAddCompanySelectHandler = jest.fn();
const editProps = {
companyDropdownList,
onAddCompanySelectHandler
};
it.only('calls add Company from dropdown', async () => {
render(<Component {...props} />);
const company = screen.getByText('Company Name');
userEvent.click(company);
expect(onAddCompanySelectHandler).toBeCalled();
});
` I seem to be able to find the company from the dropdown but when click on it function is not being called. ` 我似乎能够从下拉列表中找到公司,但是当点击它时,函数没有被调用。 I have other regular buttons in this components and test that do call mocked functions though.
我在这个组件中还有其他常规按钮并测试调用模拟函数。
` `
● Test ItemDetails component › calls add Company from dropdown
expect(jest.fn()).toBeCalled()
Expected number of calls: >= 1
Received number of calls: 0
95 | const company = screen.getByText('Company Name');
96 | userEvent.click(company);
> 97 | expect(onAddCompanySelectHandler).toBeCalled();
| ^
98 | });
99 | });
100 |
` `
I've tried to call mocked function and expected it to be called.我试图调用模拟函数并期望它被调用。
This is how I ended up testing it.这就是我最终测试它的方式。 Need to get combobox item firts with andt dropdown
需要首先使用 andt 下拉菜单获取组合框项目
it('calls Company from dropdown', async () => {
userEvent.click(screen.getByTestId('toExpand'));
userEvent.click(screen.getByRole('combobox'));
userEvent.click(screen.getByText('CompanyName'));
expect(onAddCompanySelectHandler).toBeCalledWith(0, 'CompanyName');
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.