import moment from 'moment';
import commonModule from '../';
import WsDatePickerController from './ws-date-picker.controller';

describe('wsDatePickerComponent', () => {
  let outerScope;
  let element;

  beforeEach(() => {
    angular.mock.module(commonModule);
    angular.mock.module('ws.common');
  });

  function injectElement(elementHtml) {
    beforeEach(angular.mock.inject(($rootScope, $compile) => {
      outerScope = $rootScope.$new();
      element = angular.element(elementHtml);
      $compile(element)(outerScope);
      outerScope.$digest();
    }));
  }

  const mock$timeout = fn => Promise.resolve(fn());
  const mockWsDatePickerService = {
    getWeekendDates: () => [
      '2016/12/31',
      '2017/01/01',
      '2017/01/07',
      '2017/01/08',
      '2017/01/14',
      '2017/01/15',
      '2017/01/21',
      '2017/01/22',
    ],
    getDatesFromNowToN: () => [
      '2017/10/06',
      '2017/10/07',
      '2017/10/08',
      '2017/10/09',
      '2017/10/10',
      '2017/10/11',
      '2017/10/12',
      '2017/10/13',
    ],
    getNextAvailableDate: () => 'Sat Oct 07 2017 11:13:55 GMT-0400',
    convertDisplayInputDateToServerFormat: () => '2017-10-07',
  };

  describe('initial state', () => {
    injectElement('<ws-date-picker></ws-date-picker>');
    it('there should be one datepicker', () => {
      const datePickers = element[0].querySelectorAll('datepicker');
      expect(datePickers.length).toBe(1);
    });
    it('there should be one input', () => {
      const datePickers = element[0].querySelectorAll('input');
      expect(datePickers.length).toBe(1);
    });
  });

  describe('create the controller', () => {
    it('with minDate and maxDate', () => {
      const controller = new WsDatePickerController(mock$timeout, mockWsDatePickerService);
      controller.minDate = '2017-01-01';
      controller.maxDate = '2017-01-25';
      controller.$onInit();
      const expectedMinDate = moment('2017-01-01', 'YYYY-MM-DD');
      const expectedMaxDate = moment('2017-01-25', 'YYYY-MM-DD');
      const actualMinDate = moment(controller.model.minDate);
      const actualMaxDate = moment(controller.model.maxDate);
      expect(actualMinDate.isSame(expectedMinDate, 'day')).toBe(true);
      expect(actualMaxDate.isSame(expectedMaxDate, 'day')).toBe(true);
    });


    it('with minYear and maxYear', () => {
      const controller = new WsDatePickerController(mock$timeout, mockWsDatePickerService);
      controller.minYear = '2015';
      controller.maxYear = '2018';
      controller.$onInit();
      const expectedMinDate = moment('2015-01-01', 'YYYY-MM-DD');
      const expectedMaxDate = moment('2018-12-31', 'YYYY-MM-DD');
      const actualMinDate = moment(controller.model.minDate);
      const actualMaxDate = moment(controller.model.maxDate);
      expect(actualMinDate.isSame(expectedMinDate, 'day')).toBe(true);
      expect(actualMaxDate.isSame(expectedMaxDate, 'day')).toBe(true);
    });
  });
});
