wix-style-react
Version:
wix-style-react
66 lines (55 loc) • 2.05 kB
JavaScript
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
import ReactTestUtils from 'react-dom/test-utils';
var popoverMenuDriverFactory = function popoverMenuDriverFactory(_ref) {
var element = _ref.element;
var _menuItemDataHook = void 0;
var _parentElement = document.body;
// before accessing menu methods one need to init driver with menu-item data hook
var protect = function protect(fn) {
return function () {
if (!_menuItemDataHook) {
throw new Error('Before accessing menu fields init menu item data hook with "driver.given.menuItemDataHook(\'myDataHook\')" command');
}
return fn.apply(undefined, arguments);
};
};
var itemsArray = function itemsArray() {
return [].concat(_toConsumableArray(_parentElement.querySelectorAll(_menuItemDataHook.split(' ').reduce(function (q, hook) {
return q + ('[data-hook~="' + hook + '"]');
}, ''))));
};
var driver = {
exists: function exists() {
return !!element;
},
click: function click() {
return ReactTestUtils.Simulate.click(element);
},
init: {
menuItemDataHook: function menuItemDataHook(dataHook) {
_menuItemDataHook = dataHook;
return driver;
},
parentElement: function parentElement(elm) {
_parentElement = elm;
return driver;
}
},
menu: {
isShown: protect(function () {
return itemsArray().length > 0;
}),
itemsLength: protect(function () {
return itemsArray().length;
}),
itemContentAt: protect(function (index) {
return itemsArray()[index].querySelector('[data-hook="menu-item-text"]').innerHTML;
}),
clickItemAt: protect(function (index) {
return ReactTestUtils.Simulate.click(itemsArray()[index].querySelector('button'));
})
}
};
return driver;
};
export default popoverMenuDriverFactory;