简体   繁体   English

如何使用 jest、react-testing-library 在 React 中模拟元素上的点击事件

[英]How to mock a click event on an element in React using jest , react-testing-library

My component renders the following我的组件呈现以下内容

{list.options && list.options.length > 0 ? (
<div
    data-testId="MyAlertText" onClick={onAddText}>
    Add Text
</div>
) : null}

And, in my tests I am doing the following而且,在我的测试中,我正在执行以下操作

it('Add Text link should render', () => {
    const { container, getByTestId} = render(<MyComp />);

    const link = getByTestId('MyAlertText');
    expect(link).toBeInTheDocument();
})

It runs successfully它运行成功

But when I try to run, and simulate onClick it fails.但是当我尝试运行并模拟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();
})

I tried mocking the function using jest mock .我使用jest mock尝试了 mocking function 。 What did I do wrong?我做错了什么?

link.simulate('click') - should to the job! link.simulate('click') - 应该去工作!

Generally, you shouldn't worry about testing methods inside your component.通常,您不必担心组件内部的测试方法。 Rather, you should test the side effects of those methods.相反,您应该测试这些方法的副作用

Of course onAddText will be called (or whatever onAddText references), but what does onAddText actually do?当然会调用onAddText (或任何onAddText引用),但onAddText实际上做了什么? Test that effect.测试那个效果。

You can use shallow in jest to resolve your case.您可以开玩笑地使用shallow来解决您的问题。

it('Add Text link should render', async () => {
  const wrapper = shallow(<MyComp />);
  const spy = jest.spyOn(wrapper.instance(), 'onAddText');
  wrapper.find('#MyAlertText).simulate('click');
  await wrapper.update();
  expect(spy).toHaveBeenCalled();
});

暂无
暂无

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

相关问题 使用 react-testing-library 和 jest 测试是否调用了 prop 函数 - Testing if a prop function was called using react-testing-library and jest 如何在Jest和react-testing-library中使用react-hook - How to use react-hooks with Jest and react-testing-library 如何使用 React、Jest 和 React-testing-library 为带有令牌的 api 调用编写单元测试? - How can I write unit test for api call with token using React, Jest and React-testing-library? 如何使用 React 测试库 + Jest 模拟测试函数 - How to mock functions for testing using the React Testing Library + Jest 如何在使用jest和react-testing-library进行测试时设置组件的本地状态? - How to set component's local state while testing using jest and react-testing-library? 用于 react-select 或 react-testing-library on change 的 Jest mock 不触发目标更改 - Jest mock for react-select or react-testing-library on change not firing on target change 如何使用 react-testing-library 和 Jest 按值查询 textarea 元素? - How to query textarea elements by value using react-testing-library and Jest? 更新未包含在 act(...) 中,并且模拟 function 未触发。 反应测试库,开玩笑 - Update not wrapped in act(…) and the mock function not firing. react-testing-library, jest 使用 React-Testing-Library 模拟 ArrowRight 事件 - Simulating ArrowRight event with React-Testing-Library Jest &amp; React &amp; Typescript &amp; React-Testing-Library 错误 - Jest & React & Typescript & React-Testing-Library error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM