【发布时间】:2020-06-19 19:26:27
【问题描述】:
我的组件呈现以下内容
{list.options && list.options.length > 0 ? (
<div
data-testId="MyAlertText" onClick={onAddText}>
Add Text
</div>
) : null}
而且,在我的测试中,我正在执行以下操作
it('Add Text link should render', () => {
const { container, getByTestId} = render(<MyComp />);
const link = getByTestId('MyAlertText');
expect(link).toBeInTheDocument();
})
运行成功
但是当我尝试运行并模拟 onClick 时,它失败了。
it('Add Text link should call method', () => {
const { container, getByTestId} = render(<MyComp />);
const link = getByTestId('MyAlertText');
expect(link).toBeInTheDocument();
fireEvent.click(link );
expect(jest.fn()).toHaveBeenCalled();
})
我尝试使用jest mock 模拟该函数。我做错了什么?
【问题讨论】:
-
这样的属性大小写重要吗?我一直使用
data-testid而不是data-testId
标签: javascript reactjs jestjs react-testing-library