"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("../../utils/test-utils/react");
var _color = _interopRequireDefault(require("color"));
var _ColorPickerPrivateUni = require("../ColorPicker.private.uni.driver");
var _ColorPicker = _interopRequireDefault(require("../ColorPicker"));
var _constants = require("../constants");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/ColorPicker/test/ColorPicker.spec.jsx",
  _this = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
describe('ColorPicker', function () {
  describe('[async]', function () {
    runTests((0, _react2.createRendererWithUniDriver)(_ColorPickerPrivateUni.colorPickerUniDriverFactory));
  });
  function runTests(render) {
    var _this2 = this;
    afterEach(function () {
      return (0, _react2.cleanup)();
    });
    it('should successfully render a component', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var onChange, onCancel, onConfirm, _render, driver;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            onChange = vi.fn();
            onCancel = vi.fn();
            onConfirm = vi.fn();
            _render = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: '#000000',
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              __self: _this2,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 25,
                columnNumber: 9
              }
            })), driver = _render.driver;
            _context.t0 = expect;
            _context.next = 7;
            return driver.exists();
          case 7:
            _context.t1 = _context.sent;
            (0, _context.t0)(_context.t1).toBe(true);
            _context.t2 = expect;
            _context.next = 12;
            return driver.historyPanelExists();
          case 12:
            _context.t3 = _context.sent;
            (0, _context.t2)(_context.t3).toBe(false);
          case 14:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    describe('History', function () {
      it('should show history panel with current color selected as previous', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
        var onChange, onCancel, onConfirm, value, _render2, driver;
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              onChange = vi.fn();
              onCancel = vi.fn();
              onConfirm = vi.fn();
              value = '#000000';
              _render2 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
                value: value,
                onChange: onChange,
                onCancel: onCancel,
                onConfirm: onConfirm,
                showHistory: true,
                __self: _this2,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 40,
                  columnNumber: 11
                }
              })), driver = _render2.driver;
              _context2.t0 = expect;
              _context2.next = 8;
              return driver.historyPanelExists();
            case 8:
              _context2.t1 = _context2.sent;
              (0, _context2.t0)(_context2.t1).toBe(true);
              _context2.t2 = expect;
              _context2.t3 = _color["default"];
              _context2.next = 14;
              return driver.historyCurrentColor();
            case 14:
              _context2.t4 = _context2.sent;
              _context2.t5 = (0, _context2.t3)(_context2.t4).hex();
              (0, _context2.t2)(_context2.t5).toBe(value);
              _context2.t6 = expect;
              _context2.t7 = _color["default"];
              _context2.next = 21;
              return driver.historyPreviousColor();
            case 21:
              _context2.t8 = _context2.sent;
              _context2.t9 = (0, _context2.t7)(_context2.t8).hex();
              (0, _context2.t6)(_context2.t9).toBe(value);
            case 24:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      })));
      it('should not update previous color after current color change but not confirm', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
        var onChange, onCancel, onConfirm, value, _render3, driver;
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              onChange = vi.fn();
              onCancel = vi.fn();
              onConfirm = vi.fn();
              value = '#00FF00';
              _render3 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
                value: value,
                onChange: onChange,
                onCancel: onCancel,
                onConfirm: onConfirm,
                showHistory: true,
                __self: _this2,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 61,
                  columnNumber: 11
                }
              })), driver = _render3.driver;
              _context3.next = 7;
              return driver.selectBlackColor();
            case 7:
              _context3.t0 = expect;
              _context3.t1 = _color["default"];
              _context3.next = 11;
              return driver.historyCurrentColor();
            case 11:
              _context3.t2 = _context3.sent;
              _context3.t3 = (0, _context3.t1)(_context3.t2).hex();
              (0, _context3.t0)(_context3.t3).toBe('#000000');
              _context3.t4 = expect;
              _context3.t5 = _color["default"];
              _context3.next = 18;
              return driver.historyPreviousColor();
            case 18:
              _context3.t6 = _context3.sent;
              _context3.t7 = (0, _context3.t5)(_context3.t6).hex();
              (0, _context3.t4)(_context3.t7).toBe(value);
            case 21:
            case "end":
              return _context3.stop();
          }
        }, _callee3);
      })));
      it('should set previous color to be active color', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
        var onChange, onCancel, onConfirm, value, _render4, driver;
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              onChange = vi.fn();
              onCancel = vi.fn();
              onConfirm = vi.fn();
              value = '#00FF00';
              _render4 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
                value: value,
                onChange: onChange,
                onCancel: onCancel,
                onConfirm: onConfirm,
                showHistory: true,
                __self: _this2,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 82,
                  columnNumber: 11
                }
              })), driver = _render4.driver;
              _context4.next = 7;
              return driver.selectBlackColor();
            case 7:
              _context4.t0 = expect;
              _context4.t1 = _color["default"];
              _context4.next = 11;
              return driver.historyCurrentColor();
            case 11:
              _context4.t2 = _context4.sent;
              _context4.t3 = (0, _context4.t1)(_context4.t2).hex();
              (0, _context4.t0)(_context4.t3).toBe('#000000');
              _context4.t4 = expect;
              _context4.t5 = _color["default"];
              _context4.next = 18;
              return driver.historyPreviousColor();
            case 18:
              _context4.t6 = _context4.sent;
              _context4.t7 = (0, _context4.t5)(_context4.t6).hex();
              (0, _context4.t4)(_context4.t7).toBe(value);
              _context4.next = 23;
              return driver.clickOnPreviousColor();
            case 23:
              _context4.t8 = expect;
              _context4.t9 = _color["default"];
              _context4.next = 27;
              return driver.historyCurrentColor();
            case 27:
              _context4.t10 = _context4.sent;
              _context4.t11 = (0, _context4.t9)(_context4.t10).hex();
              (0, _context4.t8)(_context4.t11).toBe(value);
            case 30:
            case "end":
              return _context4.stop();
          }
        }, _callee4);
      })));
      it('should update previous color after confirm click', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
        var onChange, onCancel, onConfirm, value, _render5, driver;
        return _regenerator["default"].wrap(function _callee5$(_context5) {
          while (1) switch (_context5.prev = _context5.next) {
            case 0:
              onChange = vi.fn();
              onCancel = vi.fn();
              onConfirm = vi.fn();
              value = '#00FF00';
              _render5 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
                value: value,
                onChange: onChange,
                onCancel: onCancel,
                onConfirm: onConfirm,
                showHistory: true,
                __self: _this2,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 105,
                  columnNumber: 11
                }
              })), driver = _render5.driver;
              _context5.next = 7;
              return driver.selectBlackColor();
            case 7:
              _context5.t0 = expect;
              _context5.t1 = _color["default"];
              _context5.next = 11;
              return driver.historyCurrentColor();
            case 11:
              _context5.t2 = _context5.sent;
              _context5.t3 = (0, _context5.t1)(_context5.t2).hex();
              (0, _context5.t0)(_context5.t3).toBe('#000000');
              _context5.t4 = expect;
              _context5.t5 = _color["default"];
              _context5.next = 18;
              return driver.historyPreviousColor();
            case 18:
              _context5.t6 = _context5.sent;
              _context5.t7 = (0, _context5.t5)(_context5.t6).hex();
              (0, _context5.t4)(_context5.t7).toBe(value);
              _context5.next = 23;
              return driver.confirm();
            case 23:
              _context5.t8 = expect;
              _context5.t9 = _color["default"];
              _context5.next = 27;
              return driver.historyCurrentColor();
            case 27:
              _context5.t10 = _context5.sent;
              _context5.t11 = (0, _context5.t9)(_context5.t10).hex();
              (0, _context5.t8)(_context5.t11).toBe('#000000');
              _context5.t12 = expect;
              _context5.t13 = _color["default"];
              _context5.next = 34;
              return driver.historyPreviousColor();
            case 34:
              _context5.t14 = _context5.sent;
              _context5.t15 = (0, _context5.t13)(_context5.t14).hex();
              (0, _context5.t12)(_context5.t15).toBe('#000000');
            case 37:
            case "end":
              return _context5.stop();
          }
        }, _callee5);
      })));
      it('`onCancel` should be called when cancel icon is clicked', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
        var onChange, onCancel, onConfirm, value, _render6, driver;
        return _regenerator["default"].wrap(function _callee6$(_context6) {
          while (1) switch (_context6.prev = _context6.next) {
            case 0:
              onChange = vi.fn();
              onCancel = vi.fn();
              onConfirm = vi.fn();
              value = '#00FF00';
              _render6 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
                value: value,
                onChange: onChange,
                onCancel: onCancel,
                onConfirm: onConfirm,
                showHistory: true,
                __self: _this2,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 131,
                  columnNumber: 11
                }
              })), driver = _render6.driver;
              _context6.next = 7;
              return driver.selectBlackColor();
            case 7:
              _context6.t0 = expect;
              _context6.t1 = _color["default"];
              _context6.next = 11;
              return driver.historyCurrentColor();
            case 11:
              _context6.t2 = _context6.sent;
              _context6.t3 = (0, _context6.t1)(_context6.t2).hex();
              (0, _context6.t0)(_context6.t3).toBe('#000000');
              _context6.t4 = expect;
              _context6.t5 = _color["default"];
              _context6.next = 18;
              return driver.historyPreviousColor();
            case 18:
              _context6.t6 = _context6.sent;
              _context6.t7 = (0, _context6.t5)(_context6.t6).hex();
              (0, _context6.t4)(_context6.t7).toBe(value);
              _context6.next = 23;
              return driver.cancel();
            case 23:
              expect(onChange).toHaveBeenCalledTimes(1);
            case 24:
            case "end":
              return _context6.stop();
          }
        }, _callee6);
      })));
    });
  }
  describe('onAdd prop', function () {
    var render = (0, _react2.createRendererWithUniDriver)(_ColorPickerPrivateUni.colorPickerUniDriverFactory);
    it('should call callback with current color', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee7() {
      var onChange, onCancel, onConfirm, onAdd, value, _render7, driver;
      return _regenerator["default"].wrap(function _callee7$(_context7) {
        while (1) switch (_context7.prev = _context7.next) {
          case 0:
            onChange = vi.fn();
            onCancel = vi.fn();
            onConfirm = vi.fn();
            onAdd = vi.fn();
            value = '#00FF00';
            _render7 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: value,
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              onAdd: onAdd,
              showHistory: true,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 159,
                columnNumber: 9
              }
            })), driver = _render7.driver;
            _context7.next = 8;
            return driver.clickAddColor();
          case 8:
            expect(onAdd).toHaveBeenCalledTimes(1);
            expect(onAdd).toHaveBeenCalledWith(value);
          case 10:
          case "end":
            return _context7.stop();
        }
      }, _callee7);
    })));
  });
  describe('allowEmpty prop', function () {
    var render = (0, _react2.createRendererWithUniDriver)(_ColorPickerPrivateUni.colorPickerUniDriverFactory);
    it('should return color with alpha=0 if no color selected', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee8() {
      var onChange, onCancel, onConfirm, value, _render8, driver;
      return _regenerator["default"].wrap(function _callee8$(_context8) {
        while (1) switch (_context8.prev = _context8.next) {
          case 0:
            onChange = vi.fn();
            onCancel = vi.fn();
            onConfirm = vi.fn();
            value = '';
            _render8 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: value,
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              allowEmpty: true,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 185,
                columnNumber: 9
              }
            })), driver = _render8.driver;
            _context8.next = 7;
            return driver.confirm();
          case 7:
            expect(onConfirm).toHaveBeenCalledTimes(1);
            expect(onConfirm).toHaveBeenCalledWith(expect.objectContaining({
              valpha: 0
            }));
          case 9:
          case "end":
            return _context8.stop();
        }
      }, _callee8);
    })));
  });
  describe('children prop', function () {
    var render = (0, _react2.createRendererWithUniDriver)(_ColorPickerPrivateUni.colorPickerUniDriverFactory);
    it('should call given function if passed and inject changeColor', function () {
      var onChange = vi.fn();
      var onCancel = vi.fn();
      var onConfirm = vi.fn();
      var value = '#000';
      var childrenFunction = vi.fn();
      var children = function children(changeColor) {
        return childrenFunction(changeColor);
      };
      var _render9 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
          value: value,
          onChange: onChange,
          onCancel: onCancel,
          onConfirm: onConfirm,
          children: children,
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 215,
            columnNumber: 9
          }
        })),
        driver = _render9.driver;
      expect(childrenFunction).toHaveBeenCalledTimes(1);
      expect(childrenFunction).toHaveBeenCalledWith(expect.objectContaining({
        changeColor: expect.anything()
      }));
    });
  });
  describe('EyeDropper API availability', function () {
    var render = (0, _react2.createRendererWithUniDriver)(_ColorPickerPrivateUni.colorPickerUniDriverFactory);
    it('should not render eye dropper button when EyeDropper API is not available', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee9() {
      var onChange, onCancel, onConfirm, value, originalEyeDropper, _render0, driver, rerender;
      return _regenerator["default"].wrap(function _callee9$(_context9) {
        while (1) switch (_context9.prev = _context9.next) {
          case 0:
            onChange = vi.fn();
            onCancel = vi.fn();
            onConfirm = vi.fn();
            value = '#000000'; // Mock window without EyeDropper API
            originalEyeDropper = window.EyeDropper;
            delete window.EyeDropper;
            _render0 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: value,
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              showEyeDropper: true,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 248,
                columnNumber: 9
              }
            })), driver = _render0.driver, rerender = _render0.rerender; // Force a re-render to ensure the component re-evaluates the EyeDropper availability
            rerender(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: value,
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              showEyeDropper: true,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 261,
                columnNumber: 9
              }
            }));
            _context9.t0 = expect;
            _context9.next = 11;
            return driver.eyeDropperExists();
          case 11:
            _context9.t1 = _context9.sent;
            (0, _context9.t0)(_context9.t1).toBe(false);
            // Restore original EyeDropper
            if (originalEyeDropper) {
              window.EyeDropper = originalEyeDropper;
            }
          case 14:
          case "end":
            return _context9.stop();
        }
      }, _callee9);
    })));
    it('should render eye dropper button when EyeDropper API is available', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee0() {
      var onChange, onCancel, onConfirm, value, mockEyeDropper, _render1, driver;
      return _regenerator["default"].wrap(function _callee0$(_context0) {
        while (1) switch (_context0.prev = _context0.next) {
          case 0:
            onChange = vi.fn();
            onCancel = vi.fn();
            onConfirm = vi.fn();
            value = '#000000'; // Mock window with EyeDropper API
            mockEyeDropper = vi.fn();
            window.EyeDropper = mockEyeDropper;
            _render1 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: value,
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              showEyeDropper: true,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 291,
                columnNumber: 9
              }
            })), driver = _render1.driver;
            _context0.t0 = expect;
            _context0.next = 10;
            return driver.eyeDropperExists();
          case 10:
            _context0.t1 = _context0.sent;
            (0, _context0.t0)(_context0.t1).toBe(true);
          case 12:
          case "end":
            return _context0.stop();
        }
      }, _callee0);
    })));
    it('should not render eye dropper button when showEyeDropper is false', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee1() {
      var onChange, onCancel, onConfirm, value, mockEyeDropper, _render10, driver;
      return _regenerator["default"].wrap(function _callee1$(_context1) {
        while (1) switch (_context1.prev = _context1.next) {
          case 0:
            onChange = vi.fn();
            onCancel = vi.fn();
            onConfirm = vi.fn();
            value = '#000000'; // Mock window with EyeDropper API
            mockEyeDropper = vi.fn();
            window.EyeDropper = mockEyeDropper;
            _render10 = render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              value: value,
              onChange: onChange,
              onCancel: onCancel,
              onConfirm: onConfirm,
              showEyeDropper: false,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 316,
                columnNumber: 9
              }
            })), driver = _render10.driver;
            _context1.t0 = expect;
            _context1.next = 10;
            return driver.eyeDropperExists();
          case 10:
            _context1.t1 = _context1.sent;
            (0, _context1.t0)(_context1.t1).toBe(false);
            // Clean up
            delete window.EyeDropper;
          case 13:
          case "end":
            return _context1.stop();
        }
      }, _callee1);
    })));
  });
  describe('focus', function () {
    var render = (0, _react2.createRendererWithUniDriver)(_ColorPickerPrivateUni.colorPickerUniDriverFactory);
    it('should focus the first interactive element when focus() is called', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee10() {
      var colorPickerRef, value, hsbElement;
      return _regenerator["default"].wrap(function _callee10$(_context10) {
        while (1) switch (_context10.prev = _context10.next) {
          case 0:
            colorPickerRef = /*#__PURE__*/(0, _react.createRef)();
            value = '#000000';
            render(/*#__PURE__*/_react["default"].createElement(_ColorPicker["default"], {
              ref: colorPickerRef,
              value: value,
              onChange: function onChange() {},
              onCancel: function onCancel() {},
              onConfirm: function onConfirm() {},
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 342,
                columnNumber: 9
              }
            }));
            colorPickerRef.current.focus();
            hsbElement = document.querySelector("[data-hook=\"".concat(_constants.DataHooks.hsb, "\"]"));
            expect(document.activeElement).toBe(hsbElement);
          case 6:
          case "end":
            return _context10.stop();
        }
      }, _callee10);
    })));
  });
});