-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const constructor = Symbol('constructor'); | ||
|
||
module.exports = class ValidatorBuilder { | ||
/** | ||
* Class constructor. | ||
* | ||
* @type {function} | ||
* | ||
* @throws {TypeError} If given value is not a function. | ||
*/ | ||
set classConstructor(value) { | ||
if (typeof value !== 'function') { | ||
throw new TypeError(`Class constructor must be a class constructor function, got a ${typeof value} instead`); | ||
} | ||
this[constructor] = value; | ||
} | ||
|
||
/** | ||
* Build a validator. | ||
* | ||
* @param {function} validate - The validation function. | ||
* | ||
* @returns {Validator} A validator. | ||
*/ | ||
build(validate) { | ||
const validator = new this[constructor](); | ||
validator.validationFunction = validate; | ||
return validator; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const expect = require('../expect'); | ||
const ValidatorBuilder = require('../../src/ValidatorBuilder'); | ||
|
||
const builder = new ValidatorBuilder(); | ||
class Validator {} | ||
|
||
describe('ValidatorBuilder', () => { | ||
describe('"classConstructor" setter', () => { | ||
it('shoud accept a function', () => { | ||
builder.classConstructor = value => value; | ||
}); | ||
|
||
it('shoud only accept a function', () => { | ||
expect(() => { builder.classConstructor = 'foo'; }).to.throw(TypeError); | ||
}); | ||
}); | ||
|
||
describe('"build" method', () => { | ||
before(() => { | ||
builder.classConstructor = Validator; | ||
}); | ||
|
||
it('shoud return an instance of classContructor', () => { | ||
expect(builder.build()).to.be.an.instanceof(Validator); | ||
}); | ||
|
||
it('shoud pass first argument to validator validationFunction', () => { | ||
expect(builder.build(2)).to.have.property('validationFunction', 2); | ||
}); | ||
}); | ||
}); |