简体   繁体   English

当包含的反应元素具有箭头函数事件处理程序时,如何使用 .contains(nodeOrNodes) A​​PI?

[英]How to use .contains(nodeOrNodes) API when the contained react element has an arrow function event handler?

For below example, .contains(nodeOrNodes) => Boolean API works fine.对于下面的示例, .contains(nodeOrNodes) => Boolean API 工作正常。

index.tsx : index.tsx

import React from 'react';

const Comp = ({ onChange }) => (
  <form>
    <input type="text" placeholder="username" onChange={onChange} />
  </form>
);

export default Comp;

index.test.tsx : index.test.tsx

import React from 'react';
import { shallow } from 'enzyme';
import Comp from '.';

describe('Comp', () => {
  it('should render', () => {
    const noop = () => null;
    const wrapper = shallow(<Comp onChange={noop} />);
    expect(
      wrapper.contains(
        <form>
          <input type="text" placeholder="username" onChange={noop} />
        </form>,
      ),
    ).toBeTruthy();
  });
});

Unit test results:单元测试结果:

 PASS  src/stackoverflow/46133847/02/index.test.tsx
  Comp
    ✓ should render (13ms)

-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |      100 |      100 |      100 |      100 |                   |
 index.tsx |      100 |      100 |      100 |      100 |                   |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        7.754s, estimated 24s

But, if I change the onChange event handler using the arrow function:但是,如果我使用箭头函数更改onChange事件处理程序:

index.ts : index.ts

import React from 'react';

const Comp = ({ onChange }) => (
  <form>
    <input type="text" placeholder="username" onChange={(e) => onChange(e)} />
  </form>
);

export default Comp;

The unit test will fail.单元测试将失败。

 FAIL  src/stackoverflow/46133847/02/index.test.tsx
  Comp
    ✕ should render (18ms)

  ● Comp › should render

    expect(received).toBeTruthy()

    Received: false

      13 |         </form>,
      14 |       ),
    > 15 |     ).toBeTruthy();
         |       ^
      16 |   });
      17 | });
      18 | 

      at Object.it (src/stackoverflow/46133847/02/index.test.tsx:15:7)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        7.689s, estimated 25s

I think the test failed because the arrow function created a new function reference.我认为测试失败是因为箭头函数创建了一个新的函数引用。 This new function has a different reference with noop function passed into Comp .这个新函数有一个不同的引用, noop函数传递给Comp

But what I want is, is there any way like expect.any(Function) of jestjs , just to assert whether or not the wrapper contains any function of onChange event handler?但我想要的是,有没有像expect.any(Function)jestjs ,只是为了断言wrapper是否包含onChange事件处理程序的任何函数?

Package versions:包版本:

"enzyme": "^3.10.0",
"jest": "^24.9.0",

To be honest, I do not think using .contains(nodeOrNodes) is good way for this situation.老实说,我认为使用 .contains(nodeOrNodes) 是解决这种情况的好方法。

My personal suggestion is followed by below:我个人的建议如下:

 let inputComponent = wrapper.find("input[placeholder]='username'");
 expect(inputComponent.length).toBe(1); // This tests your component is exists rather than contains)
 expect(inputComponent.props().onChange).toEqual(noop); //This tests onChange function  is equal your mock function

Please vote it, if it works for you请投票,如果它适合你

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

相关问题 为什么我必须在反应的事件处理程序中使用箭头 function? - Why do I have to use arrow function in event handler of react? 当使用胖箭头功能作为事件处理程序时,如何模拟React组件的事件? - How to mock a react component's event when fat arrow function is used as event handler? 在反应的事件处理程序中命名为 function 和箭头 function - named function and arrow function in event handler in react 在React的事件处理程序onClick()中,将prop值作为参数传递给箭头函数 - Pass prop value as a parameter in an arrow function in an event handler onClick() in React React Native:何时使用箭头 function - React native : when to use arrow function 如何在 React 中动态绑定事件处理函数 - How to bind event handler function dynamically in React 如何在 React 的事件处理函数中访问 DOM 元素的属性和值? - How to access DOM element's attributes and value inside an event handler function in React? 如何在React中传递带有事件处理函数的道具? - How to pass a prop with an event handler function in React? 如何在箭头 function 中使用反应钩? - How to use a react hook in an arrow function? 为什么我必须在 React 事件处理程序中放置一个箭头 Function 而不仅仅是返回值? - Why Must I Put An Arrow Function Instead of Just the Return Value in React Event Handler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM