@wix/design-system
Version:
@wix/design-system
395 lines (394 loc) • 20.1 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
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;
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
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().m(function _callee2() {
var _setSelectedIds, _isSelected;
return _regenerator().w(function (_context2) {
while (1) switch (_context2.n) {
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.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee() {
return _regenerator().w(function (_context) {
while (1) switch (_context.n) {
case 0:
_setSelectedIds([1, 2]);
case 1:
return _context.a(2);
}
}, _callee);
})));
case 1:
expect(_isSelected(1)).toBe(true);
expect(_isSelected(2)).toBe(true);
expect(_isSelected(3)).toBe(false);
case 2:
return _context2.a(2);
}
}, _callee2);
})));
it('updates selectedIds when items are removed', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee4() {
var _render, rerender;
return _regenerator().w(function (_context4) {
while (1) switch (_context4.n) {
case 0:
_render = (0, _index.render)(renderBulkSelection({
allIds: [1, 2, 3]
})), rerender = _render.rerender;
_context4.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee3() {
return _regenerator().w(function (_context3) {
while (1) switch (_context3.n) {
case 0:
_selectionContext.setSelectedIds([1, 2]);
case 1:
return _context3.a(2);
}
}, _callee3);
})));
case 1:
expect(_selectionContext.getSelectedIds()).toEqual([1, 2]);
rerender(renderBulkSelection({
allIds: [2, 3]
}));
expect(_selectionContext.getSelectedIds()).toEqual([2]);
case 2:
return _context4.a(2);
}
}, _callee4);
})));
describe('hasMoreInBulkSelection (infinite bulk selection mode)', function () {
it('should return correct totalCount when in infinite bulk selection', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee7() {
return _regenerator().w(function (_context7) {
while (1) switch (_context7.n) {
case 0:
(0, _index.render)(renderBulkSelection({
hasMoreInBulkSelection: true,
allIds: [1, 2, 3],
totalCount: 100
}));
expect(_selectionContext.selectedCount).toEqual(0);
_context7.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee5() {
return _regenerator().w(function (_context5) {
while (1) switch (_context5.n) {
case 0:
_selectionContext.toggleAll();
case 1:
return _context5.a(2);
}
}, _callee5);
})));
case 1:
expect(_selectionContext.selectedCount).toEqual(100);
_context7.n = 2;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee6() {
return _regenerator().w(function (_context6) {
while (1) switch (_context6.n) {
case 0:
_selectionContext.toggleSelectionById(1);
case 1:
return _context6.a(2);
}
}, _callee6);
})));
case 2:
expect(_selectionContext.selectedCount).toEqual(99);
case 3:
return _context7.a(2);
}
}, _callee7);
})));
it('should have context with correct info when in infinite bulk selection', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee0() {
return _regenerator().w(function (_context0) {
while (1) switch (_context0.n) {
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.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee8() {
return _regenerator().w(function (_context8) {
while (1) switch (_context8.n) {
case 0:
_selectionContext.toggleAll();
case 1:
return _context8.a(2);
}
}, _callee8);
})));
case 1:
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.n = 2;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee9() {
return _regenerator().w(function (_context9) {
while (1) switch (_context9.n) {
case 0:
_selectionContext.toggleSelectionById(1);
case 1:
return _context9.a(2);
}
}, _callee9);
})));
case 2:
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 3:
return _context0.a(2);
}
}, _callee0);
})));
it('should update selected ids when the list is fully loaded', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee11() {
var _render2, rerender;
return _regenerator().w(function (_context11) {
while (1) switch (_context11.n) {
case 0:
_render2 = (0, _index.render)(renderBulkSelection({
hasMoreInBulkSelection: true,
allIds: [1, 2, 3],
totalCount: 100
})), rerender = _render2.rerender;
_context11.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee1() {
return _regenerator().w(function (_context1) {
while (1) switch (_context1.n) {
case 0:
_selectionContext.toggleAll();
case 1:
return _context1.a(2);
}
}, _callee1);
})));
case 1:
_context11.n = 2;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee10() {
return _regenerator().w(function (_context10) {
while (1) switch (_context10.n) {
case 0:
_selectionContext.toggleSelectionById(1);
case 1:
return _context10.a(2);
}
}, _callee10);
})));
case 2:
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 3:
return _context11.a(2);
}
}, _callee11);
})));
it('should update bulkSelectionState after loading more when not in infinite bulk selection mode', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee13() {
var _render3, rerender;
return _regenerator().w(function (_context13) {
while (1) switch (_context13.n) {
case 0:
_render3 = (0, _index.render)(renderBulkSelection({
hasMoreInBulkSelection: false,
allIds: [1, 2, 3],
totalCount: 100
})), rerender = _render3.rerender;
_context13.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee12() {
return _regenerator().w(function (_context12) {
while (1) switch (_context12.n) {
case 0:
_selectionContext.toggleAll();
case 1:
return _context12.a(2);
}
}, _callee12);
})));
case 1:
expect(_selectionContext.bulkSelectionState).toEqual('ALL');
rerender(renderBulkSelection({
allIds: [1, 2, 3, 4, 5, 6]
}));
expect(_selectionContext.bulkSelectionState).toEqual('SOME');
case 2:
return _context13.a(2);
}
}, _callee13);
})));
it('should not change bulkSelectionState after loading more when in infinite bulk selection mode', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee15() {
var _render4, rerender;
return _regenerator().w(function (_context15) {
while (1) switch (_context15.n) {
case 0:
_render4 = (0, _index.render)(renderBulkSelection({
hasMoreInBulkSelection: true,
allIds: [1, 2, 3],
totalCount: 100
})), rerender = _render4.rerender;
_context15.n = 1;
return (0, _index.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator().m(function _callee14() {
return _regenerator().w(function (_context14) {
while (1) switch (_context14.n) {
case 0:
_selectionContext.toggleAll();
case 1:
return _context14.a(2);
}
}, _callee14);
})));
case 1:
expect(_selectionContext.bulkSelectionState).toEqual('ALL');
rerender(renderBulkSelection({
allIds: [1, 2, 3, 4, 5, 6]
}));
expect(_selectionContext.bulkSelectionState).toEqual('ALL');
case 2:
return _context15.a(2);
}
}, _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);
});
});
});