Смоделируйте несколько вызовов API внутри одной функции, используя Moxios - PullRequest
0 голосов
/ 03 апреля 2020

Я пишу контрольный пример для своего класса обслуживания. Я хочу сделать несколько вызовов внутри одной функции, так как я делаю два вызова API из одной функции. Я попробовал следующее, но оно не работает

it('should get store info', async done => {
      const store: any = DealersAPIFixture.generateStoreInfo();
      moxios.wait(() => {
        const request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: store
        });
        const nextRequest = moxios.requests.at(1);
        nextRequest.respondWith({
          status: 200,
          response: DealersAPIFixture.generateLocation()
        });
      });
      const params = {
        dealerId: store.dealerId,
        storeId: store.storeId,
        uid: 'h0pw1p20'
      };
      return DealerServices.retrieveStoreInfo(params).then((data: IStore) => {
        const expectedOutput = DealersFixture.generateStoreInfo(data);
        expect(data).toMatchObject(expectedOutput);
      });
    });

const nextRequest всегда неопределено

оно выдает ошибку TypeError: Cannot read property 'respondWith' of undefined

вот мой класс обслуживания

static async retrieveStoreInfo(
    queryParam: IStoreQueryString
  ): Promise<IStore> {
    const res = await request(getDealerStoreParams(queryParam));
    try {
      const locationResponse = await graphQlRequest({
        query: locationQuery,
        variables: { storeId: res.data.storeId }
      });
      res.data['inventoryLocationCode'] =
        locationResponse.data?.location?.inventoryLocationCode;
    } catch (e) {
      res.data['inventoryLocationCode'] = 'N/A';
    }
    return res.data;
  }
...