import { fromJS } from 'immutable';
import emailReducer from '../reducer';
import * as types from '../constants';

describe('emailReducer', () => {
  it.concurrent('it returns the initial state', () => {
    expect(emailReducer(undefined, {})).toMatchSnapshot();
  });

  it.concurrent('it handles the TRANSFORM_EMAIL_TEMPLATE_REQUEST action ', () => {
    const action = {
      type: types.TRANSFORM_EMAIL_TEMPLATE_REQUEST,
      template: {},
      callback: ()=>{},
    };
    expect(emailReducer(undefined, action)).toMatchSnapshot();
  });

  it.concurrent('it handles GET_CMS_EDITOR_DETAILS_SUCCESS action — sets isBeeEnabled true and CmsSettings', () => {
    const initialState = fromJS({ isBeeEnabled: null, fetchingCmsSettings: true });
    const action = {
      type: types.GET_CMS_EDITOR_DETAILS_SUCCESS,
      isBeeEnabled: true,
      settings: { isDragDrop: true, editorType: 'bee' },
    };
    const result = emailReducer(initialState, action);
    expect(result.get('fetchingCmsSettings')).toBe(false);
    expect(result.get('isBeeEnabled')).toBe(true);
    expect(result.getIn(['CmsSettings', 'isDragDrop'])).toBe(true);
  });

  it.concurrent('it handles GET_CMS_EDITOR_DETAILS_SUCCESS action — sets isBeeEnabled false', () => {
    const initialState = fromJS({ isBeeEnabled: true, fetchingCmsSettings: true });
    const action = {
      type: types.GET_CMS_EDITOR_DETAILS_SUCCESS,
      isBeeEnabled: false,
      settings: {},
    };
    const result = emailReducer(initialState, action);
    expect(result.get('fetchingCmsSettings')).toBe(false);
    expect(result.get('isBeeEnabled')).toBe(false);
  });

  it.concurrent('it handles GET_CMS_EDITOR_DETAILS_SUCCESS action — overwrites stale isBeeEnabled from CLEAR_ALL_VALUES (null)', () => {
    const initialState = fromJS({ isBeeEnabled: null, fetchingCmsSettings: true });
    const action = {
      type: types.GET_CMS_EDITOR_DETAILS_SUCCESS,
      isBeeEnabled: true,
      settings: { type: 'bee' },
    };
    const result = emailReducer(initialState, action);
    expect(result.get('isBeeEnabled')).toBe(true);
  });

  it.concurrent('it handles GET_CMS_EDITOR_DETAILS_SUCCESS action — sets CmsSettings to empty object when settings is {}', () => {
    const initialState = fromJS({ CmsSettings: { old: 'data' } });
    const action = {
      type: types.GET_CMS_EDITOR_DETAILS_SUCCESS,
      isBeeEnabled: false,
      settings: {},
    };
    const result = emailReducer(initialState, action);
    expect(result.get('CmsSettings').toJS()).toEqual({});
  });

  it.concurrent('it handles GET_CMS_ACCOUNTS_REQUEST action (line 111-113)', () => {
    const initialState = fromJS({
      isBeeEnabled: true, // Start with true to verify it gets set to false
    });
    const action = {
      type: types.GET_CMS_ACCOUNTS_REQUEST,
    };
    const result = emailReducer(initialState, action);
    expect(result.get('isBeeEnabled')).toBe(false);
  });

  it.concurrent('it handles GET_CMS_ACCOUNTS_SUCCESS action (line 114-116)', () => {
    const initialState = fromJS({
      isBeeEnabled: false,
    });
    const action = {
      type: types.GET_CMS_ACCOUNTS_SUCCESS,
      isBeeEnabled: true,
    };
    const result = emailReducer(initialState, action);
    expect(result.get('isBeeEnabled')).toBe(true);
  });

  it.concurrent('it handles GET_CMS_ACCOUNTS_SUCCESS action with false (line 114-116)', () => {
    const initialState = fromJS({
      isBeeEnabled: true,
    });
    const action = {
      type: types.GET_CMS_ACCOUNTS_SUCCESS,
      isBeeEnabled: false,
    };
    const result = emailReducer(initialState, action);
    expect(result.get('isBeeEnabled')).toBe(false);
  });

  it.concurrent('it handles GET_CMS_ACCOUNTS_FAILURE action (line 117-119)', () => {
    const initialState = fromJS({
      isBeeEnabled: true, // Start with true to verify it gets set to false
    });
    const action = {
      type: types.GET_CMS_ACCOUNTS_FAILURE,
    };
    const result = emailReducer(initialState, action);
    expect(result.get('isBeeEnabled')).toBe(false);
  });
});