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
109 lines (93 loc) • 3.99 kB
JavaScript
import Bunyan from 'bunyan';
const log = Bunyan.createLogger({name: "ValidationTest"});
import 'mocha';
import { expect } from 'chai';
import { NONE, CLASS_O_MAT } from '../src/constants';
import ClassOMat from '../src/class-o-mat';
describe(`Test of required parameter in fields`,
()=>{
class TestClass {
TYPE = 'TEST'
}
var classOMat;
var testClass;
it(`create a basic classOMat with three fields.
Two of the fields are required.`,
()=>{
classOMat = new ClassOMat();
classOMat.BaseClass(TestClass)
.field('name').required()
.field('favoriteSong')
.field('age').required()
.$();
testClass = new (classOMat.$())();
expect(classOMat.TYPE).to.equal(CLASS_O_MAT);
expect(testClass.TYPE).to.equal('TEST');
expect(typeof testClass.name).to.equal('function');
expect(typeof testClass.name).to.equal('function');
expect(typeof testClass.name).to.equal('function');
}
);
it(`should have a NamedDepCounter at testClass.___required()
and there should be a remaining count of 2`,
()=>{
const rDepCounter = testClass.___required();
expect(typeof rDepCounter).to.equal('object');
expect(typeof rDepCounter.count).to.equal('function');
expect(typeof rDepCounter.remaining).to.equal('function');
expect(rDepCounter.remainingCount()).to.equal(2);
expect(rDepCounter.remaining()).to.deep.equal(['name', 'age']);
}
);
it(`should be able to set and get a non-required field`,
()=>{
testClass.favoriteSong("Us and Them");
expect(testClass.___required().remainingCount()).to.equal(2);
expect(testClass.favoriteSong()).to.equal("Us and Them");
}
);
it(`should be able to set and get required fields.
The new remaining counts should be reflected once the
fields are set.`,
()=>{
testClass.name('Feral Foster');
expect(testClass.name()).to.equal('Feral Foster');
expect(testClass.___required().remainingCount()).to.equal(1);
expect(testClass.___required().remaining()).to.deep.equal(['age']);
expect(testClass.___required().currentCount()).to.equal(1);
expect(testClass.___required().current()).to.deep.equal(['name']);
expect(testClass.___required().ready()).to.equal(false);
expect(testClass.___ready()).to.equal(false);
testClass.age(29);
expect(testClass.age()).to.equal(29);
expect(testClass.___required().remainingCount()).to.equal(0);
expect(testClass.___required().remaining()).to.deep.equal([]);
expect(testClass.___required().currentCount()).to.equal(2);
expect(testClass.___required().current()).to.deep.equal(['name', 'age']);
expect(testClass.___required().ready()).to.equal(true);
expect(testClass.___ready()).to.equal(true);
}
);
it(`should be able to reset the required fields counts`,
()=>{
testClass.___required().reset();
expect(testClass.___required().remainingCount()).to.equal(2);
expect(testClass.___required().remaining()).to.deep.equal(['name', 'age']);
expect(testClass.___required().currentCount()).to.equal(0);
expect(testClass.___required().current()).to.deep.equal([]);
expect(testClass.___required().ready()).to.equal(false);
expect(testClass.___ready()).to.equal(false);
}
);
it(`should be able to add an on ready event to the testClass depCounter`,
()=>{
let completeInfo = false;
testClass.___required().onComplete((self)=>{ completeInfo = self.TYPE });
expect(completeInfo).to.equal(false);
testClass.name("Gloria").age(20);
expect(testClass.___ready()).to.equal(true);
expect(completeInfo).to.equal('TEST');
}
);
}
);