@deskpro/react-forms
Version:
Forms library for React
309 lines (228 loc) • 11.8 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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _testUtils = require('react-dom/test-utils');
var _testUtils2 = _interopRequireDefault(_testUtils);
var _Component9 = require('../Component');
var _Component10 = _interopRequireDefault(_Component9);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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; } /**
* @copyright 2015, Prometheus Research, LLC
*/
var ReactHasContextSupport = !!/^0\.14\.\d/.exec(_react2.default.version);
describe('<Component />', function () {
it('allows to access form value passed via props', function () {
var FormComponent = function (_Component) {
_inherits(FormComponent, _Component);
function FormComponent() {
_classCallCheck(this, FormComponent);
return _possibleConstructorReturn(this, (FormComponent.__proto__ || Object.getPrototypeOf(FormComponent)).apply(this, arguments));
}
_createClass(FormComponent, [{
key: 'render',
value: function render() {
return _react2.default.createElement('div', null);
}
}]);
return FormComponent;
}(_Component10.default);
var formValue = {};
var instance = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponent, {
formValue: formValue
}));
assert(instance.formValue === formValue);
});
it('allows to select from form value passed via props', function () {
var _formValue;
var FormComponent = function (_Component2) {
_inherits(FormComponent, _Component2);
function FormComponent() {
_classCallCheck(this, FormComponent);
return _possibleConstructorReturn(this, (FormComponent.__proto__ || Object.getPrototypeOf(FormComponent)).apply(this, arguments));
}
_createClass(FormComponent, [{
key: 'render',
value: function render() {
return _react2.default.createElement('div', null);
}
}]);
return FormComponent;
}(_Component10.default);
var formValue = (_formValue = {}, _defineProperty(_formValue, 'a', {}), _defineProperty(_formValue, 1, {}), _defineProperty(_formValue, 'a.b', {}), _defineProperty(_formValue, 'select', function select(keyPath) {
keyPath = keyPath.join('.');
switch (keyPath) {
case 'a':
return formValue.a;
case '1':
return formValue[1];
case 'a.b':
return formValue['a.b'];
}
}), _formValue);
var instance = void 0;
instance = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponent, {
formValue: formValue,
select: 'a'
}));
assert(instance.formValue === formValue);
});
it('allows to access form value passed via context', function () {
if (!ReactHasContextSupport) {
return;
}
var FormComponentChild = function (_Component3) {
_inherits(FormComponentChild, _Component3);
function FormComponentChild() {
_classCallCheck(this, FormComponentChild);
return _possibleConstructorReturn(this, (FormComponentChild.__proto__ || Object.getPrototypeOf(FormComponentChild)).apply(this, arguments));
}
_createClass(FormComponentChild, [{
key: 'render',
value: function render() {
return _react2.default.createElement('div', null);
}
}]);
return FormComponentChild;
}(_Component10.default);
var FormComponentParent = function (_Component4) {
_inherits(FormComponentParent, _Component4);
function FormComponentParent() {
_classCallCheck(this, FormComponentParent);
return _possibleConstructorReturn(this, (FormComponentParent.__proto__ || Object.getPrototypeOf(FormComponentParent)).apply(this, arguments));
}
_createClass(FormComponentParent, [{
key: 'render',
value: function render() {
return _react2.default.createElement(FormComponentChild, { ref: 'child' });
}
}]);
return FormComponentParent;
}(_Component10.default);
var formValue = {};
var parent = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponentParent, {
formValue: formValue
}));
var child = parent.refs.child;
assert(parent.formValue === formValue);
assert(child.formValue === formValue);
});
it('allows to access form value passed via context (parent path)', function () {
if (!ReactHasContextSupport) {
return;
}
var child = void 0;
var FormComponentChild = function (_Component5) {
_inherits(FormComponentChild, _Component5);
function FormComponentChild() {
_classCallCheck(this, FormComponentChild);
return _possibleConstructorReturn(this, (FormComponentChild.__proto__ || Object.getPrototypeOf(FormComponentChild)).apply(this, arguments));
}
_createClass(FormComponentChild, [{
key: 'render',
value: function render() {
child = this;
return _react2.default.createElement('div', null);
}
}]);
return FormComponentChild;
}(_Component10.default);
var FormComponentParent = function (_Component6) {
_inherits(FormComponentParent, _Component6);
function FormComponentParent() {
_classCallCheck(this, FormComponentParent);
return _possibleConstructorReturn(this, (FormComponentParent.__proto__ || Object.getPrototypeOf(FormComponentParent)).apply(this, arguments));
}
_createClass(FormComponentParent, [{
key: 'render',
value: function render() {
return this.props.children;
}
}]);
return FormComponentParent;
}(_Component10.default);
var formValue = {};
var parent = _testUtils2.default.renderIntoDocument(_react2.default.createElement(
FormComponentParent,
{ formValue: formValue },
_react2.default.createElement(FormComponentChild, null)
));
assert(parent.formValue === formValue);
assert(child.formValue === formValue);
});
it('allows to select from form value passed via context', function () {
var _formValue2;
if (!ReactHasContextSupport) {
return;
}
var FormComponentChild = function (_Component7) {
_inherits(FormComponentChild, _Component7);
function FormComponentChild() {
_classCallCheck(this, FormComponentChild);
return _possibleConstructorReturn(this, (FormComponentChild.__proto__ || Object.getPrototypeOf(FormComponentChild)).apply(this, arguments));
}
_createClass(FormComponentChild, [{
key: 'render',
value: function render() {
return _react2.default.createElement('div', null);
}
}]);
return FormComponentChild;
}(_Component10.default);
var FormComponentParent = function (_Component8) {
_inherits(FormComponentParent, _Component8);
function FormComponentParent() {
_classCallCheck(this, FormComponentParent);
return _possibleConstructorReturn(this, (FormComponentParent.__proto__ || Object.getPrototypeOf(FormComponentParent)).apply(this, arguments));
}
_createClass(FormComponentParent, [{
key: 'render',
value: function render() {
var childSelectFormValue = this.props.childSelectFormValue;
return _react2.default.createElement(FormComponentChild, { ref: 'child', select: childSelectFormValue });
}
}]);
return FormComponentParent;
}(_Component10.default);
var formValue = (_formValue2 = {}, _defineProperty(_formValue2, 'a', {}), _defineProperty(_formValue2, 1, {}), _defineProperty(_formValue2, 'a.b', {}), _defineProperty(_formValue2, 'select', function select(keyPath) {
keyPath = keyPath.join('.');
switch (keyPath) {
case 'a':
return formValue.a;
case '1':
return formValue[1];
case 'a.b':
return formValue['a.b'];
}
}), _formValue2);
var parent = void 0;
var child = void 0;
parent = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponentParent, {
formValue: formValue,
childSelectFormValue: 'a'
}));
child = parent.refs.child;
assert(child.formValue === formValue.a);
parent = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponentParent, {
formValue: formValue,
childSelectFormValue: 1
}));
child = parent.refs.child;
assert(child.formValue === formValue[1]);
parent = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponentParent, {
formValue: formValue,
childSelectFormValue: ['a', 'b']
}));
child = parent.refs.child;
assert(child.formValue === formValue['a.b']);
parent = _testUtils2.default.renderIntoDocument(_react2.default.createElement(FormComponentParent, {
formValue: formValue,
childSelectFormValue: ['a.b']
}));
child = parent.refs.child;
assert(child.formValue === formValue['a.b']);
});
});