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

describe('galleryReducer', () => {
  describe('Initial State should match snapshot', () => {
    it.concurrent('returns the initial state', () => {
      expect(galleryReducer(undefined, {})).toMatchSnapshot();
    });
  });

  describe('Request for all assets', () => {
    const initialState = fromJS({
      assetList: [],
      fetchingAllAssets: false,
      fetchAllAssetSuccess: false,
    });
    const requestAction = actions.getAllAssets('IMAGE');
    const stateOnRequest = galleryReducer(initialState, requestAction);
    const successStateExpected = {
      assetList: [
        {
          _id: '5678fdghfh',
        },
        {
          _id: '5678fdghfh5',
        },
      ],
      fetchingAllAssets: false,
      fetchAllAssetSuccess: true,
    };
    const successAction = {
      type: types.GET_ALL_ASSETS_SUCCESS,
      data: {
        assets: [
          {
            _id: '5678fdghfh',
          },
          {
            _id: '5678fdghfh5',
          },
        ],
      },
    };
    const stateOnSuccess = galleryReducer(initialState, successAction);
    const failureAction = {
      type: types.GET_ALL_ASSETS_FAILURE,
      error: 'Some error Occurred',
    };
    const stateOnFailure = galleryReducer(initialState, failureAction);
    it.concurrent('should set fetching_schedule_message to true on request', () => {
      expect(stateOnRequest.toJS()).toEqual({ assetList: [], fetchingAllAssets: true, fetchAllAssetSuccess: false });
    });

    it.concurrent('should set assetList from response on success', () => {
      expect(stateOnSuccess.toJS()).toEqual(successStateExpected);
    });
    it.concurrent('should set fetching_schedule_message to false on GET_ALL_ASSETS_FAILURE', () => {
      expect(stateOnFailure.toJS()).toEqual({assetList: [], fetchingAllAssets: false, fetchAllAssetSuccess: false});
    });
  });

  describe('Upload Assets', () => {
    const initialState = fromJS({
      assetUploading: false,
    });
    const requestAction = actions.uploadAsset({}, 'IMAGE', {});
    const stateOnRequest = galleryReducer(initialState, requestAction);
    const successStateExpected = {
      assetUploading: false,
      uploadAssetSuccess: false,
      uploadedAssetData: undefined,
    };
    const successAction = {
      type: types.UPLOAD_ASSET_SUCCESS,
    };
    const stateOnSuccess = galleryReducer(initialState, successAction);
    const failureAction = {
      type: types.UPLOAD_ASSET_FAILURE,
      error: 'Some error Occurred',
    };
    const stateOnFailure = galleryReducer(initialState, failureAction);
    it.concurrent('should set assetUploading to true and uploadAssetSuccess to false on request', () => {
      expect(stateOnRequest.toJS()).toEqual({ uploadAssetSuccess: false, assetUploading: true });
    });

    it.concurrent('should set assetUploading to false and uploadAssetSuccess to true on success', () => {
      expect(stateOnSuccess.toJS()).toEqual(successStateExpected);
    });
    it.concurrent('should set assetUploading to false and uploadAssetSuccess to false on UPLOAD_ASSET_FAILURE', () => {
      expect(stateOnFailure.toJS()).toEqual({ uploadAssetSuccess: false, assetUploading: false });
    });
  });
});
