wix-style-react
Version:
wix-style-react
76 lines (61 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _testUtils = require('react-dom/test-utils');
var _testUtils2 = _interopRequireDefault(_testUtils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); } }
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 _testUtils2.default.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 _testUtils2.default.Simulate.click(itemsArray()[index].querySelector('button'));
})
}
};
return driver;
};
exports.default = popoverMenuDriverFactory;