Aja*_*mar 0 javascript tdd reactjs jestjs enzyme
我是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);
Run Code Online (Sandbox Code Playgroud)
我的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}
/>
);
};
Run Code Online (Sandbox Code Playgroud)
我想测试Age组件的ageValueCallBack函数,但我没有得到任何特定的方法到达那里.
任何见解都会有所帮助.
谢谢..
使用酶,您可以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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3989 次 |
| 最近记录: |