import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/esm/AutoComplete/test/Autocomplete.spec.jsx";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
import { autoCompleteUniDriverFactory } from '../AutoComplete.uni.driver';
import AutoComplete from '../AutoComplete';
import { cleanup, createRendererWithUniDriver } from '../../utils/test-utils/unit';
import { act } from '../../utils/test-utils/unidriver/actCompat';
import { waitFor } from '../../utils/test-utils/react';
var asciiA = '97';
describe('Autocomplete', function () {
  describe('[async]', function () {
    runTests(createRendererWithUniDriver(autoCompleteUniDriverFactory));
  });
  function runTests(render) {
    var _this = this;
    var createDriver = function createDriver(jsx) {
      return render(jsx).driver;
    };
    afterEach(function () {
      return cleanup();
    });
    var options = [{
      id: 0,
      value: 'aaa'
    }, {
      id: 1,
      value: 'abb'
    }, {
      id: 2,
      value: 'bbb',
      disabled: true
    }, {
      id: 3,
      value: 'bcc'
    }, {
      id: 'divider1',
      value: '-'
    }, {
      id: 'element1',
      value: /*#__PURE__*/React.createElement("span", {
        style: {
          color: 'brown'
        },
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 27,
          columnNumber: 32
        }
      }, "ccc")
    }];
    var predicate = function predicate(option) {
      return option.value.toString().toLowerCase().indexOf('a') !== -1;
    };
    it('should not filter anything without predicate function', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var _createDriver, dropdownLayoutDriver;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _createDriver = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
              options: options,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 35,
                columnNumber: 9
              }
            })), dropdownLayoutDriver = _createDriver.dropdownLayoutDriver;
            _context.t0 = expect;
            _context.next = 4;
            return dropdownLayoutDriver.optionsLength();
          case 4:
            _context.t1 = _context.sent;
            (0, _context.t0)(_context.t1).toBe(options.length);
          case 6:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    ['ArrowUp', 'ArrowDown'].forEach(function (key) {
      it("should not filter items according to predicate function when pressing ".concat(key), /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var _createDriver2, inputDriver, dropdownLayoutDriver;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              _createDriver2 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
                options: options,
                predicate: predicate,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 43,
                  columnNumber: 11
                }
              })), inputDriver = _createDriver2.inputDriver, dropdownLayoutDriver = _createDriver2.dropdownLayoutDriver;
              _context2.next = 3;
              return inputDriver.trigger('keyDown', {
                key: key
              });
            case 3:
              _context2.t0 = expect;
              _context2.next = 6;
              return dropdownLayoutDriver.optionsLength();
            case 6:
              _context2.t1 = _context2.sent;
              (0, _context2.t0)(_context2.t1).toBe(options.length);
            case 8:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      })));
    });
    it('should filter items according to predicate function when typing characters', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
      var _createDriver3, inputDriver, dropdownLayoutDriver;
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            _createDriver3 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
              options: options,
              predicate: predicate,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 52,
                columnNumber: 9
              }
            })), inputDriver = _createDriver3.inputDriver, dropdownLayoutDriver = _createDriver3.dropdownLayoutDriver;
            _context3.next = 3;
            return inputDriver.click();
          case 3:
            _context3.next = 5;
            return inputDriver.trigger('keyDown', {
              key: asciiA
            });
          case 5:
            _context3.t0 = expect;
            _context3.next = 8;
            return dropdownLayoutDriver.optionsLength();
          case 8:
            _context3.t1 = _context3.sent;
            (0, _context3.t0)(_context3.t1).toBe(2);
          case 10:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    })));
    it('should not display dropdown layout in case all options are filtered', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
      var _createDriver4, inputDriver, dropdownLayoutDriver;
      return _regeneratorRuntime.wrap(function _callee5$(_context5) {
        while (1) switch (_context5.prev = _context5.next) {
          case 0:
            _createDriver4 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
              options: options,
              predicate: function predicate() {
                return false;
              },
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 61,
                columnNumber: 9
              }
            })), inputDriver = _createDriver4.inputDriver, dropdownLayoutDriver = _createDriver4.dropdownLayoutDriver;
            _context5.next = 3;
            return inputDriver.click();
          case 3:
            _context5.next = 5;
            return inputDriver.trigger('keyDown', {
              key: asciiA
            });
          case 5:
            _context5.next = 7;
            return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
              return _regeneratorRuntime.wrap(function _callee4$(_context4) {
                while (1) switch (_context4.prev = _context4.next) {
                  case 0:
                    _context4.t0 = expect;
                    _context4.next = 3;
                    return dropdownLayoutDriver.optionsLength();
                  case 3:
                    _context4.t1 = _context4.sent;
                    (0, _context4.t0)(_context4.t1).toBe(0);
                  case 5:
                  case "end":
                    return _context4.stop();
                }
              }, _callee4);
            })));
          case 7:
          case "end":
            return _context5.stop();
        }
      }, _callee5);
    })));
    it('should display empty state message in case all options are filtered', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
      var emptyStateMessage, _createDriver5, inputDriver, dropdownLayoutDriver, option;
      return _regeneratorRuntime.wrap(function _callee6$(_context6) {
        while (1) switch (_context6.prev = _context6.next) {
          case 0:
            emptyStateMessage = 'Empty state';
            _createDriver5 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
              options: options,
              predicate: function predicate() {
                return false;
              },
              emptyStateMessage: /*#__PURE__*/React.createElement("div", {
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 76,
                  columnNumber: 30
                }
              }, emptyStateMessage),
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 73,
                columnNumber: 9
              }
            })), inputDriver = _createDriver5.inputDriver, dropdownLayoutDriver = _createDriver5.dropdownLayoutDriver;
            _context6.next = 4;
            return inputDriver.click();
          case 4:
            _context6.next = 6;
            return inputDriver.trigger('keyDown', {
              key: asciiA
            });
          case 6:
            _context6.t0 = expect;
            _context6.next = 9;
            return dropdownLayoutDriver.isShown();
          case 9:
            _context6.t1 = _context6.sent;
            (0, _context6.t0)(_context6.t1).toBe(true);
            _context6.t2 = expect;
            _context6.next = 14;
            return dropdownLayoutDriver.optionsLength();
          case 14:
            _context6.t3 = _context6.sent;
            (0, _context6.t2)(_context6.t3).toBe(1);
            _context6.next = 18;
            return dropdownLayoutDriver.options();
          case 18:
            option = _context6.sent[0];
            _context6.t4 = expect;
            _context6.next = 22;
            return option.content();
          case 22:
            _context6.t5 = _context6.sent;
            (0, _context6.t4)(_context6.t5).toBe(emptyStateMessage);
            _context6.t6 = expect;
            _context6.next = 27;
            return option.isDisabled();
          case 27:
            _context6.t7 = _context6.sent;
            (0, _context6.t6)(_context6.t7).toBe(true);
          case 29:
          case "end":
            return _context6.stop();
        }
      }, _callee6);
    })));
    it('should show all items when focusing even if some text exist', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
      var _createDriver6, dropdownLayoutDriver, inputDriver;
      return _regeneratorRuntime.wrap(function _callee7$(_context7) {
        while (1) switch (_context7.prev = _context7.next) {
          case 0:
            _createDriver6 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
              options: options,
              predicate: predicate,
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 92,
                columnNumber: 9
              }
            })), dropdownLayoutDriver = _createDriver6.dropdownLayoutDriver, inputDriver = _createDriver6.inputDriver;
            _context7.next = 3;
            return inputDriver.enterText('aaa');
          case 3:
            _context7.next = 5;
            return inputDriver.focus();
          case 5:
            _context7.t0 = expect;
            _context7.next = 8;
            return dropdownLayoutDriver.optionsLength();
          case 8:
            _context7.t1 = _context7.sent;
            (0, _context7.t0)(_context7.t1).toBe(options.length);
          case 10:
          case "end":
            return _context7.stop();
        }
      }, _callee7);
    })));
    it('should invoke the onEmptyState callback function when no options exist', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
      var _onEmptyState, _createDriver7, inputDriver, dropdownLayoutDriver;
      return _regeneratorRuntime.wrap(function _callee9$(_context9) {
        while (1) switch (_context9.prev = _context9.next) {
          case 0:
            _onEmptyState = vi.fn();
            _createDriver7 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
              options: options,
              predicate: function predicate() {
                return false;
              },
              onEmptyState: function onEmptyState() {
                return _onEmptyState();
              },
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 102,
                columnNumber: 9
              }
            })), inputDriver = _createDriver7.inputDriver, dropdownLayoutDriver = _createDriver7.dropdownLayoutDriver;
            _context9.next = 4;
            return inputDriver.click();
          case 4:
            _context9.next = 6;
            return inputDriver.trigger('keyDown', {
              key: asciiA
            });
          case 6:
            _context9.next = 8;
            return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
              return _regeneratorRuntime.wrap(function _callee8$(_context8) {
                while (1) switch (_context8.prev = _context8.next) {
                  case 0:
                    _context8.t0 = expect;
                    _context8.next = 3;
                    return dropdownLayoutDriver.optionsLength();
                  case 3:
                    _context8.t1 = _context8.sent;
                    (0, _context8.t0)(_context8.t1).toBe(0);
                  case 5:
                  case "end":
                    return _context8.stop();
                }
              }, _callee8);
            })));
          case 8:
            expect(_onEmptyState).toHaveBeenCalledTimes(1);
          case 9:
          case "end":
            return _context9.stop();
        }
      }, _callee9);
    })));
    describe('Escape key navigation', function () {
      it('should hide dropdown when Escape key is pressed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee1() {
        var _createDriver8, driver, dropdownLayoutDriver;
        return _regeneratorRuntime.wrap(function _callee1$(_context1) {
          while (1) switch (_context1.prev = _context1.next) {
            case 0:
              _createDriver8 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
                options: options,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 121,
                  columnNumber: 11
                }
              })), driver = _createDriver8.driver, dropdownLayoutDriver = _createDriver8.dropdownLayoutDriver;
              _context1.next = 3;
              return driver.pressKey('ArrowDown');
            case 3:
              _context1.t0 = expect;
              _context1.next = 6;
              return dropdownLayoutDriver.isShown();
            case 6:
              _context1.t1 = _context1.sent;
              (0, _context1.t0)(_context1.t1).toBe(true);
              _context1.next = 10;
              return driver.pressKey('Escape');
            case 10:
              _context1.next = 12;
              return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee0() {
                return _regeneratorRuntime.wrap(function _callee0$(_context0) {
                  while (1) switch (_context0.prev = _context0.next) {
                    case 0:
                      _context0.t0 = expect;
                      _context0.next = 3;
                      return dropdownLayoutDriver.isShown();
                    case 3:
                      _context0.t1 = _context0.sent;
                      (0, _context0.t0)(_context0.t1).toBe(false);
                    case 5:
                    case "end":
                      return _context0.stop();
                  }
                }, _callee0);
              })));
            case 12:
            case "end":
              return _context1.stop();
          }
        }, _callee1);
      })));
      it('should call onClose callback when Escape key is pressed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
        var onClose, _createDriver9, driver;
        return _regeneratorRuntime.wrap(function _callee10$(_context10) {
          while (1) switch (_context10.prev = _context10.next) {
            case 0:
              onClose = vi.fn();
              _createDriver9 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
                options: options,
                onClose: onClose,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 134,
                  columnNumber: 11
                }
              })), driver = _createDriver9.driver;
              _context10.next = 4;
              return driver.focus();
            case 4:
              _context10.next = 6;
              return driver.pressKey('ArrowDown');
            case 6:
              expect(onClose).not.toHaveBeenCalled();
              _context10.next = 9;
              return driver.pressKey('Escape');
            case 9:
              expect(onClose).toHaveBeenCalled();
            case 10:
            case "end":
              return _context10.stop();
          }
        }, _callee10);
      })));
      it('should call onOptionsHide callback when Escape key is pressed', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee11() {
        var onOptionsHide, _createDriver0, driver;
        return _regeneratorRuntime.wrap(function _callee11$(_context11) {
          while (1) switch (_context11.prev = _context11.next) {
            case 0:
              onOptionsHide = vi.fn();
              _createDriver0 = createDriver(/*#__PURE__*/React.createElement(AutoComplete, {
                options: options,
                onOptionsHide: onOptionsHide,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 147,
                  columnNumber: 11
                }
              })), driver = _createDriver0.driver;
              _context11.next = 4;
              return driver.focus();
            case 4:
              _context11.next = 6;
              return driver.pressKey('ArrowDown');
            case 6:
              expect(onOptionsHide).not.toHaveBeenCalled();
              _context11.next = 9;
              return driver.pressKey('Escape');
            case 9:
              expect(onOptionsHide).toHaveBeenCalled();
            case 10:
            case "end":
              return _context11.stop();
          }
        }, _callee11);
      })));
      it('should stop Escape key propagation to parent elements', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee13() {
        var onParentKeyDown, rendered, _rendered$driver, driver, dropdownLayoutDriver, arrowDownCallCount;
        return _regeneratorRuntime.wrap(function _callee13$(_context13) {
          while (1) switch (_context13.prev = _context13.next) {
            case 0:
              onParentKeyDown = vi.fn();
              rendered = render(/*#__PURE__*/React.createElement("div", {
                onKeyDown: onParentKeyDown,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 160,
                  columnNumber: 11
                }
              }, /*#__PURE__*/React.createElement(AutoComplete, {
                dataHook: "test-autocomplete",
                options: options,
                __self: _this,
                __source: {
                  fileName: _jsxFileName,
                  lineNumber: 161,
                  columnNumber: 13
                }
              })), {
                dataHook: 'test-autocomplete'
              });
              _rendered$driver = rendered.driver, driver = _rendered$driver.driver, dropdownLayoutDriver = _rendered$driver.dropdownLayoutDriver;
              _context13.next = 5;
              return driver.focus();
            case 5:
              _context13.next = 7;
              return driver.pressKey('ArrowDown');
            case 7:
              arrowDownCallCount = onParentKeyDown.mock.calls.length;
              _context13.t0 = expect;
              _context13.next = 11;
              return dropdownLayoutDriver.isShown();
            case 11:
              _context13.t1 = _context13.sent;
              (0, _context13.t0)(_context13.t1).toBe(true);
              _context13.next = 15;
              return driver.focus();
            case 15:
              _context13.next = 17;
              return driver.pressKey('Escape');
            case 17:
              _context13.next = 19;
              return waitFor(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee12() {
                return _regeneratorRuntime.wrap(function _callee12$(_context12) {
                  while (1) switch (_context12.prev = _context12.next) {
                    case 0:
                      _context12.t0 = expect;
                      _context12.next = 3;
                      return dropdownLayoutDriver.isShown();
                    case 3:
                      _context12.t1 = _context12.sent;
                      (0, _context12.t0)(_context12.t1).toBe(false);
                    case 5:
                    case "end":
                      return _context12.stop();
                  }
                }, _callee12);
              })));
            case 19:
              // Verify Escape didn't propagate (call count should remain the same as after ArrowDown)
              expect(onParentKeyDown.mock.calls.length).toBe(arrowDownCallCount);
            case 20:
            case "end":
              return _context13.stop();
          }
        }, _callee13);
      })));
    });
  }
});