wix-style-react
Version:
587 lines (483 loc) • 19.2 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.dataTableUniDriverFactory = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _baseDriver = require("wix-ui-test-utils/base-driver");
var _unidriver = require("../../../test/utils/unidriver");
var _values = _interopRequireDefault(require("../../utils/operators/values"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
// const arbitraryEmptyElement = () => document.createElement('div');
var dataTableUniDriverFactory = function dataTableUniDriverFactory(base) {
// Node is a DOM API which does not exist in jsdom. Using a constant instead: https://developer.mozilla.org/en-US/docs/Web/API/Node
var COMMENT_NODE = 8; // When a React component renders null in React 15, a Comment-Element is rendered to the DOM.
var isDisplayingNothing = /*#__PURE__*/function () {
var _ref = (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:
_context.next = 2;
return base.exists();
case 2:
_context.t0 = !_context.sent;
if (_context.t0) {
_context.next = 9;
break;
}
_context.next = 6;
return base._prop('nodeType');
case 6:
_context.t1 = _context.sent;
_context.t2 = COMMENT_NODE;
_context.t0 = _context.t1 === _context.t2;
case 9:
return _context.abrupt("return", _context.t0);
case 10:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function isDisplayingNothing() {
return _ref.apply(this, arguments);
};
}();
/* Since a Comment-Element has no methods like querySelector(),
* we replace (if needed) it with an arbitrary Element.
* This allows simple implementation of methods like getRows().
*/
// const base = () => await isDisplayingNothing() ? arbitraryEmptyElement() : base;
var reactBase = (0, _unidriver.ReactBase)(base);
var getHeader = function getHeader() {
return base.$('thead');
};
var hasHeader = function hasHeader() {
return getHeader().exists();
};
var getRows = function getRows() {
return base.$$('tbody tr[data-table-row="dataTableRow"]');
};
var getRowsCount = /*#__PURE__*/function () {
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return isDisplayingNothing();
case 2:
if (!_context2.sent) {
_context2.next = 6;
break;
}
_context2.t0 = 0;
_context2.next = 7;
break;
case 6:
_context2.t0 = getRows().count();
case 7:
return _context2.abrupt("return", _context2.t0);
case 8:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return function getRowsCount() {
return _ref2.apply(this, arguments);
};
}();
var getRow = function getRow(rowIndex) {
return getRows().get(rowIndex);
};
var _getCell = function getCell(rowIndex, cellIndex) {
return getRow(rowIndex).$$('td').get(cellIndex);
};
var _getRowDetails = function getRowDetails(index) {
return base.$("tbody tr td[data-hook=\"".concat(index, "_details\"]"));
};
var _getHeaderCell = function getHeaderCell(index) {
return getHeader().$$('th').get(index);
};
var getSortableTitle = function getSortableTitle(index) {
return base.$("th [data-hook=\"".concat(index, "_title\"]"));
};
var getTitleInfoIcon = function getTitleInfoIcon(index) {
return base.$("th [data-hook=\"".concat(index, "_info_tooltip\"]"));
};
var getSortableTitleArrowDesc = function getSortableTitleArrowDesc(index) {
return base.$("th [data-hook=\"".concat(index, "_title\"] [data-hook=\"sort_arrow_dec\"]"));
};
return _objectSpread(_objectSpread({}, (0, _baseDriver.baseUniDriverFactory)(base)), {}, {
getRow: getRow,
getRowsCount: getRowsCount,
/** @deprecated Should be private */
getRowsWithClassCount: function () {
var _getRowsWithClassCount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(className) {
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return isDisplayingNothing();
case 2:
if (!_context3.sent) {
_context3.next = 6;
break;
}
_context3.t0 = 0;
_context3.next = 7;
break;
case 6:
_context3.t0 = getRows().filter(function (elem) {
return elem.hasClass(className);
}).count();
case 7:
return _context3.abrupt("return", _context3.t0);
case 8:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
function getRowsWithClassCount(_x) {
return _getRowsWithClassCount.apply(this, arguments);
}
return getRowsWithClassCount;
}(),
/** @deprecated Should be private */
getRowsWithDataHook: function () {
var _getRowsWithDataHook = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(dataHookName) {
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return isDisplayingNothing();
case 2:
if (!_context4.sent) {
_context4.next = 6;
break;
}
_context4.t0 = [];
_context4.next = 7;
break;
case 6:
_context4.t0 = base.$$("[data-hook=\"".concat(dataHookName, "\"]")).map(function (b) {
return b.getNative();
});
case 7:
return _context4.abrupt("return", _context4.t0);
case 8:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
function getRowsWithDataHook(_x2) {
return _getRowsWithDataHook.apply(this, arguments);
}
return getRowsWithDataHook;
}(),
// eslint-disable-line no-restricted-properties
/** @deprecated Should be private */
getRowWithDataHook: function () {
var _getRowWithDataHook = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(dataHookName) {
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return isDisplayingNothing();
case 2:
if (!_context5.sent) {
_context5.next = 6;
break;
}
_context5.t0 = null;
_context5.next = 7;
break;
case 6:
_context5.t0 = base.$("[data-hook=\"".concat(dataHookName, "\"]")).getNative();
case 7:
return _context5.abrupt("return", _context5.t0);
case 8:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
function getRowWithDataHook(_x3) {
return _getRowWithDataHook.apply(this, arguments);
}
return getRowWithDataHook;
}(),
// eslint-disable-line no-restricted-properties
/** Returns an array representing the text content of the cells in a given row `index`. */
getRowText: function () {
var _getRowText = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(index) {
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return isDisplayingNothing();
case 2:
if (!_context6.sent) {
_context6.next = 6;
break;
}
_context6.t0 = '';
_context6.next = 7;
break;
case 6:
_context6.t0 = getRows().get(index).$$('td').map(function (td) {
return td.text();
});
case 7:
return _context6.abrupt("return", _context6.t0);
case 8:
case "end":
return _context6.stop();
}
}
}, _callee6);
}));
function getRowText(_x4) {
return _getRowText.apply(this, arguments);
}
return getRowText;
}(),
/** @deprecated Should be private */
getRowClasses: function () {
var _getRowClasses = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(index) {
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.t0 = _values["default"];
_context7.next = 3;
return (0, _unidriver.ReactBase)(getRows().get(index))._DEPRECATED_getClassList();
case 3:
_context7.t1 = _context7.sent;
return _context7.abrupt("return", (0, _context7.t0)(_context7.t1));
case 5:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
function getRowClasses(_x5) {
return _getRowClasses.apply(this, arguments);
}
return getRowClasses;
}(),
/**
* Get header cell element: (columnIndex) => Element
* @deprecated Should be private
*/
getHeaderCell: function getHeaderCell(index) {
return _getHeaderCell(index).getNative();
},
// eslint-disable-line no-restricted-properties
getHeaderCellStyle: function getHeaderCellStyle(index) {
return _getHeaderCell(index)._prop('style');
},
getHeaderCellWidth: function () {
var _getHeaderCellWidth = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(index) {
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return _getHeaderCell(index)._prop('style');
case 2:
return _context8.abrupt("return", _context8.sent.width);
case 3:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
function getHeaderCellWidth(_x6) {
return _getHeaderCellWidth.apply(this, arguments);
}
return getHeaderCellWidth;
}(),
/**
* Get cell element: (rowIndex, columnIndex) => Element
* @deprecated Should be private
*/
getCell: function getCell(rowIndex, cellIndex) {
return _getCell(rowIndex, cellIndex).getNative();
},
// eslint-disable-line no-restricted-properties
getCellStyle: function getCellStyle(rowIndex, colIndex) {
return _getCell(rowIndex, colIndex)._prop('style');
},
getCellWidth: function getCellWidth(rowIndex, colIndex) {
return _getCell(rowIndex, colIndex)._prop('width');
},
isRowClickable: function () {
var _isRowClickable = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(index) {
return _regenerator["default"].wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return getRows().get(index).attr('data-clickable');
case 2:
return _context9.abrupt("return", !!_context9.sent);
case 3:
case "end":
return _context9.stop();
}
}
}, _callee9);
}));
function isRowClickable(_x7) {
return _isRowClickable.apply(this, arguments);
}
return isRowClickable;
}(),
isRowAnimated: function () {
var _isRowAnimated = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(index) {
return _regenerator["default"].wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
_context10.next = 2;
return getRows().get(index).attr('data-animated');
case 2:
return _context10.abrupt("return", !!_context10.sent);
case 3:
case "end":
return _context10.stop();
}
}
}, _callee10);
}));
function isRowAnimated(_x8) {
return _isRowAnimated.apply(this, arguments);
}
return isRowAnimated;
}(),
getTitles: function getTitles() {
return getHeader().$$('th').map(function (th) {
return th.text();
});
},
isDisplayingNothing: isDisplayingNothing,
isDisplayingHeaderOnly: function () {
var _isDisplayingHeaderOnly = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11() {
return _regenerator["default"].wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_context11.next = 2;
return hasHeader();
case 2:
_context11.t0 = _context11.sent;
if (!_context11.t0) {
_context11.next = 8;
break;
}
_context11.next = 6;
return getRowsCount();
case 6:
_context11.t1 = _context11.sent;
_context11.t0 = _context11.t1 === 0;
case 8:
return _context11.abrupt("return", _context11.t0);
case 9:
case "end":
return _context11.stop();
}
}
}, _callee11);
}));
function isDisplayingHeaderOnly() {
return _isDisplayingHeaderOnly.apply(this, arguments);
}
return isDisplayingHeaderOnly;
}(),
isDisplayingHeader: function isDisplayingHeader() {
return hasHeader();
},
hasChildWithId: function hasChildWithId(id) {
return base.$("#".concat(id)).exists();
},
clickRow: function clickRow(index, eventData) {
return getRow(index).click(eventData);
},
clickColumn: function clickColumn(rowIndex, colIndex, eventData) {
return _getCell(rowIndex, colIndex).click(eventData);
},
mouseEnterRow: function mouseEnterRow(index, eventData) {
return (0, _unidriver.ReactBase)(getRow(index)).mouseEnter(eventData);
},
mouseLeaveRow: function mouseLeaveRow(index, eventData) {
return (0, _unidriver.ReactBase)(getRow(index)).mouseLeave(eventData);
},
hasRowDetails: function hasRowDetails(index) {
return _getRowDetails(index).exists();
},
getRowDetailsText: function getRowDetailsText(index) {
return _getRowDetails(index).text();
},
hasSortableTitle: function hasSortableTitle(index) {
return getSortableTitle(index).exists();
},
hasInfoIcon: function hasInfoIcon(index) {
return getTitleInfoIcon(index).exists();
},
hasSortDescending: function hasSortDescending(index) {
return getSortableTitleArrowDesc(index).exists();
},
clickSort: function clickSort(index, eventData) {
return _getHeaderCell(index).click(eventData);
},
//
/** @deprecated Should be private */
getRowDetails: function getRowDetails(index) {
return _getRowDetails(index).getNative();
},
// eslint-disable-line no-restricted-properties
exists: function () {
var _exists = (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:
_context12.next = 2;
return isDisplayingNothing();
case 2:
return _context12.abrupt("return", !_context12.sent);
case 3:
case "end":
return _context12.stop();
}
}
}, _callee12);
}));
function exists() {
return _exists.apply(this, arguments);
}
return exists;
}()
});
};
exports.dataTableUniDriverFactory = dataTableUniDriverFactory;