我是TDD新手,我想在Age组件中测试我的回调函数: 我的 Age.js 文件如下:
import React, { Component } from "react";
import { connect } from "react-redux";
import actions from "../../actions";
import TextFieldComponent from "../Module/TextFieldComponent";
export class Age extends Component {
ageValueCallBack = age => {
console.log("value is : ", age);
this.props.selectAgeAction(age)
};
render() {
const props = {
onChange: this.ageValueCallBack,
hintText : 'Eg. 25',
floatingLabelText: "Age(Years)",
value : (this.props.usersData) ? this.props.usersData.basic.age : null
};
return <TextFieldComponent {...props} />;
}
}
function mapStateToProps({ usersData }) {
return {
usersData
};
}
export default connect(mapStateToProps, {
selectAgeAction: actions.selectAgeValue
})(Age);
TextFieldComponent 所在的位置:
import TextField from "material-ui/TextField";
const TextFieldComponent = props => {
return (
<TextField
onChange={(event, string) => {
props.onChange(string)
}}
floatingLabelText={props.floatingLabelText || "floatingLabelText"}
value={props.value || null}
hintText={props.hintText || "hintText will be here"}
autoFocus ={true || props.autoFocus}
/>
);
};
我想测试Age组件的 ageValueCallBack 功能,但我没有得到任何特定方法到达那里。
任何见解都会有所帮助。
谢谢..
答案 0 :(得分:5)
使用酶,您可以使用onChange
触发TextFieldComponent
上的simulate('change', {}, 'someString')
事件。您selectAgeAction
中的Age.js
需要成为使用jest.fn()
创建的间谍:
const selectAgeAction = jest.fn()
const component = shallow(<Age selectAgeAction={selectAgeAction} />)
component.find('TextField').simulate('change', {}, '10')
expect(selectAgeAction).toHaveBeenCalledWith('10')