wix-style-react
Version:
wix-style-react
169 lines (156 loc) • 6.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _testUtils = require('react-dom/test-utils');
var _testUtils2 = _interopRequireDefault(_testUtils);
var _values = require('../utils/operators/values');
var _values2 = _interopRequireDefault(_values);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var arbitraryEmptyElement = function arbitraryEmptyElement() {
return document.createElement('div');
};
var dataTableDriverFactory = function dataTableDriverFactory(_ref) {
var element = _ref.element;
// When a React component renders null in React 15, a Comment-Element is rendered to the DOM.
var _isDisplayingNothing = !element || element.nodeType === Node.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().
*/
element = _isDisplayingNothing ? arbitraryEmptyElement() : element;
var getHeader = function getHeader() {
return element.querySelector('thead');
};
var hasHeader = function hasHeader() {
return !!getHeader();
};
var getRows = function getRows() {
return element.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 element.querySelector('tbody tr td[data-hook="' + index + '_details"]');
};
var getHeaderCell = function getHeaderCell(index) {
return getHeader().querySelectorAll('th')[index];
};
var getSortableTitle = function getSortableTitle(index) {
return element.querySelector('th [data-hook="' + index + '_title"]');
};
var getTitleInfoIcon = function getTitleInfoIcon(index) {
return element.querySelector('th [data-hook="' + index + '_info_tooltip"]');
};
var getSortableTitleArrowDesc = function getSortableTitleArrowDesc(index) {
return element.querySelector('th [data-hook="' + index + '_title"] [data-hook="sort_arrow_dec"]');
};
return {
exists: function exists() {
return !!element;
},
getRow: getRow,
getRowsCount: getRowsCount,
getRowsWithClassCount: function getRowsWithClassCount(className) {
return _isDisplayingNothing ? 0 : (0, _values2.default)(getRows()).filter(function (elem) {
return elem.classList.contains(className);
}).length;
},
getRowsWithDataHook: function getRowsWithDataHook(dataHookName) {
return element.querySelectorAll('[data-hook="' + dataHookName + '"]');
},
getRowWithDataHook: function getRowWithDataHook(dataHookName) {
return element.querySelector('[data-hook="' + dataHookName + '"]');
},
/** Returns an array representing the text content of the cells in a given row `index`. */
getRowText: function getRowText(index) {
return (0, _values2.default)(getRows()[index].querySelectorAll('td')).map(function (td) {
return td.textContent;
});
},
getRowClasses: function getRowClasses(index) {
return (0, _values2.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].classList.contains('clickableDataRow');
},
isRowAnimated: function isRowAnimated(index) {
return getRows()[index].classList.contains('animatedDataRow');
},
getTitles: function getTitles() {
return (0, _values2.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 !!element.querySelector('#' + id);
},
clickRow: function clickRow(index, eventData) {
return _testUtils2.default.Simulate.click(getRow(index), eventData);
},
mouseEnterRow: function mouseEnterRow(index, eventData) {
return _testUtils2.default.Simulate.mouseEnter(getRow(index), eventData);
},
mouseLeaveRow: function mouseLeaveRow(index, eventData) {
return _testUtils2.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);
},
hasNewSortDescending: function hasNewSortDescending(index) {
return !!getSortableTitleArrowDesc(index);
},
hasSortDescending: function hasSortDescending(index) {
var sortableTitle = getSortableTitle(index);
return !!sortableTitle && sortableTitle.classList.contains('sortArrowAsc');
},
clickSort: function clickSort(index, eventData) {
return _testUtils2.default.Simulate.click(getHeaderCell(index), eventData);
},
getRowDetails: function getRowDetails(index) {
return _getRowDetails(index);
}
};
};
exports.default = dataTableDriverFactory;