如何使用 jest 和 enzyme 测试 children 的 React 组件?
How to test React component with children using jest and enzyme?
要测试的组件。 this.props.children 有 child 个组成 example.js
class Example extends component {
constructor(){
super(props);
this.state={};
}
render() {
<div>{this.props.children}</div>
}
}
测试用例
it("should render Example with children", () => {
const wrapper = shallow(<Example {...props} />);
expect(wrapper.find("div").text()).toBe(""); //
});
如何测试 child 个组件通过了?
谢谢。
您可以像下面这样使用
describe('Parent Component', () => {
it('renders Child component', () => {
const wrapper = shallow(<Parent store={store} />);
expect(wrapper.find(Child).length).toEqual(1);
});
});
此外,如果您只想测试子组件 div
像这样使用
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('Foo')).to.have.lengthOf(1);
要测试的组件。 this.props.children 有 child 个组成 example.js
class Example extends component {
constructor(){
super(props);
this.state={};
}
render() {
<div>{this.props.children}</div>
}
}
测试用例
it("should render Example with children", () => {
const wrapper = shallow(<Example {...props} />);
expect(wrapper.find("div").text()).toBe(""); //
});
如何测试 child 个组件通过了?
谢谢。
您可以像下面这样使用
describe('Parent Component', () => {
it('renders Child component', () => {
const wrapper = shallow(<Parent store={store} />);
expect(wrapper.find(Child).length).toEqual(1);
});
});
此外,如果您只想测试子组件 div 像这样使用
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('Foo')).to.have.lengthOf(1);