UNPKG

class-o-mat

Version:

ALPHA VERSION ONLY: class-o-mats will help to create getter-setter functionality with validations that can be attached to a container class for easier coding. Some basic validations are included. class-o-mats also has the ability to store params as bloc

144 lines (117 loc) 3.97 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "NestedUsageTest"}); import 'mocha'; import { expect } from 'chai'; import { NONE, TEST_CLASS, CLASS_O_MAT, FIELD, FIELD_O_MAT, API_FIELD_O_MAT, CHILD_ADDER, COPY, STACK, QUEUE } from '../src/constants'; import ClassOMat from '../src/class-o-mat'; describe(`Test of creating params with validation.`, ()=> { class TestClass { TYPE = 'TEST' } var classOMat; var apiFieldOMat; var fieldOMat; var testClass; it(`create a basic classOMat created class`, ()=>{ classOMat = new ClassOMat('apiTestOMat'); classOMat.BaseClass(TestClass) .$(); testClass = new (classOMat.$())(); expect(classOMat.TYPE).to.equal(CLASS_O_MAT); expect(testClass.TYPE).to.equal('TEST'); } ); it(`try specifying an ApiFieldOMat`, ()=>{ apiFieldOMat = classOMat.apiField('path'); expect(apiFieldOMat.TYPE).to.equal(API_FIELD_O_MAT); } ); it(`test specify the field for ApiFieldOMat`, ()=>{ fieldOMat = apiFieldOMat.field(); expect(fieldOMat.TYPE).to.equal(FIELD_O_MAT); } ); it(`specify a validator for fieldOMat`, ()=>{ expect(fieldOMat.validate().int().$().TYPE).to.equal(FIELD_O_MAT); expect(typeof fieldOMat._params.validator).to.equal('function'); expect(apiFieldOMat._params.fieldOMat.TYPE).to.equal(FIELD_O_MAT); expect(typeof apiFieldOMat._params.fieldOMat._params.validator).to.equal('function'); } ); it(`get a Class from this`, ()=>{ testClass = new (classOMat.$())(); expect(testClass.TYPE).to.equal('TEST'); expect(typeof testClass.path).to.equal('function'); expect(Array.isArray(testClass.___fields)).to.equal(true); } ); it(`use apiField to add a function int which is getter setter which validates to integer`, ()=>{ testClass.path().as('age').$(); expect(typeof testClass.age).to.equal('function'); } ); it(`set the new "age" parameter. It should only be able to take an integer.`, ()=>{ testClass.age(9); const setErr =()=>testClass.age('heart of glass'); expect(testClass.age()).to.equal(9); expect(testClass._params.age).to.equal(9); expect(setErr).to.throw(Error); expect(Object.keys(testClass.params()).indexOf('age')).to.not.equal(-1); } ); it(`specify a dynamic ApiField`, ()=>{ classOMat.apiField('wildcard').dynamic(); } ); it(`should be able to specify new methods with the dynamic apiField that has no validation`, ()=>{ testClass = new (classOMat.$())(); testClass.wildcard().as$('anything'); expect(typeof testClass.wildcard).to.equal('function'); expect(typeof testClass.anything).to.equal('function'); testClass.anything(10); expect(testClass.anything()).to.equal(10); testClass.anything('10'); expect(testClass.anything()).to.equal('10'); } ); it(`should be able to add a validation to the dynamic apiField`, ()=>{ testClass.wildcard().validate().string().$().as$('name') testClass.wildcard().as('income').validate().uint().$().$(); expect(typeof testClass.name).to.equal('function'); expect(typeof testClass.income).to.equal('function'); testClass.name('Frank'); expect(testClass.name()).to.equal('Frank'); testClass.income(40); expect(testClass.income()).to.equal(40); const nameErr = ()=>testClass.name(54); const incomeErr=()=>testClass.income(false); expect(nameErr).to.throw(Error); expect(incomeErr).to.throw(Error); } ); } );