如何测试 React with Enzyme 中子组件回调调用的组件回调?
How to test component callback invoked by child component callback in React with Enzyme?
假设我有以下应用程序:
class Child extends React.Component {
render() {
return <button onClick={this.handleChildOnClick}>{this.props.children}</button>;
}
handleChildOnClick() {
this.props.onChildClick('foo');
}
}
class Parent extends React.Component {
render() {
return <Child onChildClick={this.handleParentOnClick}>click me</Child>;
}
handleParentOnClick(text) {
this.props.onParentClick(12345);
}
}
class App extends React.Component {
render() {
return <Parent onParentClick={(num) => console.log(num)} />;
}
}
我很难找到测试 Parent
组件的正确方法。 Child
和 App
没有问题,但是 Parent
...
我的意思是,如何测试单击 Child
组件是否会调用 Parent
单击回调,而无需:
- ...渲染
Child
。 Parent
应该作为浅渲染单独测试。 Child
也将单独进行测试,如果我进行挂载渲染,我基本上是在 Child
上测试两次点击回调。
- ...直接在
Parent
实例上调用 handleParentOnClick
。我不应该为此依赖 Parent
的确切实现。如果我更改回调函数的名称,测试将中断并且很可能是误报。
还有第三种选择吗?
我想这可以给你一些想法。
// 组件
class Child extends React.Component {
render() {
return <button onClick={this.handleChildOnClick} className="t-btn">{this.props.children}</button>;
}
handleChildOnClick() {
this.props.onChildClick('foo');
}
}
// 测试
import { spy, stub } from 'sinon';
import { shallow } from 'enzyme';
describe('Child Component', () => {
it('should check handle click', () => {
spy(Child.prototype, 'handleChildOnClick');
const onChildClick = stub();
const wrapper = shallow(<Child onChildClick={onChildClick}>);
wrapper.find(".t-btn").simulate('click');
expect(Child.prototype.handleChildOnClick.callCount).to.equal(1);
});
it('should check onChildClick', () => {
const onChildClick = stub();
const wrapper = shallow(<Child onChildClick={onChildClick}>);
wrapper.find(".t-btn").simulate('click');
expect(onChildClick.callCount).to.equal(1);
});
});
用子组件测试父组件
import { stub } from 'sinon';
import { shallow } from 'enzyme';
import Child from '../Components/Child';
describe('Parent Component', () => {
it('should check handle click', () => {
const onParentClick = stub();
const wrapper = shallow(<Parent onParentClick={onParentClick} />);
wrapper.find(".t-btn").simulate('click');
expect(Child.prototype.handleChildOnClick.callCount).to.equal(1);
});
it('should check onChildClick', () => {
const onChildClick = stub();
const wrapper = shallow(<Child onChildClick={onChildClick}>);
wrapper.find(Child).prop('onChildClick')('foo');
expect(onParentClick.callCount).to.be.equal(1);
});
});
以上代码只处理了一个组件,但我希望这能给你一些要点。抱歉,如果语法有任何问题..
在测试 Parent
时,您可以:
import { shallow } from 'enzyme';
import { stub } from 'sinon';
describe('<Parent/>', () => {
it('should handle a child click', () => {
const onParentClick = stub();
const wrapper = shallow(<Parent onParentClick={onParentClick} />);
wrapper.find("Child").prop('onChildClick')('foo');
expect(onParentClick.callCount).to.be.equal(1);
// You can also check if the 'foo' argument was passed to onParentClick
});
});
假设我有以下应用程序:
class Child extends React.Component {
render() {
return <button onClick={this.handleChildOnClick}>{this.props.children}</button>;
}
handleChildOnClick() {
this.props.onChildClick('foo');
}
}
class Parent extends React.Component {
render() {
return <Child onChildClick={this.handleParentOnClick}>click me</Child>;
}
handleParentOnClick(text) {
this.props.onParentClick(12345);
}
}
class App extends React.Component {
render() {
return <Parent onParentClick={(num) => console.log(num)} />;
}
}
我很难找到测试 Parent
组件的正确方法。 Child
和 App
没有问题,但是 Parent
...
我的意思是,如何测试单击 Child
组件是否会调用 Parent
单击回调,而无需:
- ...渲染
Child
。Parent
应该作为浅渲染单独测试。Child
也将单独进行测试,如果我进行挂载渲染,我基本上是在Child
上测试两次点击回调。 - ...直接在
Parent
实例上调用handleParentOnClick
。我不应该为此依赖Parent
的确切实现。如果我更改回调函数的名称,测试将中断并且很可能是误报。
还有第三种选择吗?
我想这可以给你一些想法。
// 组件
class Child extends React.Component {
render() {
return <button onClick={this.handleChildOnClick} className="t-btn">{this.props.children}</button>;
}
handleChildOnClick() {
this.props.onChildClick('foo');
}
}
// 测试
import { spy, stub } from 'sinon';
import { shallow } from 'enzyme';
describe('Child Component', () => {
it('should check handle click', () => {
spy(Child.prototype, 'handleChildOnClick');
const onChildClick = stub();
const wrapper = shallow(<Child onChildClick={onChildClick}>);
wrapper.find(".t-btn").simulate('click');
expect(Child.prototype.handleChildOnClick.callCount).to.equal(1);
});
it('should check onChildClick', () => {
const onChildClick = stub();
const wrapper = shallow(<Child onChildClick={onChildClick}>);
wrapper.find(".t-btn").simulate('click');
expect(onChildClick.callCount).to.equal(1);
});
});
用子组件测试父组件
import { stub } from 'sinon';
import { shallow } from 'enzyme';
import Child from '../Components/Child';
describe('Parent Component', () => {
it('should check handle click', () => {
const onParentClick = stub();
const wrapper = shallow(<Parent onParentClick={onParentClick} />);
wrapper.find(".t-btn").simulate('click');
expect(Child.prototype.handleChildOnClick.callCount).to.equal(1);
});
it('should check onChildClick', () => {
const onChildClick = stub();
const wrapper = shallow(<Child onChildClick={onChildClick}>);
wrapper.find(Child).prop('onChildClick')('foo');
expect(onParentClick.callCount).to.be.equal(1);
});
});
以上代码只处理了一个组件,但我希望这能给你一些要点。抱歉,如果语法有任何问题..
在测试 Parent
时,您可以:
import { shallow } from 'enzyme';
import { stub } from 'sinon';
describe('<Parent/>', () => {
it('should handle a child click', () => {
const onParentClick = stub();
const wrapper = shallow(<Parent onParentClick={onParentClick} />);
wrapper.find("Child").prop('onChildClick')('foo');
expect(onParentClick.callCount).to.be.equal(1);
// You can also check if the 'foo' argument was passed to onParentClick
});
});