简体   繁体   English

在Moxios中指定response.request.responseURL

[英]Specify response.request.responseURL in moxios

I'm currently trying to write some functionality that requires the response.request.responseURL value to be set when axios handles the response. 我目前正在尝试编写一些功能,要求axios处理响应时需要设置response.request.responseURL值。 However, I can't seem to set this value with moxios. 但是,我似乎无法使用moxios设置此值。 Here's what my test looks like: 这是我的测试结果:

it('resets list selection if input contents were replaced', (done) => {
    component.currently_selected_list_value = 10;
    component.last_called_url = 'henkiehoutman';

    let input = domElt.querySelector('input');
    ReactTestUtils.Simulate.change(input);

    moxios.wait(() => {
        let request = moxios.requests.mostRecent();
        request.respondWith({
            status: 200,
            response: [{
                customer_info_customers: ['', '', '', '', ''],
                domain_info_customers: {},
            }],
            request: {responseURL: 'banaan'}
        }).then(() => {
            // The contents were replaced, so it's best to forget whatever our selection was.
            expect(component.currently_selected_list_value).toEqual(-1);
            done();
        });
    });
});

This is what my actual application looks like: 这是我的实际应用程序的外观:

onChange (event) {
    return axios.get(
        this.props.ApiUrl + encodeURIComponent(event.target.value)
    ).then(
        (response) => {
            let response_url = response.request.responseURL;  // This is the value I want to set manually.
            if(this.shouldHandleResponse(response_url)){
                this.last_called_url = response_url;
                let data = response.data;
                this.setState({results: data, dropdown: data.length > 0});

                if ( this.currently_selected_list_value > max_index) {
                    this.currently_selected_list_value = max_index;
                }
            }
        }
    );
},

However, when I log what this value is, it just says undefined . 但是,当我记录该值是什么时,它只是说undefined And this is what the actual response.request value is (put it in a quote so it's a bit more readable): 这就是实际的response.request值(将其放在引号中,以便于阅读):

LOG: Request{resolve: function (a) { ... }, reject: function (a) { ... }, config: Object{adapter: function mockAdapter(config) { ... }, transformRequest: Object{0: ...}, transformResponse: Object{0: ...}, timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: function validateStatus(status) { ... }, headers: Object{Accept: ..., X-CSRFToken: ...}, method: 'get', url: 'my_api_url/', data: undefined}, headers: Object{Accept: 'application/json, text/plain, / ', X-CSRFToken: 'my_csrf_token'}, url: 'my_api_url/', timeout: 0, withCredentials: false, responseType: undefined} 日志:请求{解决:函数(a){...},拒绝:函数(a){...},配置:对象{适配器:函数嘲笑适配器(config){...},transformRequest:对象{0 :...},transformResponse:对象{0:...},超时:0,xsrfCookieName:“ XSRF-TOKEN”,xsrfHeaderName:“ X-XSRF-TOKEN”,maxContentLength:-1,validateStatus:函数validateStatus(status ){...},标头:Object {Accept:...,X-CSRFToken:...},方法:“ get”,URL:“ my_api_url /”,数据:undefined},标头:Object {Accept: 'application / json,text / plain, / ',X-CSRFToken:'my_csrf_token'},url:'my_api_url /',超时:0,withCredentials:false,responseType:undefined}

This breaks my application, because a responseURL should always be defined. 这打断了我的应用程序,因为应始终定义responseURL It looks like it's overwriting the request I defined in moxios. 看来它正在覆盖我在moxios中定义的请求。 This is not necessarily a bad thing, because I assume it needs these things to function properly. 这不一定是一件坏事,因为我认为它需要这些东西才能正常运行。 However, it would be nice if I could add some values to this request. 但是,如果我可以向此请求中添加一些值,那就太好了。

So; 所以; How do I add a responseURL value to this request in moxios? 如何在moxios中向此请求添加responseURL值?

Turns out that it's not possible to set the responseURL because of the way I chained it to respondWith() . 事实证明,由于我将其链接到respondWith()的方式,因此无法设置responseURL After checking this function in the moxios source code I saw that the values you pass in here really only have to do with actual data being returned, not an actual response object. 在moxios源代码中检查了此功能后,我看到您在此处传递的值实际上仅与返回的实际数据有关,而与实际的响应对象无关。

I also saw that the respondWith() function returns a Promise object, which is quite logical. 我还看到了respondWith()函数返回一个Promise对象,这很合逻辑。 I figured that I should not set anything on the Promise , but on the actual request. 我认为我不应该在Promise上设置任何内容,而应在实际请求上设置任何内容。 So I assigned the returned Promise to a promise variable and unchained the .then() part of the test. 因此,我将返回的Promise分配给了promise变量,并取消了测试的.then()部分的链接。 I set the request's responseURL after that. 之后,我设置了请求的responseURL Then I called promise.then() again to assert that my test has passed. 然后,我再次调用promise.then()以断言我的测试已通过。

Here is what I eventually went for: 这是我最终追求的目标:

it('resets list selection if input contents were replaced', (done) => {
    component.currently_selected_list_value = 10;
    component.last_called_url = 'henkiehoutman';

    let input = domElt.querySelector('input');
    ReactTestUtils.Simulate.change(input);

    moxios.wait(() => {
        let request = moxios.requests.mostRecent();
        let promise = request.respondWith({
            status: 200,
            response: [{
                customer_info_customers: ['', '', '', '', ''],
                domain_info_customers: {},
            }],
        });
        request.responseURL = 'banaan';

        promise.then(() => {
            // The contents were replaced, so it's best to forget whatever our selection was.
            expect(component.currently_selected_list_value).toEqual(-1);
            done();
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM