我在理解酶的浅层渲染时遇到问题。
我有一个组件WeatherApplication
,它有一个子组件CitySelection
。该CitySelection
接收特性selectedCity
是保持在WeatherApplication
S状态。
组件:
export default class WeatherApplication extends React.Component {
constructor(props) {
super(props);
this.state = {
city : "Hamburg"
}
}
selectCity(value) {
this.setState({
city: value
});
}
render() {
return (
<div>
<CitySelection selectCity={this.selectCity.bind(this)} selectedCity={this.state.city} />
</div>
);
}
}
我 sussessfully 测试了 CitySeleciton 存在并且 selectedCity 是“Hamburg”并且通过了正确的函数。
现在我想测试 selectCity 方法的行为。
it("updates the temperature when the city is changed", () => {
var wrapper = shallow(<WeatherApplication/>);
wrapper.instance().selectCity("Bremen");
var citySelection = wrapper.find(CitySelection);
expect(citySelection.props().selectedCity).toEqual("Bremen");
});
这个测试失败了,因为 的值citySelection.props().selectedCity
仍然是汉堡。
我检查了再次调用的render
方法WeatherApplication
并this.state.city
具有正确的值。但是我无法通过props获取它。