"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.sectionRange('same-period', 'Same Period')), 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')];
      }), uut.sectionRange('opposite-ends', 'Opposite Ends', function (_ref3) {
        var _ref4 = _slicedToArray(_ref3, 2),
            base = _ref4[0],
            comparison = _ref4[1];

        return [base[0], comparison[1]];
      }), uut.sectionRange('base-or-comparison', 'Base or Comparison', function (_ref5, _ref6) {
        var _ref7 = _slicedToArray(_ref5, 2),
            base = _ref7[0],
            comparison = _ref7[1];

        var _ref8 = _slicedToArray(_ref6, 1),
            baseId = _ref8[0];

        if (baseId === 'same-period') {
          return base;
        } else {
          return comparison;
        }
      }))]
    }));
    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('computed ranges can use both previous ranges', function () {
    var initialBaseRange = ['2019-02-02', '2019-02-04'];
    var initialComparisonRange = ['2017-04-12', '2017-04-14'];
    var expectedComparisonRange = ['2019-02-02', '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 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.sectionRange('same-period', 'Same Period')), uut.sidebarSection(uut.sectionTitle('Predefined Range'), uut.sectionRange('last-year', 'Last Year', function (_ref9) {
        var _ref10 = _slicedToArray(_ref9, 2),
            base = _ref10[0],
            _ = _ref10[1];

        return [parseDate(base[0]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd'), parseDate(base[1]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd')];
      }), uut.sectionRange('opposite-ends', 'Opposite Ends', function (_ref11) {
        var _ref12 = _slicedToArray(_ref11, 2),
            base = _ref12[0],
            comparison = _ref12[1];

        return [base[0], comparison[1]];
      }), uut.sectionRange('base-or-comparison', 'Base or Comparison', function (_ref13, _ref14) {
        var _ref15 = _slicedToArray(_ref13, 2),
            base = _ref15[0],
            comparison = _ref15[1];

        var _ref16 = _slicedToArray(_ref14, 1),
            baseId = _ref16[0];

        if (baseId === 'same-period') {
          return base;
        } else {
          return comparison;
        }
      }))]
    }));
    expect.assertions(1);
    wrapper.find('.opposite-ends').simulate('click'); // simulate reactive prop update, as component won't get new values by default

    wrapper.setProps({
      selectedNamedRanges: selectedNamedRanges,
      selectedRanges: selectedRanges
    }, function () {
      expect([selectedRanges, selectedNamedRanges]).toEqual([[initialBaseRange, expectedComparisonRange], ['custom', 'opposite-ends']]);
    });
  });
  test('computed ranges can depend on the names of the selected ranges', 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 = ['a', '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.sectionRange('same-period', 'Same Period')), uut.sidebarSection(uut.sectionTitle('Predefined Range'), uut.sectionRange('last-year', 'Last Year', function (_ref17) {
        var _ref18 = _slicedToArray(_ref17, 2),
            base = _ref18[0],
            _ = _ref18[1];

        return [parseDate(base[0]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd'), parseDate(base[1]).minus({
          years: 1
        }).toFormat('yyyy-MM-dd')];
      }), uut.sectionRange('opposite-ends', 'Opposite Ends', function (_ref19) {
        var _ref20 = _slicedToArray(_ref19, 2),
            base = _ref20[0],
            comparison = _ref20[1];

        return [base[0], comparison[1]];
      }), uut.sectionRange('base-or-comparison', 'Base or Comparison', function (_ref21, _, _ref22) {
        var _ref23 = _slicedToArray(_ref21, 2),
            base = _ref23[0],
            comparison = _ref23[1];

        var _ref24 = _slicedToArray(_ref22, 1),
            baseId = _ref24[0];

        if (baseId === 'same-period') {
          return base;
        } else {
          return comparison;
        }
      }))]
    }));
    wrapper.find('.base-or-comparison').simulate('click');
    expect(selectedNamedRanges).toEqual(['a', 'base-or-comparison']);
    expect(selectedRanges).toEqual([initialBaseRange, initialComparisonRange]); // simulate reactive prop update, as component won't get new values by default

    wrapper.setProps({
      selectedNamedRanges: selectedNamedRanges,
      selectedRanges: selectedRanges
    }, function () {
      wrapper.find('.same-period').simulate('click');
      expect(selectedRanges).toEqual([initialBaseRange, initialBaseRange]);
    });
  });
  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 (_ref25) {
        var _ref26 = _slicedToArray(_ref25, 2),
            base = _ref26[0],
            _ = _ref26[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]);
  });
  test('applying sends the selected ranges and named ranges to onApply', 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', 'tomorrow'];
    var selectedRanges = [initialBaseRange, initialComparisonRange];
    var expectedNamedRanges = ['custom', 'tomorrow'];
    var appliedRanges = null;
    var appliedNamedRanges = 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, n) {
        appliedRanges = v;
        appliedNamedRanges = n;
      },
      onCancel: function onCancel() {},
      sidebarConfig: [uut.sidebarSection(uut.sectionTitle('Base Range'), uut.sectionRange('a', 'b', [])), uut.sidebarSection(uut.sectionTitle('Predefined Range'), uut.sectionRange('tomorrow', 'Last Year', function (_ref27) {
        var _ref28 = _slicedToArray(_ref27, 2),
            base = _ref28[0],
            _ = _ref28[1];

        return [parseDate(base[0]).plus({
          days: 1
        }).toFormat('yyyy-MM-dd'), parseDate(base[1]).plus({
          days: 1
        }).toFormat('yyyy-MM-dd')];
      }))]
    }));
    wrapper.find('DatePickerSidebar .tomorrow input').simulate('change');
    wrapper.find('button.vs-apply-button').simulate('click');
    expect(appliedRanges).toMatchObject([initialBaseRange, initialComparisonRange]);
    expect(appliedNamedRanges).toEqual(expectedNamedRanges);
  });
});