"use strict";

var _react = _interopRequireDefault(require("react"));

var _enzyme = _interopRequireWildcard(require("enzyme"));

var _luxon = require("luxon");

var _enzymeAdapterReact = _interopRequireDefault(require("enzyme-adapter-react-16"));

var uut = _interopRequireWildcard(require("../"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

_enzyme.default.configure({
  adapter: new _enzymeAdapterReact.default()
});

describe('DatePicker', function () {
  test('basic functionality works', function () {
    var expectedRange = ['2019-02-02', '2019-02-04'];

    var parseDate = function parseDate(d) {
      return _luxon.DateTime.fromFormat(d, 'yyyy-MM-dd').toJSDate();
    };

    var expectedMinDate = '2016-01-01';
    var expectedMaxDate = '2020-01-01';
    var calendarSelectionReset = false;

    var onCancel = function onCancel() {
      calendarSelectionReset = true;
    };

    var applied = false;

    var apply = function apply() {
      applied = true;
    };

    var selectedNamedRanges = ['custom'];
    var selectedRanges = [expectedRange];
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(uut.DatePicker, {
      selectedRanges: selectedRanges,
      selectedNamedRanges: selectedNamedRanges,
      cancelButtonText: "Cancel",
      applyButtonText: "Apply",
      selectableRange: [expectedMinDate, expectedMaxDate],
      updateCalendarRanges: function updateCalendarRanges(v) {
        selectedRanges = v;
      },
      updateNamedCalendarRanges: function updateNamedCalendarRanges(v) {
        selectedNamedRanges = v;
      },
      onApply: apply,
      onCancel: onCancel,
      sidebarConfig: [uut.sidebarSection(uut.sectionTitle('Predefined Range'), uut.sectionRange('today', 'Today', [_luxon.DateTime.fromJSDate(new Date()).toFormat('yyyy-MM-dd'), _luxon.DateTime.fromJSDate(new Date()).toFormat('yyyy-MM-dd')]), uut.sectionRange('tomorrow', 'Tomorrow', [_luxon.DateTime.fromJSDate(new Date()).plus({
        days: 1
      }).toFormat('yyyy-MM-dd'), _luxon.DateTime.fromJSDate(new Date()).plus({
        days: 1
      }).toFormat('yyyy-MM-dd')]), uut.sectionRange('custom', 'Custom', null))]
    }));
    expect(wrapper.find('DatePickerSidebar h4').text()).toEqual('Predefined Range');
    expect(wrapper.find('DatePickerSidebar li').map(function (v) {
      return v.text();
    })).toEqual(['Today', 'Tomorrow', 'Custom']);
    expect(wrapper.find('button.vs-apply-button').text()).toEqual('Apply');
    expect(wrapper.find('button.vs-cancel-button').text()).toEqual('Cancel');
    expect(wrapper.find('DatePickerSidebar .custom input').prop('checked')).toBeTruthy();
    expect(wrapper.find('DateRange').props()).toMatchObject({
      minDate: uut._internal.fromInternalDateFormat(expectedMinDate).toJSDate(),
      maxDate: uut._internal.fromInternalDateFormat(expectedMaxDate).toJSDate(),
      ranges: [expect.objectContaining({
        startDate: parseDate(expectedRange[0]),
        endDate: parseDate(expectedRange[1]),
        range1: expectedRange
      })]
    });
    wrapper.find('DatePickerSidebar .today').simulate('click');
    expect(selectedNamedRanges).toEqual(['today']);
    wrapper.find('DatePickerSidebar .tomorrow input').simulate('change');
    expect(selectedNamedRanges).toEqual(['tomorrow']);
    expect(selectedRanges).toMatchObject([[_luxon.DateTime.fromJSDate(new Date()).plus({
      days: 1
    }).toFormat('yyyy-MM-dd'), _luxon.DateTime.fromJSDate(new Date()).plus({
      days: 1
    }).toFormat('yyyy-MM-dd')]]);
    wrapper.find('button.vs-cancel-button').simulate('click');
    expect(calendarSelectionReset).toBeTruthy();
    wrapper.find('button.vs-apply-button').simulate('click');
    expect(applied).toBeTruthy();
  });
  test('computed ranges and transition from single-calendar', function () {
    var initialBaseRange = ['2019-02-02', '2019-02-04'];
    var initialComparisonRange = ['2017-04-12', '2017-04-14'];
    var expectedComparisonRange = ['2018-02-02', '2018-02-04'];

    var parseDate = function parseDate(d) {
      return _luxon.DateTime.fromFormat(d, 'yyyy-MM-dd');
    };

    var selectableRange = ['2016-01-01', '2020-01-01'];
    var selectedNamedRanges = ['custom', 'none'];
    var selectedRanges = [initialBaseRange, initialComparisonRange];
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(uut.DatePicker, {
      selectedRanges: selectedRanges,
      selectedNamedRanges: selectedNamedRanges,
      selectableRange: selectableRange,
      updateCalendarRanges: function updateCalendarRanges(v) {
        selectedRanges = v;
      },
      updateNamedCalendarRanges: function updateNamedCalendarRanges(v) {
        selectedNamedRanges = v;
      },
      onApply: function onApply() {},
      onCancel: function onCancel() {},
      sidebarConfig: [uut.sidebarSection(uut.sectionTitle('Base Range'), uut.sectionRange('a', 'b', [])), uut.sidebarSection(uut.sectionTitle('Predefined Range'), uut.sectionRange('last-year', 'Last Year', function (_ref) {
        var _ref2 = _slicedToArray(_ref, 2),
            base = _ref2[0],
            _ = _ref2[1];

        return [parseDate(base[0]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd'), parseDate(base[1]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd')];
      }))]
    }));
    expect.assertions(3);
    expect(wrapper.find('.vs-date-pickers').children()).toHaveLength(1);
    wrapper.find('.last-year').simulate('click'); // simulate reactive prop update, as component won't get new values by default

    wrapper.setProps({
      selectedNamedRanges: selectedNamedRanges,
      selectedRanges: selectedRanges
    }, function () {
      expect(wrapper.find('.vs-date-pickers').children()).toHaveLength(2);
      expect(selectedRanges).toEqual([initialBaseRange, expectedComparisonRange]);
    });
  });
  test('applying with "none" selected excludes the corresponding range', function () {
    var initialBaseRange = ['2019-02-02', '2019-02-04'];
    var initialComparisonRange = ['2017-04-12', '2017-04-14'];

    var parseDate = function parseDate(d) {
      return _luxon.DateTime.fromFormat(d, 'yyyy-MM-dd');
    };

    var selectableRange = ['2016-01-01', '2020-01-01'];
    var selectedNamedRanges = ['custom', 'none'];
    var selectedRanges = [initialBaseRange, initialComparisonRange];
    var appliedRanges = null;
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(uut.DatePicker, {
      selectedRanges: selectedRanges,
      selectedNamedRanges: selectedNamedRanges,
      selectableRange: selectableRange,
      updateCalendarRanges: function updateCalendarRanges(_) {},
      updateNamedCalendarRanges: function updateNamedCalendarRanges(_) {},
      onApply: function onApply(v) {
        appliedRanges = v;
      },
      onCancel: function onCancel() {},
      sidebarConfig: [uut.sidebarSection(uut.sectionTitle('Base Range'), uut.sectionRange('a', 'b', [])), uut.sidebarSection(uut.sectionTitle('Predefined Range'), uut.sectionRange('last-year', 'Last Year', function (_ref3) {
        var _ref4 = _slicedToArray(_ref3, 2),
            base = _ref4[0],
            _ = _ref4[1];

        return [parseDate(base[0]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd'), parseDate(base[1]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd')];
      }))]
    }));
    expect(wrapper.find('.vs-date-pickers').children()).toHaveLength(1);
    wrapper.find('button.vs-apply-button').simulate('click');
    expect(appliedRanges).toEqual([initialBaseRange]);
  });
});