2017-10-13 27 views
0

私はTDDに新しいですし、私は私の年齢構成要素で私のコールバック関数をテストしたい: 私Age.jsファイルは以下の通りです:ReactJSのjestと酵素を使用してコールバック関数をテストしますか?

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} 
    /> 
); 
}; 

私は年齢構成要素のageValueCallBack機能をテストしたいが、私は、任意の特定の方法が到達するまで届きません。

洞察力が役立ちます。

おかげ..酵素で

+0

はあなたがスパイを必要とすることを、sinonJsは、その仕事のために良いです。 ですから、年齢・コンポーネントをレンダリングし、小道具selectAgeActionは=「sinonスパイ」。 はまた、あなたはあなたのTextFieldにonChangeEventを行い、その後、sinonスパイに対して主張する必要があります。 http://sinonjs.org/releases/v4.0.1/spies/ –

答えて

2

あなたがsimulate('change', {}, 'someString')を使用してTextFieldComponentonChangeイベントをトリガすることができます。 jest.fn()で作成したスパイするためにあなたのAge.jsニーズのselectAgeAction:あなたがしなければならない何

const selectAgeAction = jest.fn() 
const component = shallow(<Age selectAgeAction={selectAgeAction} />) 
component.find('TextField').simulate('change', {}, '10') 
expect(selectAgeAction).toHaveBeenCalledWith('10') 
関連する問題