参考文档https://github.com/airbnb/enzyme/issues/341
http://www.ruanyifeng.com/blog/2016/02/react-testing-tutorial.html
1.引入enzyme 的mount
2.引入jsdom
3.调用simulate 模拟操作
4.断言结果是否是预期
import {mount} from 'enzyme';
import { expect } from 'chai';
import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import jsdom from 'jsdom'
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.document = doc
global.window = doc.defaultview
class App extends Component {
constructor(props) {
super(props)
this.state = {
count: 1
}
}
render() {
return <h1 onClick={()=>{ this.setState({
count: this.state.count + 1
}) }}>{this.state.count}</h1>
}
}
function shalloWrender(Component) {
const renderer = TestUtils.createRenderer();
renderer.render(<Component/>);
return renderer.getRenderOutput();
}
describe('Shallow Rendering',function () {
it('countTest',function () {
let app = mount(<App/>);
app.find('h1').simulate('click');
expect(app.find('h1').text()).to.equal('2');
});
});
原文地址:https://www.jb51.cc/react/304877.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。