【发布时间】:2019-05-17 12:08:43
【问题描述】:
我已经基于 reactstrap 构建了一个组件,然后使用 jest 和 Enzyme 我无法测试模态的内容。让我们看看到目前为止我已经尝试过什么:
import React from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
class ModalExample extends React.Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
modal: !this.state.modal
});
}
render() {
return (
<div className="modal-testing">
<Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
<Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
<ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
<ModalBody className="inside">
I just want this to show up in unit test
Name: <input type="text" />
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={this.toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
export default ModalExample;
我的单元测试如下:
import React from 'react';
import {mount, ReactWrapper} from "enzyme";
import ModalExample from "./ModalExample";
const wrapper = mount(
<ModalExample isOpen={isOpen} toggle={toggle} />
);
const button = wrapper.find('Button').at(0);
button.simulate('click');
// What I have tried
expect(wrapper.text()).toBe('I just want this to show up in unit test');
expect(wrapper.find('input')).toHaveLength(1); // it gets failed
对了,这个方法我已经试过了,但是没用:
Similar issue posted on stackoverflow
但我不知道我做错了什么,如果有什么不对的地方,谁能发现任何错误或纠正我?
【问题讨论】:
标签: reactjs unit-testing jestjs enzyme reactstrap