import { fromJS } from "immutable";
import rcsReducer from "../reducer";
import * as types from "../constants";

describe("rcs reducer", () => {
  it("UPLOAD_RCS_ASSET_REQUEST", () => {
    const state = fromJS({});
    const action = { type: types.UPLOAD_RCS_ASSET_REQUEST };
    const expected = state.set("uploadAssetSuccess", false).set("assetUploading", true);
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("UPLOAD_RCS_ASSET_SUCCESS", () => {
    const state = fromJS({});
    const action = {
      type: types.UPLOAD_RCS_ASSET_SUCCESS,
      statusCode: 200,
      data: { foo: "bar" },
      index: 1,
    };
    const expected = state
      .set("uploadAssetSuccess", true)
      .set("assetUploading", false)
      .set("uploadedAssetData1", { foo: "bar" });
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("UPLOAD_RCS_ASSET_SUCCESS with undefined statusCode", () => {
    const state = fromJS({});
    const action = {
      type: types.UPLOAD_RCS_ASSET_SUCCESS,
      data: { foo: "bar" },
      index: 1,
    };
    const expected = state
      .set("uploadAssetSuccess", false)
      .set("assetUploading", false)
      .set("uploadedAssetData1", { foo: "bar" });
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("UPLOAD_RCS_ASSET_SUCCESS with empty statusCode", () => {
    const state = fromJS({});
    const action = {
      type: types.UPLOAD_RCS_ASSET_SUCCESS,
      statusCode: "",
      data: { foo: "bar" },
      index: 1,
    };
    const expected = state
      .set("uploadAssetSuccess", false)
      .set("assetUploading", false)
      .set("uploadedAssetData1", { foo: "bar" });
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("UPLOAD_RCS_ASSET_SUCCESS with statusCode >= 300", () => {
    const state = fromJS({});
    const action = {
      type: types.UPLOAD_RCS_ASSET_SUCCESS,
      statusCode: 400,
      data: { foo: "bar" },
      index: 1,
    };
    const expected = state
      .set("uploadAssetSuccess", false)
      .set("assetUploading", false)
      .set("uploadedAssetData1", { foo: "bar" });
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("UPLOAD_RCS_ASSET_SUCCESS without index (defaults to 0)", () => {
    const state = fromJS({});
    const action = { type: types.UPLOAD_RCS_ASSET_SUCCESS, statusCode: 200, data: { foo: "bar" } };
    const expected = state
      .set("uploadAssetSuccess", true)
      .set("assetUploading", false)
      .set("uploadedAssetData0", { foo: "bar" });
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("UPLOAD_RCS_ASSET_FAILURE", () => {
    const state = fromJS({});
    const action = { type: types.UPLOAD_RCS_ASSET_FAILURE };
    const expected = state.set("uploadAssetSuccess", false).set("assetUploading", false).set("uploadAssetError", true);
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CLEAR_RCS_ASSET", () => {
    const state = fromJS({ uploadedAssetData: { foo: "bar" } });
    const action = { type: types.CLEAR_RCS_ASSET };
    const expected = state.delete("uploadedAssetData");
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CLEAR_RCS_MEDIA_ASSET", () => {
    const state = fromJS({ uploadedAssetData1: { foo: "bar" } });
    const action = { type: types.CLEAR_RCS_MEDIA_ASSET, index: 1 };
    const expected = state.delete("uploadedAssetData1");
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CLEAR_RCS_MEDIA_ASSET without index (defaults to 0)", () => {
    const state = fromJS({ uploadedAssetData0: { foo: "bar" } });
    const action = { type: types.CLEAR_RCS_MEDIA_ASSET };
    const expected = state.delete("uploadedAssetData0");
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CREATE_RCS_TEMPLATE_REQUEST", () => {
    const state = fromJS({});
    const action = { type: types.CREATE_RCS_TEMPLATE_REQUEST };
    const expected = state.set("createTemplateInProgress", true).set("createTemplateError", false).set("createTemplateErrorMessage", fromJS(""));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CREATE_RCS_TEMPLATE_SUCCESS", () => {
    const state = fromJS({});
    const action = {
      type: types.CREATE_RCS_TEMPLATE_SUCCESS,
      statusCode: 200,
      data: { foo: "bar" },
      errorMsg: "",
    };
    const expected = state
      .set("createTemplateInProgress", false)
      .set("response", { foo: "bar" })
      .set("createTemplateError", false)
      .set("createTemplateErrorMessage", fromJS(""));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CREATE_RCS_TEMPLATE_SUCCESS with statusCode > 300", () => {
    const state = fromJS({});
    const action = {
      type: types.CREATE_RCS_TEMPLATE_SUCCESS,
      statusCode: 400,
      data: { foo: "bar" },
      errorMsg: "error occurred",
    };
    const expected = state
      .set("createTemplateInProgress", false)
      .set("response", { foo: "bar" })
      .set("createTemplateError", true)
      .set("createTemplateErrorMessage", fromJS("error occurred"));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CREATE_RCS_TEMPLATE_SUCCESS with undefined statusCode", () => {
    const state = fromJS({});
    const action = { type: types.CREATE_RCS_TEMPLATE_SUCCESS, data: { foo: "bar" }, errorMsg: "error occurred" };
    const expected = state
      .set("createTemplateInProgress", false)
      .set("response", { foo: "bar" })
      .set("createTemplateError", false)
      .set("createTemplateErrorMessage", fromJS("error occurred"));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CREATE_RCS_TEMPLATE_FAILURE", () => {
    const state = fromJS({});
    const action = { type: types.CREATE_RCS_TEMPLATE_FAILURE, errorMsg: "fail" };
    const expected = state.set("createTemplateInProgress", false).set("createTemplateError", true).set("createTemplateErrorMessage", fromJS("fail"));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CLEAR_CREATE_RESPONSE_REQUEST", () => {
    const state = fromJS({ response: { foo: "bar" } });
    const action = { type: types.CLEAR_CREATE_RESPONSE_REQUEST };
    const expected = state.set("response", {});
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("GET_RCS_TEMPLATE_DETAILS_REQUEST", () => {
    const state = fromJS({});
    const action = { type: types.GET_RCS_TEMPLATE_DETAILS_REQUEST };
    const expected = state.set("getTemplateDetailsInProgress", true);
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("GET_RCS_TEMPLATE_DETAILS_SUCCESS", () => {
    const state = fromJS({});
    const action = { type: types.GET_RCS_TEMPLATE_DETAILS_SUCCESS, data: { foo: "bar" } };
    const expected = state.set("getTemplateDetailsInProgress", false).set("templateDetails", { foo: "bar" });
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("GET_RCS_TEMPLATE_DETAILS_FAILURE", () => {
    const state = fromJS({});
    const action = { type: types.GET_RCS_TEMPLATE_DETAILS_FAILURE };
    const expected = state.set("getTemplateDetailsInProgress", false);
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("EDIT_RCS_TEMPLATE_REQUEST", () => {
    const state = fromJS({});
    const action = { type: types.EDIT_RCS_TEMPLATE_REQUEST };
    const expected = state.set("editTemplateInProgress", true).set("editTemplateError", false).set("editTemplateErrorMessage", fromJS(""));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("EDIT_RCS_TEMPLATE_SUCCESS", () => {
    const state = fromJS({});
    const action = {
      type: types.EDIT_RCS_TEMPLATE_SUCCESS,
      statusCode: 200,
      data: { foo: "bar" },
      errorMsg: "",
    };
    const expected = state
      .set("editTemplateInProgress", false)
      .set("editResponse", { foo: "bar" })
      .set("editTemplateError", false)
      .set("editTemplateErrorMessage", fromJS(""));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("EDIT_RCS_TEMPLATE_SUCCESS with statusCode > 300", () => {
    const state = fromJS({});
    const action = {
      type: types.EDIT_RCS_TEMPLATE_SUCCESS,
      statusCode: 400,
      data: { foo: "bar" },
      errorMsg: "edit error occurred",
    };
    const expected = state
      .set("editTemplateInProgress", false)
      .set("editResponse", { foo: "bar" })
      .set("editTemplateError", true)
      .set("editTemplateErrorMessage", fromJS("edit error occurred"));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("EDIT_RCS_TEMPLATE_SUCCESS with undefined statusCode", () => {
    const state = fromJS({});
    const action = { type: types.EDIT_RCS_TEMPLATE_SUCCESS, data: { foo: "bar" }, errorMsg: "edit error occurred" };
    const expected = state
      .set("editTemplateInProgress", false)
      .set("editResponse", { foo: "bar" })
      .set("editTemplateError", false)
      .set("editTemplateErrorMessage", fromJS("edit error occurred"));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("EDIT_RCS_TEMPLATE_FAILURE", () => {
    const state = fromJS({});
    const action = { type: types.EDIT_RCS_TEMPLATE_FAILURE, errorMsg: "fail" };
    const expected = state.set("editTemplateInProgress", false).set("editTemplateError", true).set("editTemplateErrorMessage", fromJS("fail"));
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("CLEAR_EDIT_RESPONSE_REQUEST", () => {
    const state = fromJS({ templateDetails: { foo: "bar" } });
    const action = { type: types.CLEAR_EDIT_RESPONSE_REQUEST };
    const expected = state.set("templateDetails", {});
    expect(rcsReducer(state, action)).toEqual(expected);
  });

  it("returns state for unknown action", () => {
    const state = fromJS({ foo: "bar" });
    const action = { type: "UNKNOWN_ACTION" };
    expect(rcsReducer(state, action)).toEqual(state);
  });
});
