我的组件应该在用户登录时检索位置列表 .
我正在使用 fetch-mock 来模拟获取请求并返回组件在安装时将要使用的一些数据 .

class Comp extends Component {

    componentDidMount() {
        this.getLocations();
    }

    getLocations = () => {
        const url = 'the-url';

        fetch(url)
            .then((response) => {
                return response.json()
            })
            .then(json => {
                // json = {"value": [{"id": 0, "name": "Test Location"}]
                this.setState({
                    locations: json.value
                })
            })
            .catch(err => console.log('getLocations err', err))
    };
}

我已经记录了json,输入是在注释中定义的 .

我得到的错误是 getLocations err TypeError: Cannot read property 'createEvent' of undefined .

在callstack底部给出的行是 this.setState({locations: json.value}); ,而logging this 按预期显示了react组件的实例 .

谁能帮我这个?

技术:

  • 反应16.2.0

  • 开玩笑21.2.1

  • react-test-renderer 16.2.0

  • babel-jest 21.2.0