@wix/design-system
Version:
@wix/design-system
279 lines (278 loc) • 12.7 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireWildcard(require("react"));
var _CompositeValidation = require("./CompositeValidation");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Composite/CompositeValidation.spec.jsx",
_this2 = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var Label = function Label() {
return null;
};
var Input = /*#__PURE__*/function (_Component) {
function Input() {
var _this;
(0, _classCallCheck2["default"])(this, Input);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, Input, [].concat(args));
_this.render = function () {
return null;
};
return _this;
}
(0, _inherits2["default"])(Input, _Component);
return (0, _createClass2["default"])(Input);
}(_react.Component);
describe('CompositeValidation', function () {
describe('children()', function () {
it('should return an error if no rules are passed', function () {
var validator = (0, _CompositeValidation.children)();
expect(validator({}, 'children', 'TextField')).toEqual(new Error('TextField should have at least a single child declaration rule'));
});
describe('once()', function () {
it('should return an error if once() component is missing', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.once)(Input));
expect(validator({
children: []
}, 'children', 'TextField')).toEqual(new Error('TextField should have children of the following types in this order: Input (ONCE)'));
});
it('should return an error if multiple once components are missing', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.once)(Label), (0, _CompositeValidation.once)(Input));
expect(validator({
children: []
}, 'children', 'TextField')).toEqual(new Error('TextField should have children of the following types in this order: Label (ONCE), Input (ONCE)'));
});
it('should pass if component exists', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.once)(Label), (0, _CompositeValidation.once)(Input));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 51,
columnNumber: 26
}
}), /*#__PURE__*/_react["default"].createElement(Input, {
key: 2,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 51,
columnNumber: 45
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
});
describe('optional()', function () {
it('should return an error if optional() component is missing and there are no more rules', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.optional)(Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Input, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 63,
columnNumber: 34
}
})]
}, 'children', 'TextField')).toEqual(new Error('TextField should have children of the following types in this order: Label (OPTIONAL)'));
});
it('should pass if optional() component is missing but another component is present', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.optional)(Label), (0, _CompositeValidation.once)(Input));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Input, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 74,
columnNumber: 34
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
it('should pass if optional() component is in the middle', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.once)(Label), (0, _CompositeValidation.optional)(Input), (0, _CompositeValidation.once)(Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 82,
columnNumber: 26
}
}), /*#__PURE__*/_react["default"].createElement(Label, {
key: 2,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 82,
columnNumber: 45
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
it('should pass if optional() component is the last one', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.once)(Input), (0, _CompositeValidation.optional)(Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Input, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 92,
columnNumber: 34
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
});
describe('multiple()', function () {
it('should return an error if multiple() components are missing', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.multiple)(Label));
expect(validator({
children: []
}, 'children', 'TextField')).toEqual(new Error('TextField should have children of the following types in this order: Label (MULTIPLE)'));
});
it('should pass if at least one multiple() component exists', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.multiple)(Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 110,
columnNumber: 34
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
it('should pass if several multiple() component exist', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.multiple)(Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 118,
columnNumber: 26
}
}), /*#__PURE__*/_react["default"].createElement(Label, {
key: 2,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 118,
columnNumber: 45
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
});
});
describe('any()', function () {
it('should pass if any() is being used', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.any)());
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 132,
columnNumber: 24
}
}), /*#__PURE__*/_react["default"].createElement(Input, {
key: 2,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 132,
columnNumber: 43
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
it('should pass if any() is being used as last option', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.once)(Label), (0, _CompositeValidation.any)());
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 143,
columnNumber: 24
}
}), /*#__PURE__*/_react["default"].createElement(Input, {
key: 2,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 143,
columnNumber: 43
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
});
describe('oneOf()', function () {
it('should return an error if oneOf() components are missing', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.oneOf)(Input, Label));
expect(validator({
children: []
}, 'children', 'TextField')).toEqual(new Error('TextField should have children of the following types in this order: ONEOF(Input, Label)'));
});
it('should return an error if more than one of the oneOf() components is present', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.oneOf)(Input, Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 165,
columnNumber: 24
}
}), /*#__PURE__*/_react["default"].createElement(Input, {
key: 2,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 165,
columnNumber: 43
}
})]
}, 'children', 'TextField')).toEqual(new Error('TextField should have children of the following types in this order: ONEOF(Input, Label)'));
});
it('should pass if one of the oneOf() components exists', function () {
var validator = (0, _CompositeValidation.children)((0, _CompositeValidation.oneOf)(Input, Label));
expect(validator({
children: [/*#__PURE__*/_react["default"].createElement(Label, {
key: 1,
__self: _this2,
__source: {
fileName: _jsxFileName,
lineNumber: 179,
columnNumber: 32
}
})]
}, 'children', 'TextField')).toEqual(undefined);
});
});
});