UNPKG

wix-style-react

Version:
245 lines (224 loc) • 7.95 kB
"use strict"; 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 _Tooltip = require("wix-ui-core/dist/src/components/tooltip/Tooltip.driver"); var _constants = require("./constants"); var _DataAttr = _interopRequireDefault(require("./DataAttr")); var inputDriverFactory = function inputDriverFactory(_ref) { var element = _ref.element, eventTrigger = _ref.eventTrigger; var input = element && element.querySelector('input'); var clearButton = element && element.querySelector("[data-hook=input-clear-button]"); var customAffixNode = element && element.querySelector("[data-hook=\"custom-affix\"]"); var iconAffixNode = element && element.querySelector("[data-hook=\"icon-affix\"]"); var menuArrowNode = element && element.querySelector("[data-hook=\"".concat(_constants.dataHooks.menuArrow, "\"]")); var getName = function getName() { return input.getAttribute('name'); }; var getType = function getType() { return input.getAttribute('type'); }; var getMaxLength = function getMaxLength() { return input.getAttribute('maxlength'); }; var driver = { trigger: function trigger(_trigger, event) { return _testUtils["default"].Simulate[_trigger](input, event); }, focus: function focus(options) { input.focus(options); _testUtils["default"].Simulate.focus(input); }, blur: function blur() { input.blur(); _testUtils["default"].Simulate.blur(input); }, getName: getName, getMaxLength: getMaxLength, getType: getType, keyDown: function keyDown(key) { return _testUtils["default"].Simulate.keyDown(input, { key: key }); }, click: function click() { return _testUtils["default"].Simulate.click(input); }, clickCustomAffix: function clickCustomAffix() { return _testUtils["default"].Simulate.click(customAffixNode); }, clickClear: function clickClear() { return _testUtils["default"].Simulate.click(clearButton); }, clickIconAffix: function clickIconAffix() { return _testUtils["default"].Simulate.click(iconAffixNode); }, clickMenuArrow: function clickMenuArrow() { return _testUtils["default"].Simulate.click(menuArrowNode); }, mouseOver: function mouseOver() { return _testUtils["default"].Simulate.mouseOver(input); }, mouseOut: function mouseOut() { return _testUtils["default"].Simulate.mouseOut(input); }, clearText: function clearText() { return driver.enterText(''); }, enterText: function enterText(text) { if (driver.getReadOnly() || driver.isDisabled()) { return; } input.value = text; _testUtils["default"].Simulate.change(input, { target: { name: getName(), type: getType(), value: text } }); }, getValue: function getValue() { return input.value; }, getText: function getText() { return input.value; }, getPlaceholder: function getPlaceholder() { return input.placeholder; }, getPattern: function getPattern() { return input.pattern; }, getDefaultValue: function getDefaultValue() { return input.defaultValue; }, getTabIndex: function getTabIndex() { return input.tabIndex; }, getReadOnly: function getReadOnly() { return input.readOnly; }, getDisabled: function getDisabled() { return input.disabled; }, getTextOverflow: function getTextOverflow() { return input.style['text-overflow']; }, getAriaLabel: function getAriaLabel() { return input.getAttribute('aria-label'); }, getAriaControls: function getAriaControls() { return input.getAttribute('aria-controls'); }, getAriaDescribedby: function getAriaDescribedby() { return input.getAttribute('aria-describedby'); }, getAutocomplete: function getAutocomplete() { return input.getAttribute('autocomplete'); }, getRequired: function getRequired() { return input.required; }, hasPrefix: function hasPrefix() { return element.hasAttribute(_DataAttr["default"].PREFIX); }, hasSuffix: function hasSuffix() { return !!element.querySelector("[data-hook=\"".concat(_constants.dataHooks.suffixes, "\"]")); }, prefixComponentExists: function prefixComponentExists(style) { return element.hasAttribute(_DataAttr["default"].PREFIX) && !!element.querySelector(style); }, suffixComponentExists: function suffixComponentExists(style) { return !!element.querySelector("[data-hook=\"".concat(_constants.dataHooks.suffixes, "\"] ").concat(style)); }, getDataHook: function getDataHook() { return element.getAttribute('data-hook'); }, getCustomAffix: function getCustomAffix() { return customAffixNode.textContent; }, hasMenuArrow: function hasMenuArrow() { return !!menuArrowNode; }, hasClearButton: function hasClearButton() { return !!clearButton; }, isRTL: function isRTL() { return element.getAttribute('dir') === 'rtl'; }, isFocusedStyle: function isFocusedStyle() { return element.hasAttribute(_DataAttr["default"].FOCUS); }, isHoveredStyle: function isHoveredStyle() { return element.hasAttribute(_DataAttr["default"].HOVER); }, isDisabled: function isDisabled() { return element.hasAttribute(_DataAttr["default"].DISABLED); }, isOfSize: function isOfSize(size) { return element.getAttribute(_DataAttr["default"].SIZE) === size; }, getSize: function getSize() { return element.getAttribute(_DataAttr["default"].SIZE); }, isFocus: function isFocus() { return document.activeElement === input; }, exists: function exists() { return !!(element && element.querySelector('input')); }, startComposing: function startComposing() { return _testUtils["default"].Simulate.compositionStart(input); }, endComposing: function endComposing() { return _testUtils["default"].Simulate.compositionEnd(input); }, getCursorLocation: function getCursorLocation() { return input.selectionStart; }, getRootElementClasses: function getRootElementClasses() { return element.classList; }, getInputElementClasses: function getInputElementClasses() { return input.classList; }, hasRightBorderRadius: function hasRightBorderRadius() { return !element.hasAttribute(_DataAttr["default"].RIGHTBORDERRADIUS); }, hasLeftBorderRadius: function hasLeftBorderRadius() { return !element.hasAttribute(_DataAttr["default"].LEFTBORDERRADIUS); }, isCustomInput: function isCustomInput() { return input.getAttribute('data-hook') === 'wsr-custom-input'; }, // Status /** Return true if the given status is displayed */ hasStatus: function hasStatus(status) { return element.getAttribute(_DataAttr["default"].STATUS) === status; }, /** If there's a status message, returns its text value */ getStatusMessage: function getStatusMessage() { var tooltipText = null; var tooltipDriver = (0, _Tooltip.tooltipDriverFactory)({ element: element.querySelector('[data-hook="status-indicator-tooltip"]'), eventTrigger: eventTrigger }); if (tooltipDriver.exists()) { tooltipDriver.mouseEnter(); var contentElement = tooltipDriver.getContentElement(); if (contentElement) { tooltipText = contentElement.textContent; } } return tooltipText; } }; return driver; }; var _default = inputDriverFactory; exports["default"] = _default;