2017-11-09 5 views
0

ドロップダウンメニューのonChangeイベントによってトリガされる私のhandleChange関数をテストします。そのメニューに関する反応構造を以下に示します。メソッドをテストする方法は、酵素で一度呼び出されます

class StationNavigationBar extends React.Component { 
handleChange = (event, index, value) => { 
    this.props.onStationChange(value); 
    } 

render() { 
    return (
<DropDownMenu value={this.props.initial} onChange={this.handleChange}> 
       { this.props.stations.map((station, index) => 
       <MenuItem key={station.id} value={station.id} primaryText={station.name} /> 
      )} 
      </DropDownMenu> 
); 
} 
} 

私はそのhandleChange機能をスパイする酵素を使用しようとしましたが、ここに私の現在のテストコードは、スパイ機能はonChangeイベントを検出することができないようです。

describe("when `dropDownMenu` is changed",() => { 
    it("onStationChange should be called",() => { 
    let props = { 
     initial: 'Something', 
     title: 'something', 
     onStationChange:()=>{}, 
     onLogOut:()=>{}, 
     stations: [{ 
     id: '1', 
     name: "Station 2" }, 
     { id: '2', 
     name: "Station 3" }, 
     { id: '3', 
     name: 'Station 4'}] 
    }; 
    const stationNavigationBar = shallow(<StationNavigationBar {...props} />); 
    const instance = stationNavigationBar.instance(); 
    const goSpy = sinon.spy(instance, 'handleChange'); 
    stationNavigationBar.update(); 
    const toolbarGroup = stationNavigationBar.find('ToolbarGroup'); 
    const dropDownMenu = toolbarGroup.find('DropDownMenu'); 
    dropDownMenu.simulate('change'); 
    expect(goSpy.calledOnce).toBe(true); 
    }); 
    }); 

私が注意しなかった部分はありますか?私のテストケースでは、どの部分が欠けていましたか。

+0

@OriDrori、私はその部分を置くべきsinon – Darren

+0

は、私はすでに – Darren

答えて

1

スパイメソッドを作成し、スパイをonStationChangeプロップとして渡します。

const onStationChange = sinon.spy(); 

const props = { 
    initial: 'Something', 
    title: 'something', 
    onStationChange, 
    //... 
}; 
+0

いいえ、私が試したが、それはしなかった私は、constのgoSpy = sinon.spy(例えば、「handleChange」)を持っている私のコードで考えます仕事 – Darren

+0

今すぐお試しください。スパイを変更するのを忘れました。スパイはメソッドとして渡され、ラッパーとして渡されません。 –

+0

はい、今動作します。ありがとう! – Darren

関連する問題