UNPKG

1.61 kBJavaScriptView Raw
1import React, {Component, PropTypes} from 'react'
2import { shallow, mount, render } from 'enzyme';
3
4
5class Counter extends Component {
6 handleClick () {
7 var click = this.props.clickCount;
8 this.props.handleClick({
9 type : 'CHANGE_CLICK',
10 clickCount : click + 1
11 })
12 return true;
13 }
14 render () {
15 this.testMethod();
16 return (<h2 className="aa" onClick={this.handleClick.bind(this)}>Click me! Number of clicks: {this.props.clickCount}</h2>);
17 }
18
19 componentDidMount() {
20 console.log('1111')
21 }
22
23 testMethod () {
24 console.log('testMethod')
25 }
26}
27
28
29
30describe('Counter Test', ()=> {
31
32 // text检查 渲染:render
33 it('render correct', () => {
34 const warpper = render(<Counter clickCount={0}/>);
35 expect(warpper.text()).toEqual("Click me! Number of clicks: 0");
36 })
37
38 //事件
39 it('click execute correct', () => {
40 const onButtonClick = jasmine.createSpy('111');
41 const wrapper = shallow(<Counter handleClick={onButtonClick} clickCount={0}/>);
42 wrapper.simulate('click');
43 expect(onButtonClick.calls.count()).toEqual(1);
44 })
45
46
47 //prototype的方法是否执行
48 it('prototype function execute correct', () => {
49 spyOn(Counter.prototype, "testMethod");
50 mount(<Counter clickCount={0}/>);
51 expect(Counter.prototype.testMethod).toHaveBeenCalled();
52
53 })
54
55 //生命周期监测,需要用mount
56 //生命周期
57 it('componentDidMount execute correct', () => {
58 spyOn(Counter.prototype, "componentDidMount");
59 mount(<Counter clickCount={0}/>);
60 expect(Counter.prototype.componentDidMount).toHaveBeenCalled();
61 })
62
63})
\No newline at end of file