在 moxios 中指定 response.request.responseURL
Specify response.request.responseURL in moxios
我目前正在尝试编写一些功能,需要在 axios 处理响应时设置 response.request.responseURL
值。但是,我似乎无法用 moxios 设置这个值。这是我的测试结果:
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();
});
});
});
这是我的实际应用程序的样子:
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;
}
}
}
);
},
但是,当我记录这个值是什么时,它只显示 undefined
。这就是实际的 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}
这会破坏我的应用程序,因为应该始终定义 responseURL
。看起来它正在覆盖我在 moxios 中定义的请求。这不一定是坏事,因为我认为它需要这些东西才能正常运行。但是,如果我可以为此请求添加一些值,那就太好了。
所以;如何在 moxios 中向此请求添加 responseURL
值?
由于我将它链接到 respondWith()
的方式,所以无法设置 responseURL
。在查看 moxios 源代码中的这个函数后,我发现你在这里传递的值实际上只与返回的实际数据有关,而不是实际的响应对象。
我还看到 respondWith()
函数 returns 一个 Promise
对象,这很合乎逻辑。我认为我不应该在 Promise
上设置任何内容,而是根据实际请求设置。因此,我将返回的 Promise
分配给了 promise
变量,并取消了测试的 .then()
部分。之后我设置了请求的responseURL
。然后我再次调用 promise.then()
断言我的测试已经通过。
这是我最终的目标:
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();
});
});
});
我目前正在尝试编写一些功能,需要在 axios 处理响应时设置 response.request.responseURL
值。但是,我似乎无法用 moxios 设置这个值。这是我的测试结果:
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();
});
});
});
这是我的实际应用程序的样子:
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;
}
}
}
);
},
但是,当我记录这个值是什么时,它只显示 undefined
。这就是实际的 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}
这会破坏我的应用程序,因为应该始终定义 responseURL
。看起来它正在覆盖我在 moxios 中定义的请求。这不一定是坏事,因为我认为它需要这些东西才能正常运行。但是,如果我可以为此请求添加一些值,那就太好了。
所以;如何在 moxios 中向此请求添加 responseURL
值?
由于我将它链接到 respondWith()
的方式,所以无法设置 responseURL
。在查看 moxios 源代码中的这个函数后,我发现你在这里传递的值实际上只与返回的实际数据有关,而不是实际的响应对象。
我还看到 respondWith()
函数 returns 一个 Promise
对象,这很合乎逻辑。我认为我不应该在 Promise
上设置任何内容,而是根据实际请求设置。因此,我将返回的 Promise
分配给了 promise
变量,并取消了测试的 .then()
部分。之后我设置了请求的responseURL
。然后我再次调用 promise.then()
断言我的测试已经通过。
这是我最终的目标:
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();
});
});
});