加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react 单元测试,模拟操作后数据的变化

发布时间:2020-12-15 07:34:35 所属栏目:百科 来源:网络整理
导读:参考文档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 { exp

参考文档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');

    });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读