简体   繁体   English

具有重复参数的 Axios 模拟适配器

[英]Axios Mock Adapter with repeated params

I am am using a mock adapter for my tests in a react app, and one of them looks like this:我在 React 应用程序中使用模拟适配器进行测试,其中一个看起来像这样:

http://endpoint.com/api/entities?id=123&select=labels&select=versions&labelsLang=en

The important part to note is that the select parameter is in there twice.需要注意的重要部分是select参数在那里两次。

One of the tests is rendering in another language, so we have two mock endpoints set up to reflect this, however I cannot seem to find a way to properly mock the repeated paramter.其中一个测试是用另一种语言呈现的,所以我们设置了两个模拟端点来反映这一点,但是我似乎找不到一种方法来正确模拟重复的参数。 I just keep on getting a result for the first one.我只是继续得到第一个结果。

The code for the mocked endpoints is this:模拟端点的代码是这样的:

const mockApiClient = axios.create({ baseURL: "http://localhost" });
const mock = new MockAdapter(mockApiClient);

const params1 = new URLSearchParams();
params1.append("id", "123");
params1.append("select", "labels");
params1.append("select", "versions");
params1.set("labelsLang", "en");
mock
  .onGet("/entities", {
    asymmetricMatch: function(actual: any) {
      return actual.params.toString() === params1.toString();
    },
  })
  .reply(200, getCompanyResponse);

const params2 = new URLSearchParams();
params2.append("id", "123");
params2.append("select", "labels");
params2.append("select", "versions");
params2.set("labelsLang", "de");
mock
  .onGet("/entities", {
    asymmetricMatch: function(actual: any) {
      return actual.params.toString() === params2.toString();
    },
  })
  .reply(200, getCompanyResponseDE);

I know it's messy, I just want to understand how to do this properly.我知道这很麻烦,我只想了解如何正确执行此操作。

Whenever I try specifying specific params in an object, it complains that you cant have a duplicate key .每当我尝试在对象中指定特定参数时,它都会抱怨您不能拥有重复的键。

(ie. { params:{select:'labels', select:'versions} } ) (即{ params:{select:'labels', select:'versions} }

Solved.解决了。

Here's how it was done:这是如何完成的:

const mockApiClient = axios.create({ baseURL: "http://localhost" });
const mock = new MockAdapter(mockApiClient);

const params1 = {
  "id": "123",
  select: ["labels", "versions"],
  "labelsLang": "en",
};
mock
  .onGet("/entities", {
    params: {
      asymmetricMatch: function(actual: any) {
        actual.sort(); // Important, as without it the order of params would affect the result
        return actual.toString() === toURLSearchParams(params1).toString();
      },
    },
  })
  .reply(200, getCompanyResponse);


export const toURLSearchParams = (params: {[key: string]: string|string[]}, sort:boolean = true):URLSearchParams => {
  if(params instanceof URLSearchParams) return params

  const searchParams = new URLSearchParams();
  for(const key in params){
    const value = params[key];
    if(Array.isArray(value)){
      value.forEach((eachValue) => {
        searchParams.append(key, eachValue)
      })
    } else {
      searchParams.append(key,value)
    }
  }
  if(sort) searchParams.sort() // Likewise here. 
  return searchParams;
}

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

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