wix-style-react
Version:
wix-style-react
116 lines (98 loc) • 4.01 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 React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import styles from './RichTextArea.scss';
var richTextAreaDriverFactory = function richTextAreaDriverFactory(_ref) {
var element = _ref.element,
wrapper = _ref.wrapper,
component = _ref.component;
var getButtons = function getButtons() {
return [].concat(_toConsumableArray(element.querySelectorAll('[data-hook*="rich-text-area-button"]')));
};
var getEditorWrapper = function getEditorWrapper() {
return element.querySelector('[data-hook=editor-wrapper]');
};
var getButtonType = function getButtonType(button) {
return button.getAttribute('data-hook').replace(/^rich-text-area-button-/, '');
};
var getImage = function getImage() {
return element.querySelector('[data-hook=editor-image]');
};
var getButtonByType = function getButtonByType(type) {
return getButtons().find(function (button) {
return getButtonType(button) === type;
});
};
var clickButtonByType = function clickButtonByType(type) {
return function () {
return ReactTestUtils.Simulate.mouseDown(getButtonByType(type));
};
};
var getDefaultBlock = function getDefaultBlock() {
return element.querySelector("[data-key='defaultBlock']");
};
return {
/** returns if the element exists */
exists: function exists() {
return !!element;
},
/** returns an array with the button type names */
getButtonTypes: function getButtonTypes() {
return getButtons().map(getButtonType);
},
/** click the bold button */
clickBoldButton: clickButtonByType('bold'),
/** click the italic button */
clickItalicButton: clickButtonByType('italic'),
/** click the underline button */
clickUnderlineButton: clickButtonByType('underline'),
/** click the image button */
clickImageButton: clickButtonByType('image'),
/** click the unordered list button */
clickUnorderedListButton: clickButtonByType('unordered-list'),
/** click the ordered list button */
clickOrderedListButton: clickButtonByType('ordered-list'),
/** returns the text content of the editor */
getContent: function getContent() {
return element.childNodes[1].textContent;
},
/** enters the supplied text to the editor */
enterText: function enterText(text) {
var props = { value: text, isAppend: true };
var ClonedWithProps = React.cloneElement.apply(React, [component, Object.assign({}, component.props, props)].concat(_toConsumableArray(component.props.children || [])));
ReactDOM.render(React.createElement(
'div',
{ ref: function ref(r) {
return element = r;
} },
ClonedWithProps
), wrapper);
},
/** returns if the error indicator is visible */
isErrorIndicatorVisible: function isErrorIndicatorVisible() {
return Boolean(element.classList.contains(styles.withError));
},
/** returns if disabled */
isDisabled: function isDisabled() {
return getButtons().every(function (button) {
return button.classList.contains(styles.disabled);
}) && element.childNodes[1].classList.contains(styles.disabled);
},
isImageExist: function isImageExist() {
return !!getImage();
},
/** returns if the add image button exists */
isAddImageButtonExist: function isAddImageButtonExist() {
return !!getButtonByType('image');
},
/** returns if the editor is resizable */
isResizable: function isResizable() {
return getEditorWrapper().classList.contains(styles.resizable);
},
isDefaultBlockExist: function isDefaultBlockExist() {
return getDefaultBlock();
}
};
};
export default richTextAreaDriverFactory;