我想在React with Tape上测试onClick事件。文档说你要做的就是在比较之前直接调用onClick事件回调。但是当我在比较之前调用handleSubmit事件时,我得到一个错误 - handleSubmit没有被定义。
import React, { Component, PropTypes } from 'react'
import {createRenderer} from 'react-addons-test-utils';
import tape from 'tape';
import addAssertions from 'extend-tape';
import jsxEquals from 'tape-jsx-equals';
const test = addAssertions(tape, {jsxEquals});
class MyComponent extends Component{
constructor(props) {
super(props)
this.state = {text: 'here is text'}
}
handleSubmit(e){
this.setState({text: 'new text'})
}
render() {
return (
<div>
{this.state.text}
<button onClick={(e) => this.handleSubmit(e)}>Update</button>
</div>
);
}
};
test('MyComponent is properly rendered', (t) => {
t.plan(1);
const text = 'here is text'
const renderer = createRenderer();
renderer.render(<MyComponent />);
const result = renderer.getRenderOutput();
handleSubmit();
t.jsxEquals(result, <div>{text}<button onClick={(e) => this.handleSubmit(e)}>Update</button></div>);
t.end();
});