如何使用模拟返回值调用模拟API

时间:2019-03-06 05:57:09

标签: jestjs

我正在测试saga,我被困在哪里以及如何调用模拟api,并断言它已被调用一次。当我使用mockReturnValue时如何调用模拟api?

在下面,我应该如何以及何时调用模拟api?

import { cloneableGenerator } from "redux-saga/utils";
import * as api from "../Utilities/api";

const generator = cloneableGenerator(fetchPostSaga)();

test("fetching posts successfully", () => {
  const clone = generator.clone();
  const posts = {};

  const mock = jest.spyOn(api, "default");
  const responseValue = {
    data: {
      tokenIsValid: true
    }
  };
  mock.mockReturnValueOnce({ data: responseValue });

  expect(clone.next().value).toEqual(call(// mock));
  expect(clone.next(posts).value).toEqual(put(postsReceived(responseValue)));
  expect(clone.next().done).toBe(true);
});

0 个答案:

没有答案