UNPKG

wix-style-react

Version:
128 lines (101 loc) • 7.01 kB
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, { Component } from 'react'; import { children, once, any, oneOf, multiple, optional } from './CompositeValidation'; 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; }(Component); describe('CompositeValidation', function () { describe('children()', function () { it('should return an error if no rules are passed', function () { var validator = 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 = children(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 = children(once(Label), 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 = children(once(Label), once(Input)); expect(validator({ children: [React.createElement(Label, { key: 1 }), React.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 = children(optional(Label)); expect(validator({ children: [React.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 = children(optional(Label), once(Input)); expect(validator({ children: [React.createElement(Input, { key: 1 })] }, 'children', 'TextField')).toEqual(undefined); }); it('should pass if optional() component is in the middle', function () { var validator = children(once(Label), optional(Input), once(Label)); expect(validator({ children: [React.createElement(Label, { key: 1 }), React.createElement(Label, { key: 2 })] }, 'children', 'TextField')).toEqual(undefined); }); it('should pass if optional() component is the last one', function () { var validator = children(once(Input), optional(Label)); expect(validator({ children: [React.createElement(Input, { key: 1 })] }, 'children', 'TextField')).toEqual(undefined); }); }); describe('multiple()', function () { it('should return an error if multiple() components are missing', function () { var validator = children(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 = children(multiple(Label)); expect(validator({ children: [React.createElement(Label, { key: 1 })] }, 'children', 'TextField')).toEqual(undefined); }); it('should pass if several multiple() component exist', function () { var validator = children(multiple(Label)); expect(validator({ children: [React.createElement(Label, { key: 1 }), React.createElement(Label, { key: 2 })] }, 'children', 'TextField')).toEqual(undefined); }); }); }); describe('any()', function () { it('should pass if any() is being used', function () { var validator = children(any()); expect(validator({ children: [React.createElement(Label, { key: 1 }), React.createElement(Input, { key: 2 })] }, 'children', 'TextField')).toEqual(undefined); }); it('should pass if any() is being used as last option', function () { var validator = children(once(Label), any()); expect(validator({ children: [React.createElement(Label, { key: 1 }), React.createElement(Input, { key: 2 })] }, 'children', 'TextField')).toEqual(undefined); }); }); describe('oneOf()', function () { it('should return an error if oneOf() components are missing', function () { var validator = children(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 = children(oneOf(Input, Label)); expect(validator({ children: [React.createElement(Label, { key: 1 }), React.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 = children(oneOf(Input, Label)); expect(validator({ children: [React.createElement(Label, { key: 1 })] }, 'children', 'TextField')).toEqual(undefined); }); }); });