"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireDefault(require("react"));
var _react2 = require("../../utils/test-utils/react");
var _CalendarPanel = _interopRequireDefault(require("../CalendarPanel"));
var _CalendarPanelPrivateUni = require("./CalendarPanel.private.uni.driver");
var _isSameDay = _interopRequireDefault(require("date-fns/isSameDay"));
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/CalendarPanel/test/CalendarPanel.spec.jsx";
var A_DAY = new Date(2019, 4, 29);
var A_DAY_NOT_IN_PRESETS = new Date(2019, 4, 10);
var A_DAY_BEFORE = function () {
  var date = new Date(A_DAY);
  date.setDate(date.getDate() - 1);
  return date;
}();
var A_WEEK_BEFORE = function () {
  var date = new Date(A_DAY);
  date.setDate(date.getDate() - 7);
  return date;
}();
var A_WEEK_AFTER = function () {
  var date = new Date(A_DAY);
  date.setDate(date.getDate() + 7);
  return date;
}();
var TWO_MONTHS_BEFORE = function () {
  var date = new Date(A_DAY);
  date.setMonth(date.getDate() - 60);
  return date;
}();
var ONE_MONTH_BEFORE = function () {
  var date = new Date(A_DAY);
  date.setMonth(date.getDate() - 30);
  return date;
}();
var presets = [{
  id: 1,
  value: 'a day',
  selectedDays: {
    from: A_DAY,
    to: A_DAY
  }
}, {
  id: 2,
  value: 'a day before',
  selectedDays: {
    from: A_DAY_BEFORE,
    to: A_DAY_BEFORE
  }
}, {
  id: 3,
  value: 'a week before',
  selectedDays: {
    from: A_WEEK_BEFORE,
    to: A_DAY
  }
}, {
  id: 4,
  value: 'a week after',
  selectedDays: {
    from: A_DAY,
    to: A_WEEK_AFTER
  }
}, {
  id: 5,
  value: 'one month - two months before',
  selectedDays: {
    from: TWO_MONTHS_BEFORE,
    to: ONE_MONTH_BEFORE
  }
}];
describe('CalendarPanel', function () {
  describe('[async]', function () {
    runTests((0, _react2.createRendererWithUniDriver)(_CalendarPanelPrivateUni.calendarPanelPrivateUniDriverFactory));
  });
  function runTests(render) {
    var _this = this;
    afterEach(function () {
      return (0, _react2.cleanup)();
    });
    it('should show the calendar in two month view by default', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _render, driver;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _render = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
              onChange: function onChange() {
                return null;
              },
              presets: presets,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 75,
                columnNumber: 9
              }
            })), driver = _render.driver;
            _context.t0 = expect;
            _context.next = 4;
            return driver.calendarDriver();
          case 4:
            _context.next = 6;
            return _context.sent.getNumOfVisibleMonths();
          case 6:
            _context.t1 = _context.sent;
            (0, _context.t0)(_context.t1).toBe(2);
          case 8:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    it('should show the presets dropdown layout', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var _render2, driver;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _render2 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
              onChange: function onChange() {
                return null;
              },
              presets: presets,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 84,
                columnNumber: 9
              }
            })), driver = _render2.driver;
            _context2.t0 = expect;
            _context2.next = 4;
            return driver.presetsDropdownLayoutDriver();
          case 4:
            _context2.next = 6;
            return _context2.sent.exists();
          case 6:
            _context2.t1 = _context2.sent;
            (0, _context2.t0)(_context2.t1).toBe(true);
            _context2.t2 = expect;
            _context2.next = 11;
            return driver.presetsDropdownLayoutDriver();
          case 11:
            _context2.next = 13;
            return _context2.sent.isShown();
          case 13:
            _context2.t3 = _context2.sent;
            (0, _context2.t2)(_context2.t3).toBe(true);
          case 15:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    })));
    it('should show the correct presets', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
      var _render3, driver, optionsContent;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            _render3 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
              onChange: function onChange() {
                return null;
              },
              presets: presets,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 96,
                columnNumber: 9
              }
            })), driver = _render3.driver;
            _context3.t0 = expect;
            _context3.next = 4;
            return driver.presetsDropdownLayoutDriver();
          case 4:
            _context3.next = 6;
            return _context3.sent.optionsLength();
          case 6:
            _context3.t1 = _context3.sent;
            (0, _context3.t0)(_context3.t1).toBe(presets.length);
            _context3.next = 10;
            return driver.presetsDropdownLayoutDriver();
          case 10:
            _context3.next = 12;
            return _context3.sent.optionsContent();
          case 12:
            optionsContent = _context3.sent;
            presets.map(function (preset) {
              return preset.value;
            }).forEach(function (presetValue) {
              expect(optionsContent).toContain(presetValue);
            });
          case 14:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    })));
    describe('driver.getNumOfVisibleMonths()', function () {
      it('should return 2 also when showMonthDropdown=true', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
        var _render4, driver;
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              _render4 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
                showMonthDropdown: true,
                onChange: function onChange() {
                  return null;
                },
                presets: presets,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 114,
                  columnNumber: 11
                }
              })), driver = _render4.driver;
              _context4.t0 = expect;
              _context4.next = 4;
              return driver.calendarDriver();
            case 4:
              _context4.next = 6;
              return _context4.sent.getNumOfVisibleMonths();
            case 6:
              _context4.t1 = _context4.sent;
              (0, _context4.t0)(_context4.t1).toBe(2);
            case 8:
            case "end":
              return _context4.stop();
          }
        }, _callee4);
      })));
    });
    it('should call onChange with the selected preset dates', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
      var selectedPreset, onChange, _render5, driver;
      return _regenerator["default"].wrap(function _callee5$(_context5) {
        while (1) switch (_context5.prev = _context5.next) {
          case 0:
            selectedPreset = presets[2];
            onChange = vi.fn();
            _render5 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
              onChange: onChange,
              presets: presets,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 130,
                columnNumber: 9
              }
            })), driver = _render5.driver;
            _context5.next = 5;
            return driver.presetsDropdownLayoutDriver();
          case 5:
            _context5.next = 7;
            return _context5.sent.clickAtOptionWithValue(selectedPreset.value);
          case 7:
            expect(onChange).toHaveBeenCalledTimes(1);
            expect((0, _isSameDay["default"])(onChange.mock.calls[0][0].from, selectedPreset.selectedDays.from)).toBe(true);
            expect((0, _isSameDay["default"])(onChange.mock.calls[0][0].to, selectedPreset.selectedDays.to)).toBe(true);
          case 10:
          case "end":
            return _context5.stop();
        }
      }, _callee5);
    })));
    describe('no presets', function () {
      it('should not display the dropdown layout when the presets props is empty array', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
        var _render6, driver;
        return _regenerator["default"].wrap(function _callee6$(_context6) {
          while (1) switch (_context6.prev = _context6.next) {
            case 0:
              _render6 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
                onChange: vi.fn(),
                value: A_DAY_NOT_IN_PRESETS,
                selectionMode: "range",
                presets: [],
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 151,
                  columnNumber: 11
                }
              })), driver = _render6.driver;
              _context6.t0 = expect;
              _context6.next = 4;
              return driver.isDropdownExists();
            case 4:
              _context6.t1 = _context6.sent;
              (0, _context6.t0)(_context6.t1).toBe(false);
            case 6:
            case "end":
              return _context6.stop();
          }
        }, _callee6);
      })));
      it('should not display the dropdown layout when the is no presets prop', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee7() {
        var _render7, driver;
        return _regenerator["default"].wrap(function _callee7$(_context7) {
          while (1) switch (_context7.prev = _context7.next) {
            case 0:
              _render7 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
                onChange: vi.fn(),
                value: A_DAY_NOT_IN_PRESETS,
                selectionMode: "range",
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 163,
                  columnNumber: 11
                }
              })), driver = _render7.driver;
              _context7.t0 = expect;
              _context7.next = 4;
              return driver.isDropdownExists();
            case 4:
              _context7.t1 = _context7.sent;
              (0, _context7.t0)(_context7.t1).toBe(false);
            case 6:
            case "end":
              return _context7.stop();
          }
        }, _callee7);
      })));
    });
    describe('footer', function () {
      it('should render a given footer content', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee8() {
        var compDataHook, renderProp, _render8, driver;
        return _regenerator["default"].wrap(function _callee8$(_context8) {
          while (1) switch (_context8.prev = _context8.next) {
            case 0:
              compDataHook = 'given-comp';
              renderProp = function renderProp() {
                return /*#__PURE__*/_react["default"].createElement("div", {
                  "data-hook": compDataHook,
                  __self: _this,
                  __source: {
                    fileName: _jsxFileName,
                    lineNumber: 176,
                    columnNumber: 34
                  }
                });
              };
              _render8 = render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
                onChange: vi.fn(),
                footer: renderProp,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 178,
                  columnNumber: 11
                }
              })), driver = _render8.driver;
              _context8.t0 = expect;
              _context8.next = 6;
              return driver.findByDataHook(compDataHook);
            case 6:
              _context8.t1 = _context8.sent;
              (0, _context8.t0)(_context8.t1).toBeTruthy();
            case 8:
            case "end":
              return _context8.stop();
          }
        }, _callee8);
      })));
      describe('renderProp arguments', function () {
        describe('selectedDays', function () {
          it('should have selectedDays prop', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee9() {
            var renderProp, renderPropFirstArgument;
            return _regenerator["default"].wrap(function _callee9$(_context9) {
              while (1) switch (_context9.prev = _context9.next) {
                case 0:
                  renderProp = vi.fn();
                  render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], {
                    value: A_DAY,
                    onChange: vi.fn(),
                    footer: renderProp,
                    __self: _this,
                    __source: {
                      fileName: _jsxFileName,
                      lineNumber: 188,
                      columnNumber: 15
                    }
                  }));
                  renderPropFirstArgument = renderProp.mock.calls[0][0];
                  expect(renderProp).toHaveBeenCalledTimes(1);
                  expect(Object.keys(renderPropFirstArgument)).toContain('selectedDays');
                  expect(renderPropFirstArgument.selectedDays).toBe(A_DAY);
                case 6:
                case "end":
                  return _context9.stop();
              }
            }, _callee9);
          })));
        });
        describe('submitDisabled', function () {
          var noop = function noop() {};
          function testCase(_ref0) {
            var props = _ref0.props,
              expectedValue = _ref0.expectedValue;
            var renderProp = vi.fn();
            render(/*#__PURE__*/_react["default"].createElement(_CalendarPanel["default"], (0, _extends2["default"])({
              onChange: noop,
              footer: renderProp
            }, props, {
              __self: this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 210,
                columnNumber: 15
              }
            })));
            var renderPropFirstArgument = renderProp.mock.calls[0][0];
            expect(renderPropFirstArgument.submitDisabled).toBe(expectedValue);
          }
          describe('selectionMode=day', function () {
            it('should be false', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee0() {
              return _regenerator["default"].wrap(function _callee0$(_context0) {
                while (1) switch (_context0.prev = _context0.next) {
                  case 0:
                    testCase({
                      props: {
                        value: A_DAY,
                        selectionMode: 'day'
                      },
                      expectedValue: false
                    });
                  case 1:
                  case "end":
                    return _context0.stop();
                }
              }, _callee0);
            })));
            it('should be true', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee1() {
              return _regenerator["default"].wrap(function _callee1$(_context1) {
                while (1) switch (_context1.prev = _context1.next) {
                  case 0:
                    testCase({
                      props: {
                        value: undefined,
                        selectionMode: 'day'
                      },
                      expectedValue: true
                    });
                  case 1:
                  case "end":
                    return _context1.stop();
                }
              }, _callee1);
            })));
          });
          describe('selectionMode=range', function () {
            it('should be enabled when a range is selected', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee10() {
              return _regenerator["default"].wrap(function _callee10$(_context10) {
                while (1) switch (_context10.prev = _context10.next) {
                  case 0:
                    testCase({
                      props: {
                        value: {
                          from: A_DAY_BEFORE,
                          to: A_DAY
                        },
                        selectionMode: 'range'
                      },
                      expectedValue: false
                    });
                  case 1:
                  case "end":
                    return _context10.stop();
                }
              }, _callee10);
            })));
            it('should be disabled given no selection', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee11() {
              return _regenerator["default"].wrap(function _callee11$(_context11) {
                while (1) switch (_context11.prev = _context11.next) {
                  case 0:
                    testCase({
                      props: {
                        value: undefined,
                        selectionMode: 'range'
                      },
                      expectedValue: true
                    });
                  case 1:
                  case "end":
                    return _context11.stop();
                }
              }, _callee11);
            })));
            it("should be disabled given only a 'from' date", /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee12() {
              return _regenerator["default"].wrap(function _callee12$(_context12) {
                while (1) switch (_context12.prev = _context12.next) {
                  case 0:
                    testCase({
                      props: {
                        value: {
                          from: new Date(2018, 1, 1)
                        },
                        selectionMode: 'range'
                      },
                      expectedValue: true
                    });
                  case 1:
                  case "end":
                    return _context12.stop();
                }
              }, _callee12);
            })));
          });
        });
      });
    });
  }
});