2016-09-20 170 views
0

是新来的反应,并利用流量格局正在尝试运行单元测试的用玩笑的反应程序,阵营:的onChange不是一个函数

的应用程序,我已经写了一个组件“天气”和前移动到另一个我想写测试该组件,并采取TDD方法。

我的代码运行良好,但测试失败,此错误

TypeError: tree.props.onChange is not a function 

天气组件代码:

// @flow 

import React from 'react'; 
import api from '../api'; 
import weatherStore from '../stores/weatherStore'; 

//read from weather store 
let _getState =() => { 
    return {weather: weatherStore.returnWeather()}; 
}; 

export default class Weather extends React.Component { 



    constructor(proporties) { 
     super(proporties); 

     this._onChange = this._onChange.bind(this); 

     this.state = _getState(); 

    } 
    componentWillUnmount() { 
     weatherStore.removeListener('change', this._onChange); 
    } 
    componentDidMount() { 
     api.getWeather(); 
     weatherStore.on('change', this._onChange); 
    } 

    _onChange() { 
     this.setState(_getState()); 
    } 

    render() { 
     let weatherState = this.state.weather.map(weather => { 

      return <div key={weather.id} className="pull-right row"> 

         <div> 
          <span>{weather.main.temp} C</span><br /> 
         </div> 
        </div>; 
     }) 

     return <div>{weatherState}</div>; 
    } 
} 

当测试代码:

import React from 'react'; 
import Weather from '../js/components/Weather'; 
import renderer from 'react-test-renderer'; 

it('should render weather temp and city',()=> { 
    const component = renderer.create(
     <Weather /> 
    ); 

    let tree = component.toJSON(); 
    expect(tree).toMatchSnapshot(); 

    // manually trigger the callback 
    tree.props._onChange(); 
    // re-rendering 
    tree = component.toJSON(); 
    expect(tree).toMatchSnapshot(); 

}); 

注意,因为我使用流动静态型检查器它总是高亮此行代码this._onChange = this._onChange.bind(this);

带有错误消息说:属性'_onChange'属性没有在天气中找到。

+0

当console.log(tree)''时,你会得到什么? –

回答

1

TypeError: tree.props.onChange is not a function

你有没有试着用更新的测试:

// manually trigger the callback 
tree.props._onChange(); 

注意仪表。

+0

是的,我已更新测试相同的错误。我已经更新了答案 – user3462064

+1

能否请您发布的 '的console.log结果(JSON.stringify(tree.props,空,2));' 请把下面的期望(树)线 –

+0

输出这一权利是空对象{} – user3462064

相关问题