UNPKG

wix-style-react

Version:
135 lines (104 loc) • 8.29 kB
'use strict'; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _CompositeValidation = require('./CompositeValidation'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; } var Label = function Label() { return null; }; var Input = function (_Component) { _inherits(Input, _Component); function Input() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Input); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Input.__proto__ || Object.getPrototypeOf(Input)).call.apply(_ref, [this].concat(args))), _this), _this.render = function () { return null; }, _temp), _possibleConstructorReturn(_this, _ret); } return 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: [_react2.default.createElement(Label, { key: 1 }), _react2.default.createElement(Input, { key: 2 })] }, '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: [_react2.default.createElement(Input, { key: 1 })] }, '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: [_react2.default.createElement(Input, { key: 1 })] }, '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: [_react2.default.createElement(Label, { key: 1 }), _react2.default.createElement(Label, { key: 2 })] }, '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: [_react2.default.createElement(Input, { key: 1 })] }, '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: [_react2.default.createElement(Label, { key: 1 })] }, '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: [_react2.default.createElement(Label, { key: 1 }), _react2.default.createElement(Label, { key: 2 })] }, '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: [_react2.default.createElement(Label, { key: 1 }), _react2.default.createElement(Input, { key: 2 })] }, '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: [_react2.default.createElement(Label, { key: 1 }), _react2.default.createElement(Input, { key: 2 })] }, '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: [_react2.default.createElement(Label, { key: 1 }), _react2.default.createElement(Input, { key: 2 })] }, '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: [_react2.default.createElement(Label, { key: 1 })] }, 'children', 'TextField')).toEqual(undefined); }); }); });