react-hold
Version:
Hold the empty presentational components in react.js
115 lines (94 loc) • 4.17 kB
JavaScript
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 { mount } from 'enzyme';
import HoC from '../HoC';
import Square from '../holders/Square';
var P = HoC('p', function (props) {
return !props.children;
});
describe('Holdable HoC component', function () {
it('Create correctly', function () {
expect(P).toHaveProperty('displayName', 'Hold(p)');
expect(P.propTypes.holder).not.toBeNull();
expect(P.propTypes.holderProps).not.toBeNull();
expect(P.propTypes.props).not.toBeNull();
expect(P.propTypes.innerRef).not.toBeNull();
expect(P.defaultProps.holder).not.toBeUndefined();
expect(P.defaultProps.holderProps).not.toBeUndefined();
expect(P.defaultProps.props).not.toBeUndefined();
expect(P.defaultProps.innerRef).not.toBeUndefined();
});
it('Do not hold', function () {
var wrapper = mount(React.createElement(
Container,
{ presentation: P },
'foo'
));
expect(wrapper).toMatchSnapshot();
});
it('Hold', function () {
var wrapper = mount(React.createElement(Container, { presentation: P }));
expect(wrapper).toMatchSnapshot();
wrapper.setState({ children: 'I am child, then cancel hold.' });
expect(wrapper).toMatchSnapshot();
wrapper.setState({
props: {
name: 'bar',
holder: Square,
holderProps: {
color: 'green',
width: 40,
height: 30
}
},
children: ''
});
expect(wrapper).toMatchSnapshot();
});
});
var Container = function (_React$Component) {
_inherits(Container, _React$Component);
function Container() {
var _ref;
_classCallCheck(this, Container);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var _this = _possibleConstructorReturn(this, (_ref = Container.__proto__ || Object.getPrototypeOf(Container)).call.apply(_ref, [this].concat(args)));
_this.state = {
props: {
name: 'foo',
holder: undefined,
holderProps: null
},
children: _this.props.children
};
return _this;
}
_createClass(Container, [{
key: 'render',
value: function render() {
var presentation = this.props.presentation;
var _state = this.state,
props = _state.props,
children = _state.children;
return React.createElement(
'div',
null,
React.createElement(presentation, props, children)
);
}
}]);
return Container;
}(React.Component);
Container.propTypes = {
presentation: PropTypes.func.isRequired,
children: PropTypes.string
};
Container.defaultProps = {
children: ''
};