[英]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.