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

209 lines (160 loc) 6.81 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "PluginTest"}); import mocha from 'mocha'; import { expect } from 'chai'; import { NONE, PLUGIN } from '../src/constants'; import {minionMethods} from '../src/constants/methods'; import Plugin from '../src/plugins/plugin'; import ClassOMat from '../src/class-o-mat/class-o-mat'; import buildClassMethods from '../src/methods/build-class'; describe(`Basic test of plugin system`, ()=>{ var classOMat; var Minion; var minion; var plugin; var constructorPrepend = function(...p) { this.I_AM_PLUGGED_IN = true; } var constructorAppend = function(...p) { this.I_AM_APPENDED = true; } var mixins = { getX: function() { return 20; }, getTYPE: function() { return this.TYPE; } } class BaseClass { TYPE = 'BASE'} it(`should have the proper methods available to testPlugin`, ()=>{ plugin = new Plugin().init('Test Plugin'); expect(plugin.TYPE).to.equal(PLUGIN); expect(typeof plugin.init).to.equal('function'); expect(typeof plugin.connect).to.equal('function'); expect(typeof plugin.key).to.equal('function'); expect(typeof plugin.buildMethods).to.equal('function'); expect(typeof plugin.mixins).to.equal('function'); expect(typeof plugin.onGet).to.equal('function'); expect(typeof plugin.onSet).to.equal('function'); expect(typeof plugin.append).to.equal('function'); expect(typeof plugin.prepend).to.equal('function'); expect(typeof plugin.field).to.equal('function'); } ); it(`should be able to access prepender and appender from the plugin. The extra test supports the test of singleton usage for the appender and prepender`, ()=>{ const appender = plugin.append(); const prepender = plugin.prepend(); expect(typeof appender).to.equal('object'); expect(typeof prepender).to.equal('object'); const keys = Object.keys(buildClassMethods); keys.forEach((key)=>{ expect(typeof appender[key]).to.equal('function'); expect(typeof prepender[key]).to.equal('function'); }); const alsoAppender = plugin.append(); const alsoPrepender = plugin.prepend(); keys.forEach((key)=>{ expect(typeof alsoAppender[key]).to.equal('function'); expect(typeof alsoPrepender[key]).to.equal('function'); }); expect(typeof plugin.prepend().buildConstructor).to.equal('function') } ); it(`Should be able to prepend to the constructor of the Minion`, ()=>{ expect(typeof plugin.prepend().toConstructor).to.equal('function'); expect(typeof constructorPrepend).to.equal('function'); plugin.prepend().toConstructor(constructorPrepend); } ); it(`Should be able to append to the constructor of the Minion`, ()=>{ expect(typeof plugin.append().toConstructor).to.equal('function'); expect(typeof constructorPrepend).to.equal('function'); plugin.append().toConstructor(constructorAppend); } ); it(`Should be able to add some mixin functions to the ultimate minion`, ()=>{ plugin.mixins(mixins); } ); it(`should create a Minion that contains features from the plugin`, ()=>{ classOMat = new ClassOMat('baseClassOMat'); classOMat.BaseClass(BaseClass) .field('test'); plugin.connect(classOMat); const minion = new (classOMat.$())(); expect(typeof minion.___isClassOMat).to.equal('function'); expect(minion.___isClassOMat()).to.equal(true); expect(minion.TYPE).to.equal('BASE'); expect(minion.I_AM_PLUGGED_IN).to.equal(true); expect(minion.I_AM_APPENDED).to.equal(true); expect(typeof minion.getX).to.equal('function'); expect(typeof minion.getTYPE).to.equal('function'); expect(typeof minion.test).to.equal('function'); expect(minion.test(50).TYPE).to.equal('BASE'); expect(minion.test()).to.equal(50); expect(minion.getX()).to.equal(20); expect(minion.getTYPE()).to.equal('BASE'); } ); it(`should be able to add a field to the minion from the plugin`, ()=>{ plugin.field('hair').default('the sunshine').validate().string().$() .field('cats').default('Eliot').validate().string().$(); plugin.connect(classOMat); const minion = new (classOMat.$())(); expect(typeof minion.hair).to.equal('function'); expect(typeof minion.cats).to.equal('function'); expect(minion.hair()).to.equal('the sunshine'); expect(minion.cats()).to.equal('Eliot'); minion.hair('brown'); minion.cats('sousa'); expect(minion.hair()).to.equal('brown'); expect(minion.cats()).to.equal('sousa'); } ); it(`should be able to inject function into the build stream. It will inject a function before and another after the buildFieldMixins function`, ()=>{ const prependToBuildFieldMixins =({classOMat, mixins, defaults, params}, buildMethods)=>{ mixins.anotherFunction = function() { return 20 } return [{classOMat, mixins, defaults, params}, buildMethods]; } const appendToBuildFieldMixins =({classOMat, mixins, defaults, params}, buildMethods)=>{ mixins.moreFunction = function() { return 'Bork' } return [{classOMat, mixins, defaults, params}, buildMethods]; } plugin.prepend().buildFieldMixins(prependToBuildFieldMixins); plugin.append().buildFieldMixins(appendToBuildFieldMixins); plugin.connect(classOMat); const minion = new (classOMat.$())(); expect(typeof minion.anotherFunction).to.equal('function'); expect(minion.anotherFunction()).to.equal(20); expect(typeof minion.moreFunction).to.equal('function'); expect(minion.moreFunction()).to.equal('Bork'); expect(typeof minion.hair).to.equal('function'); expect(typeof minion.cats).to.equal('function'); expect(minion.hair()).to.equal('the sunshine'); expect(minion.cats()).to.equal('Eliot'); minion.hair('brown'); minion.cats('sousa'); expect(minion.hair()).to.equal('brown'); expect(minion.cats()).to.equal('sousa'); } ); } );