使用酶

时间:2016-11-03 10:21:40

标签: reactjs jasmine enzyme

我在理解酶的浅层渲染时遇到了问题。

我有一个组件WeatherApplication,它有一个子组件CitySelectionCitySelection收到属于selectedCity州的属性WeatherApplication

组件:

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

我毫不犹豫地测试了CitySeleciton是否存在,并且selectedCity是“汉堡”并且正确的功能被传递。

现在我想测试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具有正确的值。但我无法通过道具获取它。

1 个答案:

答案 0 :(得分:6)

<?php define("ARTS_PIC_TEMP_THUMBNAIL_PATH","uploads/"); $uploaded_file_name = $_SERVER['HTTP_X_FILE_NAME']; $opt = uploadingArts($uploaded_file_name); function uploadingArts($filename=''){ //$filename=($filename); $path_of = ARTS_PIC_TEMP_THUMBNAIL_PATH.$filename; $dest_dir = ARTS_PIC_TEMP_THUMBNAIL_PATH; $brwName = getBrowser(); if($brwName=='Internet Explorer'){ //for IE $content = file_get_contents('php://input'); //the string we get contains 2 parts $content = str_replace(' ','+',$content); // we need to replace spaces by plusses $content = split(",",$content); // we do not need the first part of the string (the one with the file type). $content = base64_decode($content[1]); file_put_contents($dest_dir.''.$filename, $content); return true; } else { //other than IE // read contents from the input stream $inputHandler = fopen('php://input', "r"); $fileHandler = fopen($path_of, "w+"); // save data from the input stream while(true) { //4096 $buffer = fgets($inputHandler, 1096); if (strlen($buffer) == 0) { fclose($inputHandler); fclose($fileHandler); return true; } fwrite($fileHandler, $buffer); } } } ?> 之后调用wrapper.update()应该可以解决问题:

selectCity()
相关问题