
import { expectSaga } from "redux-saga-test-plan";

import {
  editTemplate,
  v2SmsEditSagas,
} from "../sagas";
import * as api from "../../../../services/api";
import * as types from '../constants';
import { call } from "redux-saga/effects";
import { throwError } from "redux-saga-test-plan/providers";

describe("v2SmsEditSagas Combined", () => {
    it.concurrent("should initialize all v2SmsEditSagas-related watcher sagas without error", () => {
      return expectSaga(v2SmsEditSagas).run();
    });
  });

  describe('editTemplate Saga', () => {
    const template = { id: 1, name: 'Updated Template',message: 'esd' };

    it.concurrent('handles successful template edit', () => {
      const fakeResponse = {
        response: template,
        status: { code: 200 }
      };

      return expectSaga(editTemplate, template)
        .provide([
          [call(api.editTemplate, template), fakeResponse]
        ])
        .put({
          type: types.EDIT_TEMPLATE_SUCCESS,
          data: fakeResponse.response,
          statusCode: 200,
          errorMsg: undefined
        })
        .run();
    });

    it.concurrent('handles successful template edit for empty response', () => {
      const fakeResponse = {
        status: { code: 200 }
      };

      return expectSaga(editTemplate, template)
        .provide([
          [call(api.editTemplate, template), fakeResponse]
        ])
        .put({
          type: types.EDIT_TEMPLATE_SUCCESS,
          data: fakeResponse.response,
          statusCode: 200,
          errorMsg: undefined
        })
        .run();
    });

    it.concurrent('handles failure template edit for empty response', () => {
      const fakeResponse = {
        status: { code: 500 }
      };

      return expectSaga(editTemplate, template)
        .provide([
          [call(api.editTemplate, template), fakeResponse]
        ])
        .put({
          type: types.EDIT_TEMPLATE_SUCCESS,
          data: undefined,
          statusCode: 500,
          errorMsg: undefined
        })
        .put({
          type: types.EDIT_TEMPLATE_FAILURE,
          error: new TypeError('template.onUpdateTemplateComplete is not a function'),
          errorMsg: undefined
        })
        .run();
    });

    it.concurrent('handles failure in editing a template', () => {
      const error = new Error('Edit failed');

      return expectSaga(editTemplate, template)
        .provide([
          [call(api.editTemplate, template), throwError(error)]
        ])
        .put({
          type: types.EDIT_TEMPLATE_FAILURE,
          error,
          errorMsg: undefined
        })
        .run();
    });
  });