Функция устранения неполадок при использовании Redux-Saga - PullRequest
0 голосов
/ 25 июня 2019

У меня есть сага, которая вызывает функцию, которая вызывает API.API еще не известен, поэтому мои действия в настоящее время выглядят так:

// @flow
export function* createCustomerSaga({
  customer: cust
}: CreateArgs): Saga<void> {
  try {
    const { customer, error } = createCustomerApi(cust);
    if (customer) {
      yield put({
        type: "NEW_CUSTOMER_SUCCESS",
        customer: Customer.fromApi(customer)
      });
    } else if (error) {
      yield put({
        type: "NEW_CUSTOMER_FAILURE",
        error
      });
    }
  } catch (error) {
    yield put({
      type: "NEW_CUSTOMER_FAILURE",
      error
    });
  }
}

export function createCustomerApi(customer: Customer): Object {
  console.log("hello from original function");
  // Create an API-friendly payload
  const payload = Customer.toApi(customer);
  // Post using the online API and return the result
  // currently a dummy response (success)
  return { customer: payload };
}

Я пытаюсь смоделировать сбой API:

  describe("integration", () => {
    let sagaStore;
    beforeEach(() => {
      sagaStore = new SagaTester({});
      sagaStore.start(customerSaga);
      jest.setTimeout(1000);
    });

    it("returns a failure action if the API call fails", async () => {
      const createCustomerApi = jest.fn();
      createCustomerApi.mockImplementation(() => {
        console.log("hello from mock function");
        return { error: "Something went wrong" };
      });
      sagaStore.dispatch(startAction);
      await sagaStore.waitFor(successAction.type);
      expect(createCustomerApi).toHaveBeenCalled();
      expect(sagaStore.getCalledActions()).toEqual([
        startAction,
        { type: "NEW_CUSTOMER_FAILURE", error: "Something went wrong" }
      ]);
    });
  });

Мои тесты не проходят на toHaveBeenCalled ожидание, и если я уберу эту строку, то они потерпят неудачу, вернув успешное действие вместо неудачи - мой макет не достигается.

Я все еще обнимаю насмешки и шпионов.Я успешно использовал некоторых шпионов где-то еще, но потому что эта функция не «включена» (то есть, у меня нет первого аргумента для jest.spyOn(Something, "createCustomerApi", fn)).

Как правильно это издеваться?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...