"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _BadgeSelect = _interopRequireDefault(require("../BadgeSelect"));
var _Badge = require("../../Badge/Badge.constants");
var _unit = require("../../utils/test-utils/unit");
var _BadgeSelectPrivateUni = require("../BadgeSelect.private.uni.driver");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/BadgeSelect/test/BadgeSelect.spec.jsx";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
describe('BadgeSelect', function () {
  describe('[async]', function () {
    runTests((0, _unit.createRendererWithUniDriver)(_BadgeSelectPrivateUni.badgeSelectPrivateUniDriverFactory));
  });
  function runTests(render) {
    var _this = this;
    var initialOptionId = 0;
    var options = Object.values(_Badge.SKIN).map(function (skin, id) {
      return {
        id: id.toString(),
        skin: skin,
        text: skin
      };
    });
    function createComponent() {
      var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var combinedProps = _objectSpread({
        options: options,
        selectedId: initialOptionId.toString(),
        popoverProps: {
          appendTo: 'window'
        }
      }, props);
      return render(/*#__PURE__*/_react["default"].createElement(_BadgeSelect["default"], (0, _extends2["default"])({}, combinedProps, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 34,
          columnNumber: 21
        }
      }))).driver;
    }
    afterEach(_unit.cleanup);
    it('should have a badge and hidden options by default', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var driver;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            driver = createComponent();
            _context.t0 = expect;
            _context.next = 4;
            return driver.exists();
          case 4:
            _context.t1 = _context.sent;
            (0, _context.t0)(_context.t1).toBe(true);
            _context.t2 = expect;
            _context.next = 9;
            return driver.isDropdownShown();
          case 9:
            _context.t3 = _context.sent;
            (0, _context.t2)(_context.t3).toBe(false);
            _context.t4 = expect;
            _context.next = 14;
            return driver.exists();
          case 14:
            _context.t5 = _context.sent;
            (0, _context.t4)(_context.t5).toBe(true);
          case 16:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    it('should show badge with initial selected skin and text', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var driver;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            driver = createComponent();
            _context2.t0 = expect;
            _context2.next = 4;
            return driver.getSkin();
          case 4:
            _context2.t1 = _context2.sent;
            (0, _context2.t0)(_context2.t1).toBe(options[initialOptionId].skin);
            _context2.t2 = expect;
            _context2.next = 9;
            return driver.text();
          case 9:
            _context2.t3 = _context2.sent;
            (0, _context2.t2)(_context2.t3).toBe(options[initialOptionId].text);
          case 11:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    })));
    it('should show badge with correct general props as default', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
      var driver;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            driver = createComponent();
            _context3.t0 = expect;
            _context3.next = 4;
            return driver.getType();
          case 4:
            _context3.t1 = _context3.sent;
            (0, _context3.t0)(_context3.t1).toBe(_Badge.TYPE.solid);
            _context3.t2 = expect;
            _context3.next = 9;
            return driver.getSize();
          case 9:
            _context3.t3 = _context3.sent;
            (0, _context3.t2)(_context3.t3).toBe(_Badge.SIZE.medium);
            _context3.t4 = expect;
            _context3.next = 14;
            return driver.isUppercase();
          case 14:
            _context3.t5 = _context3.sent;
            (0, _context3.t4)(_context3.t5).toBe(true);
          case 16:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    })));
    it('should show badge with props given', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
      var driver;
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) switch (_context4.prev = _context4.next) {
          case 0:
            driver = createComponent({
              type: _Badge.TYPE.outlined,
              size: _Badge.SIZE.small,
              uppercase: false
            });
            _context4.t0 = expect;
            _context4.next = 4;
            return driver.getType();
          case 4:
            _context4.t1 = _context4.sent;
            (0, _context4.t0)(_context4.t1).toBe(_Badge.TYPE.outlined);
            _context4.t2 = expect;
            _context4.next = 9;
            return driver.getSize();
          case 9:
            _context4.t3 = _context4.sent;
            (0, _context4.t2)(_context4.t3).toBe(_Badge.SIZE.small);
            _context4.t4 = expect;
            _context4.next = 14;
            return driver.isUppercase();
          case 14:
            _context4.t5 = _context4.sent;
            (0, _context4.t4)(_context4.t5).toBe(false);
          case 16:
          case "end":
            return _context4.stop();
        }
      }, _callee4);
    })));
    it('should show badge selector when badge is clicked', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
      var driver;
      return _regenerator["default"].wrap(function _callee5$(_context5) {
        while (1) switch (_context5.prev = _context5.next) {
          case 0:
            driver = createComponent();
            _context5.next = 3;
            return driver.clickOnBadge();
          case 3:
            _context5.t0 = expect;
            _context5.next = 6;
            return driver.isDropdownShown();
          case 6:
            _context5.t1 = _context5.sent;
            (0, _context5.t0)(_context5.t1).toBe(true);
          case 8:
          case "end":
            return _context5.stop();
        }
      }, _callee5);
    })));
    it('should hide options on selection', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
      var driver;
      return _regenerator["default"].wrap(function _callee6$(_context6) {
        while (1) switch (_context6.prev = _context6.next) {
          case 0:
            driver = createComponent();
            _context6.next = 3;
            return driver.click();
          case 3:
            _context6.next = 5;
            return driver.clickAtOption(2);
          case 5:
            _context6.t0 = expect;
            _context6.next = 8;
            return driver.isDropdownShown();
          case 8:
            _context6.t1 = _context6.sent;
            (0, _context6.t0)(_context6.t1).toBe(false);
          case 10:
          case "end":
            return _context6.stop();
        }
      }, _callee6);
    })));
    it('should hide options on outside click', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee7() {
      var driver;
      return _regenerator["default"].wrap(function _callee7$(_context7) {
        while (1) switch (_context7.prev = _context7.next) {
          case 0:
            driver = createComponent();
            _context7.next = 3;
            return driver.clickOnBadge();
          case 3:
            _context7.t0 = expect;
            _context7.next = 6;
            return driver.isDropdownShown();
          case 6:
            _context7.t1 = _context7.sent;
            (0, _context7.t0)(_context7.t1).toBe(true);
            _context7.next = 10;
            return driver.clickOutside();
          case 10:
            _context7.t2 = expect;
            _context7.next = 13;
            return driver.isDropdownShown();
          case 13:
            _context7.t3 = _context7.sent;
            (0, _context7.t2)(_context7.t3).toBe(false);
          case 15:
          case "end":
            return _context7.stop();
        }
      }, _callee7);
    })));
    it('should call onSelect when an option is selected', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee8() {
      var onSelect, selectedIndex, driver;
      return _regenerator["default"].wrap(function _callee8$(_context8) {
        while (1) switch (_context8.prev = _context8.next) {
          case 0:
            onSelect = vi.fn();
            selectedIndex = 3;
            driver = createComponent({
              onSelect: onSelect
            });
            _context8.next = 5;
            return driver.clickAtOption(selectedIndex);
          case 5:
            expect(onSelect).toHaveBeenCalled();
            expect(onSelect).toHaveBeenCalledWith(options[selectedIndex]);
          case 7:
          case "end":
            return _context8.stop();
        }
      }, _callee8);
    })));
    describe('uncontrolled mode', function () {
      it('should pick the first option if no selectedId given', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee9() {
        var uncontrolledProps, driver;
        return _regenerator["default"].wrap(function _callee9$(_context9) {
          while (1) switch (_context9.prev = _context9.next) {
            case 0:
              uncontrolledProps = {
                selectedId: undefined
              };
              driver = createComponent(uncontrolledProps);
              _context9.t0 = expect;
              _context9.next = 5;
              return driver.getSkin();
            case 5:
              _context9.t1 = _context9.sent;
              (0, _context9.t0)(_context9.t1).toBe(options[0].skin);
              _context9.t2 = expect;
              _context9.next = 10;
              return driver.text();
            case 10:
              _context9.t3 = _context9.sent;
              (0, _context9.t2)(_context9.t3).toBe(options[0].text);
            case 12:
            case "end":
              return _context9.stop();
          }
        }, _callee9);
      })));
      it('should change badge after an option is selected', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee0() {
        var uncontrolledProps, driver, selectedIndex;
        return _regenerator["default"].wrap(function _callee0$(_context0) {
          while (1) switch (_context0.prev = _context0.next) {
            case 0:
              uncontrolledProps = {
                selectedId: undefined
              };
              driver = createComponent(uncontrolledProps);
              selectedIndex = 3;
              _context0.next = 5;
              return driver.clickAtOption(selectedIndex);
            case 5:
              _context0.t0 = expect;
              _context0.next = 8;
              return driver.getSkin();
            case 8:
              _context0.t1 = _context0.sent;
              (0, _context0.t0)(_context0.t1).toBe(options[selectedIndex].skin);
              _context0.t2 = expect;
              _context0.next = 13;
              return driver.text();
            case 13:
              _context0.t3 = _context0.sent;
              (0, _context0.t2)(_context0.t3).toBe(options[selectedIndex].text);
            case 15:
            case "end":
              return _context0.stop();
          }
        }, _callee0);
      })));
    });
    describe('controlled mode', function () {
      it('should not change badge after an option is selected', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee1() {
        var driver, selectedIndex;
        return _regenerator["default"].wrap(function _callee1$(_context1) {
          while (1) switch (_context1.prev = _context1.next) {
            case 0:
              driver = createComponent();
              selectedIndex = 3;
              _context1.next = 4;
              return driver.clickAtOption(selectedIndex);
            case 4:
              _context1.t0 = expect;
              _context1.next = 7;
              return driver.getSkin();
            case 7:
              _context1.t1 = _context1.sent;
              (0, _context1.t0)(_context1.t1).toBe(options[initialOptionId].skin);
              _context1.t2 = expect;
              _context1.next = 12;
              return driver.text();
            case 12:
              _context1.t3 = _context1.sent;
              (0, _context1.t2)(_context1.t3).toBe(options[initialOptionId].text);
            case 14:
            case "end":
              return _context1.stop();
          }
        }, _callee1);
      })));
      it('should change badge only on selectedIndex change', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee10() {
        var dataHook, _render, driver, rerender, selectedIndex;
        return _regenerator["default"].wrap(function _callee10$(_context10) {
          while (1) switch (_context10.prev = _context10.next) {
            case 0:
              dataHook = 'badge-select';
              _render = render(/*#__PURE__*/_react["default"].createElement(_BadgeSelect["default"], {
                selectedId: "0",
                dataHook: dataHook,
                options: options,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 133,
                  columnNumber: 11
                }
              })), driver = _render.driver, rerender = _render.rerender;
              selectedIndex = 3;
              _context10.next = 5;
              return driver.clickAtOption(selectedIndex);
            case 5:
              rerender(/*#__PURE__*/_react["default"].createElement(_BadgeSelect["default"], {
                selectedId: "".concat(selectedIndex),
                dataHook: dataHook,
                options: options,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 141,
                  columnNumber: 11
                }
              }));
              _context10.t0 = expect;
              _context10.next = 9;
              return driver.getSkin();
            case 9:
              _context10.t1 = _context10.sent;
              (0, _context10.t0)(_context10.t1).toBe(options[selectedIndex].skin);
              _context10.t2 = expect;
              _context10.next = 14;
              return driver.text();
            case 14:
              _context10.t3 = _context10.sent;
              (0, _context10.t2)(_context10.t3).toBe(options[selectedIndex].text);
            case 16:
            case "end":
              return _context10.stop();
          }
        }, _callee10);
      })));
    });
  }
});