wix-style-react
Version:
181 lines (156 loc) • 6.62 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));
var _values = _interopRequireDefault(require("../../utils/operators/values"));
var arbitraryEmptyElement = function arbitraryEmptyElement() {
return document.createElement('div');
};
var dataTableDriverFactory = function dataTableDriverFactory(_ref) {
var element = _ref.element;
// 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 = !element || element.nodeType === COMMENT_NODE;
/* 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().
*/
var protectedElement = _isDisplayingNothing ? arbitraryEmptyElement() : element;
var getHeader = function getHeader() {
return protectedElement.querySelector('thead');
};
var hasHeader = function hasHeader() {
return !!getHeader();
};
var getRows = function getRows() {
return protectedElement.querySelectorAll('tbody tr[data-table-row="dataTableRow"]');
};
var getRowsCount = function getRowsCount() {
return getRows().length;
};
var getRow = function getRow(rowIndex) {
return getRows()[rowIndex];
};
var getCell = function getCell(rowIndex, cellIndex) {
return getRow(rowIndex).querySelectorAll('td')[cellIndex];
};
var _getRowDetails = function getRowDetails(index) {
return protectedElement.querySelector("tbody tr td[data-hook=\"".concat(index, "_details\"]"));
};
var getHeaderCell = function getHeaderCell(index) {
return getHeader().querySelectorAll('th')[index];
};
var getSortableTitle = function getSortableTitle(index) {
return protectedElement.querySelector("th [data-hook=\"".concat(index, "_title\"]"));
};
var getTitleInfoIcon = function getTitleInfoIcon(index) {
return protectedElement.querySelector("th [data-hook=\"".concat(index, "_info_tooltip\"]"));
};
var getSortableTitleArrowDesc = function getSortableTitleArrowDesc(index) {
return element.querySelector("th [data-hook=\"".concat(index, "_title\"] [data-hook=\"sort_arrow_dec\"]"));
};
return {
exists: function exists() {
return !_isDisplayingNothing;
},
getRow: getRow,
getRowsCount: getRowsCount,
getRowsWithClassCount: function getRowsWithClassCount(className) {
return _isDisplayingNothing ? 0 : (0, _values["default"])(getRows()).filter(function (elem) {
return elem.classList.contains(className);
}).length;
},
getRowsWithDataHook: function getRowsWithDataHook(dataHookName) {
return protectedElement.querySelectorAll("[data-hook=\"".concat(dataHookName, "\"]"));
},
getRowWithDataHook: function getRowWithDataHook(dataHookName) {
return protectedElement.querySelector("[data-hook=\"".concat(dataHookName, "\"]"));
},
/** Returns an array representing the text content of the cells in a given row `index`. */
getRowText: function getRowText(index) {
return (0, _values["default"])(getRows()[index].querySelectorAll('td')).map(function (td) {
return td.textContent;
});
},
getRowClasses: function getRowClasses(index) {
return (0, _values["default"])(getRows()[index].classList);
},
/** Get header cell element: (columnIndex) => Element */
getHeaderCell: getHeaderCell,
getHeaderCellStyle: function getHeaderCellStyle(index) {
return getHeaderCell(index).style;
},
getHeaderCellWidth: function getHeaderCellWidth(index) {
return getHeaderCell(index).style.width;
},
/** Get cell element: (rowIndex, columnIndex) => Element */
getCell: getCell,
getCellStyle: function getCellStyle(rowIndex, colIndex) {
return getCell(rowIndex, colIndex).style;
},
getCellWidth: function getCellWidth(rowIndex, colIndex) {
return getCell(rowIndex, colIndex).width;
},
isRowClickable: function isRowClickable(index) {
return getRows()[index].hasAttribute('data-clickable');
},
isRowAnimated: function isRowAnimated(index) {
return getRows()[index].hasAttribute('data-animated');
},
getTitles: function getTitles() {
return (0, _values["default"])(getHeader().querySelectorAll('th')).map(function (th) {
return th.textContent;
});
},
isDisplayingNothing: function isDisplayingNothing() {
return _isDisplayingNothing;
},
isDisplayingHeaderOnly: function isDisplayingHeaderOnly() {
return hasHeader() && getRowsCount() === 0;
},
isDisplayingHeader: function isDisplayingHeader() {
return hasHeader();
},
hasChildWithId: function hasChildWithId(id) {
return !!protectedElement.querySelector("#".concat(id));
},
clickRow: function clickRow(index, eventData) {
return _testUtils["default"].Simulate.click(getRow(index), eventData);
},
clickColumn: function clickColumn(rowIndex, colIndex, eventData) {
return _testUtils["default"].Simulate.click(getCell(rowIndex, colIndex), eventData);
},
mouseEnterRow: function mouseEnterRow(index, eventData) {
return _testUtils["default"].Simulate.mouseEnter(getRow(index), eventData);
},
mouseLeaveRow: function mouseLeaveRow(index, eventData) {
return _testUtils["default"].Simulate.mouseLeave(getRow(index), eventData);
},
hasRowDetails: function hasRowDetails(index) {
return !!_getRowDetails(index);
},
getRowDetailsText: function getRowDetailsText(index) {
return _getRowDetails(index).textContent;
},
hasSortableTitle: function hasSortableTitle(index) {
return !!getSortableTitle(index);
},
hasInfoIcon: function hasInfoIcon(index) {
return !!getTitleInfoIcon(index);
},
hasSortDescending: function hasSortDescending(index) {
return !!getSortableTitleArrowDesc(index);
},
clickSort: function clickSort(index, eventData) {
return _testUtils["default"].Simulate.click(getHeaderCell(index), eventData);
},
getRowDetails: function getRowDetails(index) {
return _getRowDetails(index);
}
};
};
var _default = dataTableDriverFactory;
exports["default"] = _default;