UNPKG

wix-style-react

Version:
241 lines (226 loc) • 9.09 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _testUtils = require('react-dom/test-utils'); var _testUtils2 = _interopRequireDefault(_testUtils); var _DropdownLayout = require('./DropdownLayout.scss'); var _DropdownLayout2 = _interopRequireDefault(_DropdownLayout); var _values = require('../utils/operators/values'); var _values2 = _interopRequireDefault(_values); var _utils = require('../../test/utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var dropdownLayoutDriverFactory = function dropdownLayoutDriverFactory(_ref) { var element = _ref.element; var contentContainer = element.childNodes[0]; var optionElements = element.querySelector('[data-hook=dropdown-layout-options]'); var optionElementAt = function optionElementAt(position) { return optionElements.childNodes[position]; }; var _optionsLength = function _optionsLength() { return optionElements.childNodes.length; }; var doIfOptionExists = function doIfOptionExists(position, onSuccess) { if (_optionsLength() <= position) { throw new Error('index out of bounds, try to get option ' + position + ' while only ' + _optionsLength() + ' options exists'); } return onSuccess(); }; var getOptionDriver = function getOptionDriver(position) { return doIfOptionExists(position, function () { return createOptionDriver(optionElementAt(position)); }); }; return { exists: function exists() { return !!element; }, isShown: function isShown() { return (0, _utils.isClassExists)(contentContainer, 'shown'); }, isDown: function isDown() { return (0, _utils.isClassExists)(contentContainer, 'down'); }, isUp: function isUp() { return (0, _utils.isClassExists)(contentContainer, 'up'); }, hasTheme: function hasTheme(theme) { return (0, _utils.isClassExists)(element, 'theme-' + theme); }, tabIndex: function tabIndex() { return element.tabIndex; }, optionsLength: function optionsLength() { return _optionsLength(); }, optionsScrollTop: function optionsScrollTop() { return optionElements.scrollTop; }, mouseEnterAtOption: function mouseEnterAtOption(position) { return doIfOptionExists(position, function () { return _testUtils2.default.Simulate.mouseEnter(optionElementAt(position)); }); }, mouseLeaveAtOption: function mouseLeaveAtOption(position) { return doIfOptionExists(position, function () { return _testUtils2.default.Simulate.mouseLeave(optionElementAt(position)); }); }, mouseClickOutside: function mouseClickOutside() { return document.body.dispatchEvent(new Event('mouseup', { cancelable: true })); }, isOptionExists: function isOptionExists(optionText) { return [].filter.call(optionElements.childNodes, function (opt) { return opt.textContent === optionText; }).length > 0; }, /** returns if an option is hovered. notice that it checks by index and __not__ by id */ isOptionHovered: function isOptionHovered(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'hovered'); }); }, isOptionSelected: function isOptionSelected(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'selected'); }); }, isOptionHoveredWithGlobalClassName: function isOptionHoveredWithGlobalClassName(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'wixstylereactHovered'); }); }, isOptionSelectedWithGlobalClassName: function isOptionSelectedWithGlobalClassName(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'wixstylereactSelected'); }); }, isOptionHeightSmall: function isOptionHeightSmall(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'smallHeight'); }); }, isOptionHeightBig: function isOptionHeightBig(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'bigHeight'); }); }, isLinkOption: function isLinkOption(position) { return optionElementAt(position).tagName.toLowerCase() === 'a'; }, classes: function classes() { return optionElements.className; }, pressDownKey: function pressDownKey() { return _testUtils2.default.Simulate.keyDown(element, { key: 'ArrowDown' }); }, pressUpKey: function pressUpKey() { return _testUtils2.default.Simulate.keyDown(element, { key: 'ArrowUp' }); }, pressEnterKey: function pressEnterKey() { return _testUtils2.default.Simulate.keyDown(element, { key: 'Enter' }); }, pressSpaceKey: function pressSpaceKey() { return _testUtils2.default.Simulate.keyDown(element, { key: ' ' }); }, pressTabKey: function pressTabKey() { return _testUtils2.default.Simulate.keyDown(element, { key: 'Tab' }); }, pressEscKey: function pressEscKey() { return _testUtils2.default.Simulate.keyDown(element, { key: 'Escape' }); }, optionContentAt: function optionContentAt(position) { return doIfOptionExists(position, function () { return optionElementAt(position).textContent; }); }, /** @deprecated Use optionDriver*/ optionAt: optionElementAt, /** Get option driver given an option index */ optionDriver: createOptionDriver, /** Get an array of all options including dividers (drivers) */ options: function options() { var drivers = []; for (var position = 0; position < _optionsLength(); position++) { drivers.push(getOptionDriver(position)); } return drivers; }, optionsContent: function optionsContent() { return (0, _values2.default)(optionElements.childNodes).map(function (option) { return option.textContent; }); }, clickAtOption: function clickAtOption(position) { return doIfOptionExists(position, function () { return _testUtils2.default.Simulate.mouseDown(optionElementAt(position)); }); }, clickAtOptionWithValue: function clickAtOptionWithValue(value) { var option = (0, _values2.default)(optionElements.childNodes).find(function (_option) { return _option.innerHTML === value; }); option && _testUtils2.default.Simulate.mouseDown(option); }, isOptionADivider: function isOptionADivider(position) { return doIfOptionExists(position, function () { return (0, _utils.isClassExists)(optionElementAt(position), 'divider'); }); }, mouseEnter: function mouseEnter() { return _testUtils2.default.Simulate.mouseEnter(element); }, mouseLeave: function mouseLeave() { return _testUtils2.default.Simulate.mouseLeave(element); }, hasTopArrow: function hasTopArrow() { return !!element.querySelector('.' + _DropdownLayout2.default.arrow); }, optionById: function optionById(optionId) { return this.optionByHook('dropdown-item-' + optionId); }, /** @deprecated This should be a private method since the hook include internal parts ('dropdown-divider-{id}, dropdown-item-{id})') */ optionByHook: function optionByHook(hook) { var option = optionElements.querySelector('[data-hook=' + hook + ']'); if (!option) { throw new Error('an option with data-hook ' + hook + ' was not found'); } return createOptionDriver(option); } }; }; var createOptionDriver = function createOptionDriver(option) { return { element: function element() { return option; }, mouseEnter: function mouseEnter() { return _testUtils2.default.Simulate.mouseEnter(option); }, mouseLeave: function mouseLeave() { return _testUtils2.default.Simulate.mouseLeave(option); }, isHovered: function isHovered() { return (0, _utils.isClassExists)(option, 'hovered'); }, isSelected: function isSelected() { return (0, _utils.isClassExists)(option, 'selected'); }, isHoveredWithGlobalClassName: function isHoveredWithGlobalClassName() { return (0, _utils.isClassExists)(option, 'wixstylereactHovered'); }, isSelectedWithGlobalClassName: function isSelectedWithGlobalClassName() { return (0, _utils.isClassExists)(option, 'wixstylereactSelected'); }, content: function content() { return option.textContent; }, click: function click() { return _testUtils2.default.Simulate.mouseDown(option); }, isDivider: function isDivider() { return (0, _utils.isClassExists)(option, 'divider'); } }; }; exports.default = dropdownLayoutDriverFactory;