UNPKG

typed-component

Version:
2 lines 8.4 kB
"use strict";var _index=require("./index");function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj}}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj}}return _typeof(obj)}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call}return _assertThisInitialized(self)}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return self}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o)};return _getPrototypeOf(o)}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function")}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o};return _setPrototypeOf(o,p)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}describe("test type by constructor",function(){test("should return true for basic JS types",function(){expect((0,_index.isType)(RegExp)(/regexp/)).toBe(true);expect((0,_index.isType)(RegExp)(new RegExp("foo"))).toBe(true);expect((0,_index.isType)(Object)({a:1})).toBe(true);expect((0,_index.isType)(Object)(new Object({b:2}))).toBe(true);expect((0,_index.isType)(Boolean)(true)).toBe(true);expect((0,_index.isType)(Boolean)(false)).toBe(true);expect((0,_index.isType)(String)("xs")).toBe(true);expect((0,_index.isType)(String)("")).toBe(true);expect((0,_index.isType)(String)("")).toBe(true);expect((0,_index.isType)(String)("")).toBe(true);expect((0,_index.isType)(Number)(1)).toBe(true);expect((0,_index.isType)(Number)(NaN)).toBe(true);expect((0,_index.isType)(Number)(Infinity)).toBe(true);expect((0,_index.isType)(Number)(0.34)).toBe(true);expect((0,_index.isType)(Number)(3.1e12)).toBe(true);expect((0,_index.isType)(Array)([1,2])).toBe(true);expect((0,_index.isType)(Array)(new Array(1,2))).toBe(true);expect((0,_index.isType)(Map)(new Map)).toBe(true);expect((0,_index.isType)(WeakMap)(new WeakMap)).toBe(true);expect((0,_index.isType)(Set)(new Set)).toBe(true);expect((0,_index.isType)(WeakSet)(new WeakSet)).toBe(true)});test("should return false is it not a type",function(){expect((0,_index.isType)(Object)("{ a: 1 }")).toBe(false);expect((0,_index.isType)(Boolean)("true")).toBe(false);expect((0,_index.isType)(String)(1)).toBe(false);expect((0,_index.isType)(String)([])).toBe(false);expect((0,_index.isType)(String)({})).toBe(false);expect((0,_index.isType)(String)()).toBe(false);expect((0,_index.isType)(Number)("1")).toBe(false)});test("should recognize instance of classes",function(){var Car=function Car(){_classCallCheck(this,Car)};expect((0,_index.isType)(Car)(new Car)).toBe(true);var Porsche=function(_Car){_inherits(Porsche,_Car);function Porsche(){_classCallCheck(this,Porsche);return _possibleConstructorReturn(this,_getPrototypeOf(Porsche).apply(this,arguments))}return Porsche}(Car);expect((0,_index.isType)(Car)(new Porsche)).toBe(false);expect((0,_index.isType)(Object)(new Array)).toBe(false)})});describe("isPrimitive",function(){test(" should recognize primitive types",function(){expect((0,_index.isPrimitive)("hola")).toBe(true);expect((0,_index.isPrimitive)(1)).toBe(true);expect((0,_index.isPrimitive)(false)).toBe(true);expect((0,_index.isPrimitive)(NaN)).toBe(true);expect((0,_index.isPrimitive)(undefined)).toBe(true);expect((0,_index.isPrimitive)(null)).toBe(true);expect((0,_index.isPrimitive)({})).toBe(false);expect((0,_index.isPrimitive)(/regex/)).toBe(false);expect((0,_index.isPrimitive)(function(){})).toBe(false);expect((0,_index.isPrimitive)([])).toBe(false)})});describe("isConstructor",function(){test("should detect if a value can be instantiated with new",function(){expect((0,_index.isConstructor)(Object)).toBe(true);expect((0,_index.isConstructor)(Array)).toBe(true);expect((0,_index.isConstructor)(console)).toBe(false);expect((0,_index.isConstructor)(12)).toBe(false);expect((0,_index.isConstructor)(function(){})).toBe(false);expect((0,_index.isConstructor)(function name(){})).toBe(false);var fn=function fn(){};expect((0,_index.isConstructor)(fn)).toBe(false)})});describe("is normal function",function(){test("should detect if a function is anonymous or his name starts with lowercase (not a class)",function(){expect((0,_index.isNormalFunction)(Object)).toBe(false);expect((0,_index.isNormalFunction)(function(){})).toBe(true);expect((0,_index.isNormalFunction)(function name(){})).toBe(true);expect((0,_index.isNormalFunction)(function(){})).toBe(true);expect((0,_index.isNormalFunction)("asdasd")).toBe(false);expect((0,_index.isNormalFunction)(1)).toBe(false)})});describe("is Valid type",function(){test("should check RegExp",function(){expect((0,_index.isValidType)(/.ola/,"hola")).toBe(true);expect((0,_index.isValidType)(/.ola/,"sola")).toBe(true);expect((0,_index.isValidType)(/.ola/,"ola")).toBe(false)});test("should work for constructors",function(){expect((0,_index.isValidType)(String,"a")).toBe(true);expect((0,_index.isValidType)(String,1)).toBe(false);expect((0,_index.isValidType)(RegExp,/12/)).toBe(true)});test("should work for primitives",function(){expect((0,_index.isValidType)("a","a")).toBe(true);expect((0,_index.isValidType)("a","a")).toBe(true);expect((0,_index.isValidType)("a","b")).toBe(false);expect((0,_index.isValidType)(1,1)).toBe(true);expect((0,_index.isValidType)(2,1)).toBe(false);expect((0,_index.isValidType)(true,true)).toBe(true);expect((0,_index.isValidType)(undefined,undefined)).toBe(true);expect((0,_index.isValidType)(null,null)).toBe(true)});test("should work for enums of constructors",function(){expect((0,_index.isValidType)([String,Function],"a")).toBe(true);expect((0,_index.isValidType)([String,Function],1)).toBe(false);expect((0,_index.isValidType)([String,Object],[])).toBe(false)});test("should work for enums of primitives",function(){expect((0,_index.isValidType)(["b","a"],"a")).toBe(true);expect((0,_index.isValidType)(["b","a"],"c")).toBe(false);expect((0,_index.isValidType)([undefined,String],"c")).toBe(true);expect((0,_index.isValidType)([undefined,String],undefined)).toBe(true);expect((0,_index.isValidType)([undefined,Number],"c")).toBe(false)});test("should work for shapes",function(){expect((0,_index.isValidType)({a:Number},{a:1})).toBe(true);expect((0,_index.isValidType)({a:Number},{a:"a"})).toBe(false);expect((0,_index.isValidType)({a:[Number,String]},{a:"a"})).toBe(true);expect((0,_index.isValidType)({a:[Number,String],b:[undefined,"b"]},{a:"a"})).toBe(true);expect((0,_index.isValidType)({a:[Number,String],b:[undefined,"b"]},{a:"a",b:"b"})).toBe(true);expect((0,_index.isValidType)({a:[Number,String],b:[undefined,"b"]},{a:"a",b:"c"})).toBe(false)});test("should work for custom validators functions",function(){expect((0,_index.isValidType)(function(value){return value>5},6)).toBe(true);expect((0,_index.isValidType)(function(value){return value===5},6)).toBe(false);expect((0,_index.isValidType)(function(value,propName,props){return propName===value},6,{a:"a"},"a")).toBe(false)});test("should use a camelCase function or anonymous",function(){function validator(v){return true}function Validator(v){return true}expect((0,_index.isValidType)(validator,3)).toBe(true);expect((0,_index.isValidType)(Validator,3)).toBe(false)});test("should throw",function(){expect(function(){(0,_index.isValidType)(function(){throw"asd"},6)}).toThrow()});test("should throw custom message",function(){expect(function(){(0,_index.isValidType)(function(value){if(value>5){throw"must be greater than 5"}},6)}).toThrow("must be greater than 5")});test("should throw custom Error",function(){expect(function(){(0,_index.isValidType)(function(value){if(value>5){throw new Error("must be greater than 5")}},6)}).toThrow("must be greater than 5")})}); //# sourceMappingURL=unit.test.js.map