import { expectSaga, testSaga } from "redux-saga-test-plan";
import * as matchers from "redux-saga-test-plan/matchers";
import { takeLatest } from "redux-saga/effects";
import { getTemplateInfoById, watchForGetTemplateInfoById,v2ZaloSagas } from "../saga";
import {
  ZALO_TEMPLATE_INFO_SUCCESS,
  ZALO_TEMPLATE_INFO_ERROR,
  ZALO_TEMPLATE_INFO_REQUEST,
  ZALO_TEMPLATE_PREVIEW_INFO,
} from "../constants";
import * as Api from "../../../services/api";


  describe("v2ZaloSagas Combined", () => {
    it("should initialize all ebill-related watcher sagas without error", () => {
      return expectSaga(v2ZaloSagas).run();
    });
});
describe("zalo saga", () => {
  const error = new Error("error");
  const actionCallback = jest.fn();
  describe("zalo test saga", () => {
    it("handle success response from getTemplateInfoById", () => {
      expectSaga(getTemplateInfoById, {
        payload: {
          id: 10234,
          username: "capillary_zns",
          oa_id: "kfhksda7326kjs",
          token: "tyfdusafhdsk783687342678",
          actionCallback
        },
      })
        .provide([
          [
            matchers.call.fn(Api.getTemplateInfoById),
            {
              success: true,
              result: [],
            },
          ],
        ])
        .put({
          type: ZALO_TEMPLATE_INFO_SUCCESS,
          result: {},
        })
        .run();
    });

    it("handle success response from getTemplateInfoById for preview", () => {
      expectSaga(getTemplateInfoById, {
        payload: {
          id: 10234,
          username: "capillary_zns",
          oa_id: "kfhksda7326kjs",
          token: "tyfdusafhdsk783687342678",
          preview: true,
          actionCallback
        },
      })
        .provide([
          [
            matchers.call.fn(Api.getTemplateInfoById),
            {
              success: true,
              result: [],
            },
          ],
        ])
        .put({
          type: ZALO_TEMPLATE_PREVIEW_INFO,
          result: {},
        })
        .run();
    });

    it("handle success response from getTemplateInfoById when response is not present", () => {
      expectSaga(getTemplateInfoById, {
        payload: {},
      })
        .provide([
          [
            matchers.call.fn(Api.getTemplateInfoById),
            {
              success: true,
            },
          ],
        ])
        .put({
          type: ZALO_TEMPLATE_INFO_SUCCESS,
        })
        .run();
    });
    it("handle failure response from getTemplateInfoById", () => {
      expectSaga(getTemplateInfoById, {
        payload: {actionCallback},
      })
        .provide([
          [
            matchers.call.fn(Api.getTemplateInfoById),
            {
              success: false,
            },
          ],
        ])
        .put({
          type: ZALO_TEMPLATE_INFO_ERROR,
        })
        .run();
    });

    it("handles error thrown", () => {
      testSaga(getTemplateInfoById, {payload: {actionCallback}}).next().throw(error).next().isDone();
    });
  });

  describe("watchForGetTemplateInfoById saga", () => {
    const generator = watchForGetTemplateInfoById();
    it("should call watchers functions", async () => {
      expect(generator.next().value).toEqual(
        takeLatest(ZALO_TEMPLATE_INFO_REQUEST, getTemplateInfoById)
      );
    });
  });
});

