Javascript 在ReactJS中使用jest和Ezyme测试回调函数?
我是TDD新手,我想在my Age组件中测试我的回调函数: 我的Age.js文件如下:Javascript 在ReactJS中使用jest和Ezyme测试回调函数?,javascript,reactjs,tdd,jestjs,enzyme,Javascript,Reactjs,Tdd,Jestjs,Enzyme,我是TDD新手,我想在my 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
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);
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}
/>
);
};
import React,{Component}来自“React”;
从“react redux”导入{connect};
从“../../actions”导入操作;
从“./模块/TextFieldComponent”导入TextFieldComponent;
导出类年龄扩展组件{
ageValueCallBack=age=>{
log(“值为:”,年龄);
this.props.selectAgeAction(年龄)
};
render(){
常量道具={
onChange:this.ageValueCallBack,
hintText:“例如25”,
浮动标签文本:“年龄(年)”,
值:(this.props.usersData)?this.props.usersData.basic.age:null
};
返回;
}
}
函数mapstateTrops({usersData}){
返回{
用户数据
};
}
导出默认连接(MapStateTops{
selectAgeAction:actions.selectAgeValue
})(年龄);
其中我的TextFieldComponent如下所示:
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);
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}
/>
);
};
从“物料界面/TextField”导入TextField;
const TextFieldComponent=props=>{
返回(
{
道具更换(弦)
}}
floatingLabelText={props.floatingLabelText | |“floatingLabelText”}
value={props.value | | null}
hintText={props.hintText | |“hintText将在这里”}
自动对焦={true | | props.autoFocus}
/>
);
};
我想测试Age组件的ageValueCallBack函数,但我没有得到任何特定的方法
任何见解都会有所帮助
谢谢..使用酶,您可以使用触发
TextFieldComponent
上的onChange
事件。Age.js
中的selectAgeAction
必须是使用jest.fn()创建的间谍:
const selectAgeAction=jest.fn()
常量分量=浅()
component.find('TextField').simulate('change',{},'10'))
expect(selectAgeAction).与('10')一起调用
你需要做的是你需要一个间谍,sinonJs很适合这份工作。因此,在渲染年龄组件时,请使用prop selectAgeAction=“the sinon spy”。此外,您还需要在文本字段上生成onChangeEvent,然后对sinon间谍进行断言。这非常有用,您设置为{}
的simulate()
的第二个参数是什么?这将是调用回调的事件参数,通常类似于{target:value:'someValue'}