"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 _react = _interopRequireDefault(require("react"));
var _index = require("../../utils/test-utils/react/index.ts");
var _BulkSelectionConsumer = require("./BulkSelectionConsumer");
var _BulkSelection = require("./BulkSelection");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Table/BulkSelection/BulkSelection.spec.jsx",
  _this = void 0;
describe('BulkSelection', function () {
  var _selectionContext;
  var renderBulkSelection = function renderBulkSelection(props) {
    return /*#__PURE__*/_react["default"].createElement(_BulkSelection.BulkSelection, (0, _extends2["default"])({}, props, {
      __self: _this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 10,
        columnNumber: 5
      }
    }), /*#__PURE__*/_react["default"].createElement(_BulkSelectionConsumer.BulkSelectionConsumer, {
      __self: _this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 11,
        columnNumber: 7
      }
    }, function (selectionContext) {
      _selectionContext = selectionContext;
      return /*#__PURE__*/_react["default"].createElement("div", {
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 14,
          columnNumber: 18
        }
      });
    }));
  };
  it('setSelectionIds & isSelected', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
    var _setSelectedIds, _isSelected;
    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          (0, _index.render)(/*#__PURE__*/_react["default"].createElement(_BulkSelection.BulkSelection, {
            allIds: [1, 2, 3],
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 23,
              columnNumber: 7
            }
          }, /*#__PURE__*/_react["default"].createElement(_BulkSelectionConsumer.BulkSelectionConsumer, {
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 24,
              columnNumber: 9
            }
          }, function (_ref2) {
            var setSelectedIds = _ref2.setSelectedIds,
              isSelected = _ref2.isSelected;
            _setSelectedIds = setSelectedIds;
            _isSelected = isSelected;
            return /*#__PURE__*/_react["default"].createElement("div", {
              __self: _this,
              __source: {
                fileName: _jsxFileName,
                lineNumber: 28,
                columnNumber: 20
              }
            });
          })));
          expect(_isSelected(1)).toBe(false);
          expect(_isSelected(2)).toBe(false);
          expect(_isSelected(3)).toBe(false);
          _context2.next = 6;
          return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
            return _regenerator["default"].wrap(function _callee$(_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  _setSelectedIds([1, 2]);
                case 1:
                case "end":
                  return _context.stop();
              }
            }, _callee);
          })));
        case 6:
          expect(_isSelected(1)).toBe(true);
          expect(_isSelected(2)).toBe(true);
          expect(_isSelected(3)).toBe(false);
        case 9:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  })));
  it('updates selectedIds when items are removed', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
    var _render, rerender;
    return _regenerator["default"].wrap(function _callee4$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          _render = (0, _index.render)(renderBulkSelection({
            allIds: [1, 2, 3]
          })), rerender = _render.rerender;
          _context4.next = 3;
          return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
            return _regenerator["default"].wrap(function _callee3$(_context3) {
              while (1) switch (_context3.prev = _context3.next) {
                case 0:
                  _selectionContext.setSelectedIds([1, 2]);
                case 1:
                case "end":
                  return _context3.stop();
              }
            }, _callee3);
          })));
        case 3:
          expect(_selectionContext.getSelectedIds()).toEqual([1, 2]);
          rerender(renderBulkSelection({
            allIds: [2, 3]
          }));
          expect(_selectionContext.getSelectedIds()).toEqual([2]);
        case 6:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  })));
  describe('hasMoreInBulkSelection (infinite bulk selection mode)', function () {
    it('should return correct totalCount when in infinite bulk selection', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee7() {
      return _regenerator["default"].wrap(function _callee7$(_context7) {
        while (1) switch (_context7.prev = _context7.next) {
          case 0:
            (0, _index.render)(renderBulkSelection({
              hasMoreInBulkSelection: true,
              allIds: [1, 2, 3],
              totalCount: 100
            }));
            expect(_selectionContext.selectedCount).toEqual(0);
            _context7.next = 4;
            return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
              return _regenerator["default"].wrap(function _callee5$(_context5) {
                while (1) switch (_context5.prev = _context5.next) {
                  case 0:
                    _selectionContext.toggleAll();
                  case 1:
                  case "end":
                    return _context5.stop();
                }
              }, _callee5);
            })));
          case 4:
            expect(_selectionContext.selectedCount).toEqual(100);
            _context7.next = 7;
            return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
              return _regenerator["default"].wrap(function _callee6$(_context6) {
                while (1) switch (_context6.prev = _context6.next) {
                  case 0:
                    _selectionContext.toggleSelectionById(1);
                  case 1:
                  case "end":
                    return _context6.stop();
                }
              }, _callee6);
            })));
          case 7:
            expect(_selectionContext.selectedCount).toEqual(99);
          case 8:
          case "end":
            return _context7.stop();
        }
      }, _callee7);
    })));
    it('should have context with correct info when in infinite bulk selection', /*#__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:
            (0, _index.render)(renderBulkSelection({
              hasMoreInBulkSelection: true,
              allIds: [1, 2, 3],
              totalCount: 100
            }));
            expect(_selectionContext.getSelectedIds()).toEqual([]);
            expect(_selectionContext.getNotSelectedIds()).toEqual(null);
            expect(_selectionContext.infiniteBulkSelected).toEqual(false);
            expect(_selectionContext.isSelected(1)).toEqual(false);
            expect(_selectionContext.bulkSelectionState).toEqual('NONE');
            _context0.next = 8;
            return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee8() {
              return _regenerator["default"].wrap(function _callee8$(_context8) {
                while (1) switch (_context8.prev = _context8.next) {
                  case 0:
                    _selectionContext.toggleAll();
                  case 1:
                  case "end":
                    return _context8.stop();
                }
              }, _callee8);
            })));
          case 8:
            expect(_selectionContext.getSelectedIds()).toEqual(null);
            expect(_selectionContext.getNotSelectedIds()).toEqual([]);
            expect(_selectionContext.infiniteBulkSelected).toEqual(true);
            expect(_selectionContext.isSelected(1)).toEqual(true);
            expect(_selectionContext.bulkSelectionState).toEqual('ALL');
            _context0.next = 15;
            return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee9() {
              return _regenerator["default"].wrap(function _callee9$(_context9) {
                while (1) switch (_context9.prev = _context9.next) {
                  case 0:
                    _selectionContext.toggleSelectionById(1);
                  case 1:
                  case "end":
                    return _context9.stop();
                }
              }, _callee9);
            })));
          case 15:
            expect(_selectionContext.getSelectedIds()).toEqual(null);
            expect(_selectionContext.getNotSelectedIds()).toEqual([1]);
            expect(_selectionContext.infiniteBulkSelected).toEqual(true);
            expect(_selectionContext.isSelected(1)).toEqual(false);
            expect(_selectionContext.bulkSelectionState).toEqual('SOME');
          case 20:
          case "end":
            return _context0.stop();
        }
      }, _callee0);
    })));
    it('should update selected ids when the list is fully loaded', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee11() {
      var _render2, rerender;
      return _regenerator["default"].wrap(function _callee11$(_context11) {
        while (1) switch (_context11.prev = _context11.next) {
          case 0:
            _render2 = (0, _index.render)(renderBulkSelection({
              hasMoreInBulkSelection: true,
              allIds: [1, 2, 3],
              totalCount: 100
            })), rerender = _render2.rerender;
            _context11.next = 3;
            return (0, _index.act)(/*#__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:
                    _selectionContext.toggleAll();
                  case 1:
                  case "end":
                    return _context1.stop();
                }
              }, _callee1);
            })));
          case 3:
            _context11.next = 5;
            return (0, _index.act)(/*#__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:
                    _selectionContext.toggleSelectionById(1);
                  case 1:
                  case "end":
                    return _context10.stop();
                }
              }, _callee10);
            })));
          case 5:
            expect(_selectionContext.getSelectedIds()).toEqual(null);
            expect(_selectionContext.getNotSelectedIds()).toEqual([1]);
            expect(_selectionContext.infiniteBulkSelected).toEqual(true);
            expect(_selectionContext.isSelected(1)).toEqual(false);
            expect(_selectionContext.isSelected(2)).toEqual(true);
            rerender(renderBulkSelection({
              hasMoreInBulkSelection: false,
              allIds: [1, 2, 3],
              totalCount: 100
            }));
            expect(_selectionContext.getSelectedIds()).toEqual([2, 3]);
            expect(_selectionContext.getNotSelectedIds()).toEqual(null);
            expect(_selectionContext.infiniteBulkSelected).toEqual(false);
            expect(_selectionContext.isSelected(1)).toEqual(false);
            expect(_selectionContext.isSelected(2)).toEqual(true);
          case 16:
          case "end":
            return _context11.stop();
        }
      }, _callee11);
    })));
    it('should update bulkSelectionState after loading more when not in infinite bulk selection mode', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee13() {
      var _render3, rerender;
      return _regenerator["default"].wrap(function _callee13$(_context13) {
        while (1) switch (_context13.prev = _context13.next) {
          case 0:
            _render3 = (0, _index.render)(renderBulkSelection({
              hasMoreInBulkSelection: false,
              allIds: [1, 2, 3],
              totalCount: 100
            })), rerender = _render3.rerender;
            _context13.next = 3;
            return (0, _index.act)(/*#__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:
                    _selectionContext.toggleAll();
                  case 1:
                  case "end":
                    return _context12.stop();
                }
              }, _callee12);
            })));
          case 3:
            expect(_selectionContext.bulkSelectionState).toEqual('ALL');
            rerender(renderBulkSelection({
              allIds: [1, 2, 3, 4, 5, 6]
            }));
            expect(_selectionContext.bulkSelectionState).toEqual('SOME');
          case 6:
          case "end":
            return _context13.stop();
        }
      }, _callee13);
    })));
    it('should not change bulkSelectionState after loading more when in infinite bulk selection mode', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee15() {
      var _render4, rerender;
      return _regenerator["default"].wrap(function _callee15$(_context15) {
        while (1) switch (_context15.prev = _context15.next) {
          case 0:
            _render4 = (0, _index.render)(renderBulkSelection({
              hasMoreInBulkSelection: true,
              allIds: [1, 2, 3],
              totalCount: 100
            })), rerender = _render4.rerender;
            _context15.next = 3;
            return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee14() {
              return _regenerator["default"].wrap(function _callee14$(_context14) {
                while (1) switch (_context14.prev = _context14.next) {
                  case 0:
                    _selectionContext.toggleAll();
                  case 1:
                  case "end":
                    return _context14.stop();
                }
              }, _callee14);
            })));
          case 3:
            expect(_selectionContext.bulkSelectionState).toEqual('ALL');
            rerender(renderBulkSelection({
              allIds: [1, 2, 3, 4, 5, 6]
            }));
            expect(_selectionContext.bulkSelectionState).toEqual('ALL');
          case 6:
          case "end":
            return _context15.stop();
        }
      }, _callee15);
    })));
  });
  describe('selectionDisabled', function () {
    var randomRow = {};
    it('should be false when prop is not set', function () {
      (0, _index.render)(renderBulkSelection({
        allIds: [1, 2, 3]
      }));
      expect(_selectionContext.selectionDisabled).toBe(false);
    });
    it('should be false when there are items', function () {
      (0, _index.render)(renderBulkSelection({
        allIds: [1, 2, 3],
        selectionDisabled: function selectionDisabled() {
          return false;
        }
      }));
      expect(_selectionContext.selectionDisabled()).toBe(false);
    });
    it('should be true when disabled is set (boolean)', function () {
      (0, _index.render)(renderBulkSelection({
        allIds: [1, 2, 3],
        selectionDisabled: true
      }));
      expect(_selectionContext.selectionDisabled).toBe(true);
    });
    it('should be true when disabled is set (function)', function () {
      (0, _index.render)(renderBulkSelection({
        allIds: [1, 2, 3],
        selectionDisabled: function selectionDisabled() {
          return true;
        }
      }));
      expect(_selectionContext.selectionDisabled(randomRow)).toBe(true);
    });
    it('should update to true when disabled is updated', function () {
      var _render5 = (0, _index.render)(renderBulkSelection({
          allIds: [1, 2, 3],
          selectionDisabled: function selectionDisabled() {
            return false;
          }
        })),
        rerender = _render5.rerender;
      expect(_selectionContext.selectionDisabled(randomRow)).toBe(false);
      rerender(renderBulkSelection({
        allIds: [1, 2, 3],
        selectionDisabled: function selectionDisabled() {
          return true;
        }
      }));
      expect(_selectionContext.selectionDisabled(randomRow)).toBe(true);
    });
  });
});