wix-style-react
Version:
wix-style-react
108 lines (82 loc) • 4.53 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import PropTypes from 'prop-types';
import { stub } from 'sinon';
import { mount } from 'enzyme';
import * as Utils from '../utils';
import withTooltip from './index';
import Tooltip from '../../../Tooltip';
describe('withTooltip hoc', function () {
var _class, _temp;
stub(Utils, 'getWidth');
afterEach(function () {
Utils.getWidth.reset();
Utils.getWidth.resetBehavior();
});
afterAll(function () {
Utils.getWidth.restore();
});
var type = 'compactSide';
var item = { id: 0, title: 'Title' };
var Component = (_temp = _class = function (_React$Component) {
_inherits(Component, _React$Component);
function Component() {
_classCallCheck(this, Component);
return _possibleConstructorReturn(this, (Component.__proto__ || Object.getPrototypeOf(Component)).apply(this, arguments));
}
_createClass(Component, [{
key: 'render',
value: function render() {
var _this2 = this;
return React.createElement(
'div',
null,
React.createElement('div', { ref: function ref(el) {
return _this2.props.initHasTooltip(el);
} })
);
}
}]);
return Component;
}(React.Component), _class.propTypes = {
initHasTooltip: PropTypes.func
}, _temp);
var aComponent = function aComponent() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return mount(React.createElement(withTooltip(Component), _extends({ type: type, item: item }, props)));
};
it('does not add a tooltip for non compactSide types', function () {
stubSizes({ elementSize: 10, parentSize: 100 });
var comp = aComponent({ type: '' });
expect(comp.find(Tooltip).exists()).toBeFalsy();
});
it('adds a tooltip if element exceeds it` parent', function () {
stubSizes({ elementSize: 20, parentSize: 30 });
var comp = aComponent();
expect(comp.find(Tooltip).exists()).toBeTruthy();
});
it('does not add a tooltip if element does not exceed it`s parent', function () {
stubSizes({ elementSize: 100, parentSize: 100 });
var comp = aComponent();
expect(comp.find(Tooltip).exists()).toBeFalsy();
});
it('passes props to the wrapped component', function () {
var props = { p1: true, p2: 'true', p3: 1 };
var comp = aComponent(props);
expect(comp.find(Component).props()).toMatchObject(props);
});
function stubSizes(_ref) {
var elementSize = _ref.elementSize,
parentSize = _ref.parentSize;
// First render
Utils.getWidth.onCall(0).returns(parentSize);
Utils.getWidth.onCall(1).returns(elementSize);
// Second render
Utils.getWidth.onCall(2).returns(parentSize);
Utils.getWidth.onCall(3).returns(elementSize);
}
});