From e381abc3f49896907d8d6b0515f9ae9f0000aefb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Soko=C5=82owski?= Date: Mon, 23 May 2016 00:26:23 +0200 Subject: [PATCH] Use const by default (instead of let) --- lib/fields/array.js | 6 +- lib/fields/boolean.js | 2 +- lib/fields/email.js | 2 +- lib/fields/enum.js | 6 +- lib/fields/integer.js | 2 +- lib/fields/model.js | 2 +- lib/fields/number.js | 2 +- lib/fields/object.js | 12 +-- lib/fields/string.js | 2 +- lib/model.js | 22 ++--- test/fields/array-spec.js | 68 +++++++-------- test/fields/boolean-spec.js | 22 ++--- test/fields/email-spec.js | 28 +++--- test/fields/enum-spec.js | 40 ++++----- test/fields/field-spec.js | 72 ++++++++-------- test/fields/integer-spec.js | 70 +++++++-------- test/fields/model-spec.js | 38 ++++---- test/fields/number-spec.js | 62 ++++++------- test/fields/object-spec.js | 48 +++++------ test/fields/string-spec.js | 58 ++++++------- test/model-spec.js | 168 ++++++++++++++++++------------------ 21 files changed, 366 insertions(+), 366 deletions(-) diff --git a/lib/fields/array.js b/lib/fields/array.js index 47148d2..c3b84fc 100644 --- a/lib/fields/array.js +++ b/lib/fields/array.js @@ -6,7 +6,7 @@ const Field = require('./field'); * An array field. It uses provided field for data validation and serialization. * * @example - * let field = new ArrayField(new IntegerField()); + * const field = new ArrayField(new IntegerField()); * * field.deserialize([ 1, '2', '3.0' ]); // returns [ 1, 2, 3 ] * field.deserialize([ 'foo', 'bar' ]); // throws validation error @@ -50,8 +50,8 @@ class ArrayField extends Field { throw new Field.ValidationError(`Value must have at most ${this.params.maxLength} elements`); } - let result = []; - let errors = {}; + const result = []; + const errors = {}; let errorOccurred = false; for (let i = 0; i < value.length; ++i) { diff --git a/lib/fields/boolean.js b/lib/fields/boolean.js index 9a1330f..08c53eb 100644 --- a/lib/fields/boolean.js +++ b/lib/fields/boolean.js @@ -6,7 +6,7 @@ const Field = require('./field'); * A true/false field. Handles booleans and strings representing booleans. Serializes to boolean. * * @example - * let field = new BooleanField(); + * const field = new BooleanField(); * * field.deserialize(false); // returns false * field.deserialize('yes'); // returns true diff --git a/lib/fields/email.js b/lib/fields/email.js index 8233822..d9c4284 100644 --- a/lib/fields/email.js +++ b/lib/fields/email.js @@ -8,7 +8,7 @@ const StringField = require('./string'); * A {@link StringField} which checks if value is a valid email. * * @example - * let field = new EmailField(); + * const field = new EmailField(); * * field.deserialize('SOME.EMAIL@GMAIL.COM'); // returns "some.email@gmail.com" * field.deserialize('SOME.EMAIL.GMAIL.COM'); // throws validation error diff --git a/lib/fields/enum.js b/lib/fields/enum.js index 7ceff32..9037143 100644 --- a/lib/fields/enum.js +++ b/lib/fields/enum.js @@ -6,7 +6,7 @@ const Field = require('./field'); * An enum field. It accepts only specified strings. * * @example - * let field = new EnumField([ 'one', 'two', 'three' ]); + * const field = new EnumField([ 'one', 'two', 'three' ]); * * field.deserialize('one'); // returns "one" * field.deserialize('TWO'); // returns "two" @@ -39,9 +39,9 @@ class EnumField extends Field { throw new Error('All choices must be a strings'); } - let choicesMap = {}; + const choicesMap = {}; - for (let choice of choices) { + for (const choice of choices) { if (this.params.caseSensitive) { choicesMap[choice.trim()] = choice; } else { diff --git a/lib/fields/integer.js b/lib/fields/integer.js index e76896a..c11efec 100644 --- a/lib/fields/integer.js +++ b/lib/fields/integer.js @@ -7,7 +7,7 @@ const NumberField = require('./number'); * A NumberField which additionally checks if value is an integer. * * @example - * let field = new IntegerField(); + * const field = new IntegerField(); * * field.deserialize(123); // returns 123 * field.deserialize(1.3); // throws validation error diff --git a/lib/fields/model.js b/lib/fields/model.js index 4f0cc10..8f357c1 100644 --- a/lib/fields/model.js +++ b/lib/fields/model.js @@ -7,7 +7,7 @@ const Field = require('./field'); * An object field. It uses provided model for data validation and serialization. * * @example - * let field = new ModelField({ + * const field = new ModelField({ * id: new IntegerField(), * name: new StringField({ default: 'abc' }) * }); diff --git a/lib/fields/number.js b/lib/fields/number.js index d09d9ec..4045b2e 100644 --- a/lib/fields/number.js +++ b/lib/fields/number.js @@ -6,7 +6,7 @@ const Field = require('./field'); * A number field. Accepts double precision real numbers. * * @example - * let field = new StringField({ maxLength: 3 }); + * const field = new StringField({ maxLength: 3 }); * * field.deserialize('123.4'); // returns 123.4 * field.deserialize(123.4); // returns 123.4 diff --git a/lib/fields/object.js b/lib/fields/object.js index cc0b9ad..51c9350 100644 --- a/lib/fields/object.js +++ b/lib/fields/object.js @@ -6,7 +6,7 @@ const Field = require('./field'); * An object field. It uses provided field for properties validation and serialization. * * @example - * let field = new ObjectField(new IntegerField()); + * const field = new ObjectField(new IntegerField()); * * field.deserialize({ foo: 1, bar: '2.0' }); // returns { foo: 1, bar: 2 } * field.deserialize({ foo: 'bar' }); // throws validation error @@ -40,11 +40,11 @@ class ObjectField extends Field { throw new Field.ValidationError('Value must be an object'); } - let result = {}; - let errors = {}; + const result = {}; + const errors = {}; let errorOccurred = false; - for (let key of Object.keys(value)) { + for (const key of Object.keys(value)) { try { result[key] = this.field.deserialize(value[key], options); } catch (error) { @@ -68,9 +68,9 @@ class ObjectField extends Field { * @returns {object} */ serialize(value, options) { - let result = {}; + const result = {}; - for (let key of Object.keys(value)) { + for (const key of Object.keys(value)) { result[key] = this.field.serialize(value[key], options); } diff --git a/lib/fields/string.js b/lib/fields/string.js index 145f94e..5009fb6 100644 --- a/lib/fields/string.js +++ b/lib/fields/string.js @@ -6,7 +6,7 @@ const Field = require('./field'); * A text field. Accepts any string. * * @example - * let field = new StringField({ maxLength: 3 }); + * const field = new StringField({ maxLength: 3 }); * * field.deserialize('123'); // returns "123" * field.deserialize(123); // throws validation error diff --git a/lib/model.js b/lib/model.js index 2ca96b2..ee231f4 100644 --- a/lib/model.js +++ b/lib/model.js @@ -89,13 +89,13 @@ class Model { static deserialize(data, options) { options = options || {}; - let result = {}; - let errors = {}; + const result = {}; + const errors = {}; // Deserialize values - for (let key of Object.keys(this.fields)) { - let field = this.fields[key]; - let value = data[key]; + for (const key of Object.keys(this.fields)) { + const field = this.fields[key]; + const value = data[key]; try { if (field.isBlank(value)) { @@ -132,11 +132,11 @@ class Model { static serialize(data, options) { options = options || {}; - let result = {}; + const result = {}; - for (let key of Object.keys(this.fields)) { - let field = this.fields[key]; - let value = data[key]; + for (const key of Object.keys(this.fields)) { + const field = this.fields[key]; + const value = data[key]; if (field.isBlank(value)) { if (!options.partial) { @@ -158,13 +158,13 @@ class Model { */ static extend(fields) { // Create new class - let Cls = class extends this {}; + const Cls = class extends this {}; // Create fields property Cls.fields = Object.assign({}, this.fields); // Copy fields to fields property - for (let key of Object.keys(fields)) { + for (const key of Object.keys(fields)) { if (fields[key] instanceof Field) { Cls.fields[key] = fields[key]; } else { diff --git a/test/fields/array-spec.js b/test/fields/array-spec.js index 29e1c46..22177c9 100644 --- a/test/fields/array-spec.js +++ b/test/fields/array-spec.js @@ -11,8 +11,8 @@ chai.use(spies); describe('ArrayField', function() { describe('constructor', function() { it('should create new instance of field using specified field', function() { - let field = new Field(); - let arrayField = new ArrayField(field); + const field = new Field(); + const arrayField = new ArrayField(field); expect(arrayField).to.be.instanceOf(Field); expect(arrayField).to.be.instanceOf(ArrayField); @@ -20,7 +20,7 @@ describe('ArrayField', function() { }); it('should create new instance of field when all params are specified', function () { - let field = new ArrayField(new Field(), { + const field = new ArrayField(new Field(), { blank: true, default: [ 1, 2 ], minLength: 1, @@ -32,7 +32,7 @@ describe('ArrayField', function() { }); it('should throw error when field is not specified', function() { - let call = () => new ArrayField(); + const call = () => new ArrayField(); expect(call).to.throw('Field is missing'); }); @@ -44,25 +44,25 @@ describe('ArrayField', function() { deserialize() { return true } } - let field = new ArrayField(new CustomField()); + const field = new ArrayField(new CustomField()); - let result = field.deserialize([ 1, 2 ]); + const result = field.deserialize([ 1, 2 ]); expect(result).to.be.deep.equal([ true, true ]); }); it('should deserialize empty array', function () { - let field = new ArrayField(new Field()); + const field = new ArrayField(new Field()); - let result = field.deserialize([]); + const result = field.deserialize([]); expect(result).to.be.deep.equal([]); }); it('should throw error when value is not an array', function() { - let field = new ArrayField(new Field()); + const field = new ArrayField(new Field()); - let call = () => field.deserialize(123); + const call = () => field.deserialize(123); expect(call).to.throw(Field.ValidationError, 'Value must be an array'); }); @@ -72,7 +72,7 @@ describe('ArrayField', function() { deserialize(v) { throw new Field.ValidationError(v) } } - let field = new ArrayField(new InvalidField()); + const field = new ArrayField(new InvalidField()); try { field.deserialize([ 1, 2 ]); @@ -84,62 +84,62 @@ describe('ArrayField', function() { }); it('should not intercept errors other than ValidationError', function () { - let error = new Error(); + const error = new Error(); class InvalidField extends Field { deserialize(v) { throw error } } - let field = new ArrayField(new InvalidField()); + const field = new ArrayField(new InvalidField()); - let call = () => field.deserialize([ 1 ]); + const call = () => field.deserialize([ 1 ]); expect(call).to.throw(error); }); it('should throw error array\'s length is less than min length', function() { - let field = new ArrayField(new Field(), { minLength: 2 }); + const field = new ArrayField(new Field(), { minLength: 2 }); - let call = () => field.deserialize([ 1 ]); + const call = () => field.deserialize([ 1 ]); expect(call).to.throw(Field.ValidationError, 'Value must have at least 2 elements'); }); it('should throw when array\'s length is greater than than max length', function() { - let field = new ArrayField(new Field(), { maxLength: 2 }); + const field = new ArrayField(new Field(), { maxLength: 2 }); - let call = () => field.deserialize([ 1, 2, 3 ]); + const call = () => field.deserialize([ 1, 2, 3 ]); expect(call).to.throw(Field.ValidationError, 'Value must have at most 2 elements'); }); it('should not throw when array\'s length is less than or equal to max length', function() { - let field = new ArrayField(new Field(), { maxLength: 2 }); + const field = new ArrayField(new Field(), { maxLength: 2 }); - let call1 = () => field.deserialize([ 1, 2 ]); - let call2 = () => field.deserialize([ 1 ]); + const call1 = () => field.deserialize([ 1, 2 ]); + const call2 = () => field.deserialize([ 1 ]); expect(call1).to.not.throw(); expect(call2).to.not.throw(); }); it('should not throw when array\'s length is greater than or equal to min length', function() { - let field = new ArrayField(new Field(), { minLength: 2 }); + const field = new ArrayField(new Field(), { minLength: 2 }); - let call1 = () => field.deserialize([ 1, 2, 3 ]); - let call2 = () => field.deserialize([ 1, 2 ]); + const call1 = () => field.deserialize([ 1, 2, 3 ]); + const call2 = () => field.deserialize([ 1, 2 ]); expect(call1).to.not.throw(); expect(call2).to.not.throw(); }); it('should pass specified options to child-field\'s deserialize method', function () { - let childField = new Field(); - let arrayField = new ArrayField(childField); + const childField = new Field(); + const arrayField = new ArrayField(childField); childField.deserialize = chai.spy(); - let opts = { option: 'option' }; + const opts = { option: 'option' }; arrayField.deserialize([ 1 ], opts); @@ -153,28 +153,28 @@ describe('ArrayField', function() { serialize() { return true } } - let field = new ArrayField(new CustomField()); + const field = new ArrayField(new CustomField()); - let result = field.serialize([ 1, 2 ]); + const result = field.serialize([ 1, 2 ]); expect(result).to.be.deep.equal([ true, true ]); }); it('should deserialize empty array', function () { - let field = new ArrayField(new Field()); + const field = new ArrayField(new Field()); - let result = field.serialize([]); + const result = field.serialize([]); expect(result).to.be.deep.equal([]); }); it('should pass specified options to child-field\'s serialize method', function () { - let childField = new Field(); - let arrayField = new ArrayField(childField); + const childField = new Field(); + const arrayField = new ArrayField(childField); childField.serialize = chai.spy(); - let opts = { option: 'option' }; + const opts = { option: 'option' }; arrayField.serialize([ 1 ], opts); diff --git a/test/fields/boolean-spec.js b/test/fields/boolean-spec.js index 5769374..786d0ec 100644 --- a/test/fields/boolean-spec.js +++ b/test/fields/boolean-spec.js @@ -7,14 +7,14 @@ const BooleanField = require('../../lib/fields/boolean'); describe('BooleanField', function () { describe('constructor', function () { it('should create new instance', function () { - let field = new BooleanField(); + const field = new BooleanField(); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(BooleanField); }); it('should create new instance when all params are specified', function () { - let field = new BooleanField({ + const field = new BooleanField({ default: false, optional: true }); @@ -26,21 +26,21 @@ describe('BooleanField', function () { describe('deserialize method', function () { it('should deserialize boolean "true" to true', function () { - let field = new BooleanField({}); - let result = field.deserialize(true); + const field = new BooleanField({}); + const result = field.deserialize(true); expect(result).to.be.equal(true); }); it('should deserialize boolean "false" to false', function () { - let field = new BooleanField({}); - let result = field.deserialize(false); + const field = new BooleanField({}); + const result = field.deserialize(false); expect(result).to.be.equal(false); }); it('should deserialize strings representing any YAML boolean literals', function () { - let field = new BooleanField({}); + const field = new BooleanField({}); expect(field.deserialize('y')).to.be.equal(true); expect(field.deserialize('yes')).to.be.equal(true); @@ -68,15 +68,15 @@ describe('BooleanField', function () { }); it('should throw error when value is a string but doesn\'t match to any YAML literals', function () { - let field = new BooleanField({}); - let call = () => console.log(field.deserialize('nope')); + const field = new BooleanField({}); + const call = () => console.log(field.deserialize('nope')); expect(call).to.throw(Field.ValidationError, 'Value must be a boolean'); }); it('should throw error when value is neither boolean nor string', function () { - let field = new BooleanField({}); - let call = () => field.deserialize(0); + const field = new BooleanField({}); + const call = () => field.deserialize(0); expect(call).to.throw(Field.ValidationError, 'Value must be a boolean'); }); diff --git a/test/fields/email-spec.js b/test/fields/email-spec.js index a4e25fe..86d324f 100644 --- a/test/fields/email-spec.js +++ b/test/fields/email-spec.js @@ -8,14 +8,14 @@ const EmailField = require('../../lib/fields/email'); describe('EmailField', function () { describe('constructor', function () { it('should create new instance of field', function () { - let field = new EmailField(); + const field = new EmailField(); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(EmailField); }); it('should create new instance of field when all params are specified', function () { - let field = new EmailField({ + const field = new EmailField({ default: 'test@dev.null', optional: true, caseSensitive: true @@ -28,43 +28,43 @@ describe('EmailField', function () { describe('deserialize method', function () { it('should deserialize valid email address', function () { - let field = new EmailField({}); - let result = field.deserialize('test+siabadaba@gmail.com'); + const field = new EmailField({}); + const result = field.deserialize('test+siabadaba@gmail.com'); expect(result).to.be.equal('test+siabadaba@gmail.com'); }); it('should trim value', function () { - let field = new EmailField({}); - let result = field.deserialize(' test+siabadaba@gmail.com \t\r\n'); + const field = new EmailField({}); + const result = field.deserialize(' test+siabadaba@gmail.com \t\r\n'); expect(result).to.be.equal('test+siabadaba@gmail.com'); }); it('should lowercase value', function () { - let field = new EmailField({}); - let result = field.deserialize('TeSt@sIaBaDaBa.cOm'); + const field = new EmailField({}); + const result = field.deserialize('TeSt@sIaBaDaBa.cOm'); expect(result).to.be.equal('test@siabadaba.com'); }); it('shouldn\'t lowercase value when caseSensitive option is enabled', function () { - let field = new EmailField({ caseSensitive: true }); - let result = field.deserialize('TeSt@sIaBaDaBa.cOm'); + const field = new EmailField({ caseSensitive: true }); + const result = field.deserialize('TeSt@sIaBaDaBa.cOm'); expect(result).to.be.equal('TeSt@sIaBaDaBa.cOm'); }); it('should throw error when value isn\'t a valid email address', function () { - let field = new EmailField({}); - let call = () => field.deserialize('.test+siabadaba@.gmail.com'); + const field = new EmailField({}); + const call = () => field.deserialize('.test+siabadaba@.gmail.com'); expect(call).to.throw(Field.ValidationError, 'Value must be a valid email address'); }); it('should throw error when value isn\'t a string', function () { - let field = new EmailField({}); - let call = () => field.deserialize(123); + const field = new EmailField({}); + const call = () => field.deserialize(123); expect(call).to.throw(Field.ValidationError, 'Value must be a string'); }); diff --git a/test/fields/enum-spec.js b/test/fields/enum-spec.js index 127c32f..a3dd893 100644 --- a/test/fields/enum-spec.js +++ b/test/fields/enum-spec.js @@ -7,14 +7,14 @@ const EnumField = require('../../lib/fields/enum'); describe('EnumField', function() { describe('constructor', function() { it('should create new instance of field', function() { - let field = new EnumField([ 'one' ]); + const field = new EnumField([ 'one' ]); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(EnumField); }); it('should create new instance of field when all params are specified', function() { - let field = new EnumField([ 'one' ], { + const field = new EnumField([ 'one' ], { default: 'one', optional: true, caseSensitive: true @@ -25,49 +25,49 @@ describe('EnumField', function() { }); it('should throw error when list of choices is missing', function () { - let call = () => new EnumField(); + const call = () => new EnumField(); expect(call).to.throw(Error, 'Choices list is missing'); }); it('should throw error when list of choices is empty', function () { - let call = () => new EnumField([]); + const call = () => new EnumField([]); expect(call).to.throw(Error, 'Choices list cannot be empty'); }); it('should throw error when some choices are doubled', function () { - let call = () => new EnumField([ 'one', 'two', 'one' ]); + const call = () => new EnumField([ 'one', 'two', 'one' ]); expect(call).to.throw(Error, 'Choices cannot be doubled'); }); it('should throw error when two choices are the same after trimming', function () { - let call = () => new EnumField([ 'one', 'two', ' one ' ]); + const call = () => new EnumField([ 'one', 'two', ' one ' ]); expect(call).to.throw(Error, 'Choices cannot be doubled'); }); it('should throw error when two choices differs in case only nad caseSensitive option is disabled', function () { - let call = () => new EnumField([ 'one', 'two', 'ONE' ]); + const call = () => new EnumField([ 'one', 'two', 'ONE' ]); expect(call).to.throw(Error, 'Choices cannot be doubled'); }); it('should\'t throw error when two choices differs in case and caseSensitive options is enabled', function () { - let call = () => new EnumField([ 'one', 'two', 'ONE' ], { caseSensitive: true }); + const call = () => new EnumField([ 'one', 'two', 'ONE' ], { caseSensitive: true }); expect(call).to.not.throw(Error, 'Choices cannot be doubled'); }); it('should throw error when some of specified choices aren\'t strings', function() { - let call = () => new EnumField([ 'one', 'two', 3 ]); + const call = () => new EnumField([ 'one', 'two', 3 ]); expect(call).to.throw(Error, 'All choices must be a strings'); }); it('should throw error when default value is not one of choices', function() { - let call = () => new EnumField([ 'one', 'two' ], { default: 'three' }); + const call = () => new EnumField([ 'one', 'two' ], { default: 'three' }); expect(call).to.throw(Error, 'Default value must be one of choices'); }); @@ -75,36 +75,36 @@ describe('EnumField', function() { describe('deserialize method', function() { it('should deserialize value', function() { - let field = new EnumField([ 'one' ]); - let result = field.deserialize('one'); + const field = new EnumField([ 'one' ]); + const result = field.deserialize('one'); expect(result).to.be.equal('one'); }); it('should deserialize untrimmed values', function() { - let field = new EnumField([ 'one' ]); - let result = field.deserialize('\t one \n'); + const field = new EnumField([ 'one' ]); + const result = field.deserialize('\t one \n'); expect(result).to.be.equal('one'); }); it('should deserialize value with invalid case', function() { - let field = new EnumField([ 'ONE' ]); - let result = field.deserialize('one'); + const field = new EnumField([ 'ONE' ]); + const result = field.deserialize('one'); expect(result).to.be.equal('ONE'); }); it('should throw error when value has invalid case and caseSensitive option is enabled', function () { - let field = new EnumField([ 'ONE' ], { caseSensitive: true }); - let call = () => field.deserialize('one'); + const field = new EnumField([ 'ONE' ], { caseSensitive: true }); + const call = () => field.deserialize('one'); expect(call).to.throw(Field.ValidationError, 'Value must be one of allowed choices: ONE'); }); it('should throw error when value is not one of choices', function () { - let field = new EnumField([ 'one', 'two' ]); - let call = () => field.deserialize('three'); + const field = new EnumField([ 'one', 'two' ]); + const call = () => field.deserialize('three'); expect(call).to.throw(Field.ValidationError, 'Value must be one of allowed choices: one, two'); }); diff --git a/test/fields/field-spec.js b/test/fields/field-spec.js index b999939..b2d1201 100644 --- a/test/fields/field-spec.js +++ b/test/fields/field-spec.js @@ -6,7 +6,7 @@ const Field = require('../../lib/fields/field'); describe('Field', function () { describe('constructor', function () { it('should create new instance', function () { - let field = new Field(); + const field = new Field(); expect(field).to.be.instanceOf(Field); expect(field).to.have.property('params'); @@ -21,13 +21,13 @@ describe('Field', function () { } } - let field = new TestField(1, 2, 3); + const field = new TestField(1, 2, 3); expect(field.args).to.be.deep.equal([1, 2, 3]); }); it('should freeze params', function () { - let field = new Field(); + const field = new Field(); expect(field.params).to.be.frozen; }); @@ -39,20 +39,20 @@ describe('Field', function () { } } - let call = () => new TestField({ default: true }); + const call = () => new TestField({ default: true }); expect(call).to.throw(Error, 'Default value is invalid: value is invalid'); }); it('should throw error when unexpected error occurs during deserialization of default value', function () { - let error = new Error(); + const error = new Error(); class TestField extends Field { deserialize() { throw error; } } - let call = () => new TestField({ default: true }); + const call = () => new TestField({ default: true }); expect(call).to.throw(error); }); @@ -60,13 +60,13 @@ describe('Field', function () { describe('init method', function () { it('should copy params to field', function () { - let params = { + const params = { default: {}, optional: true, foo: 'bar' }; - let field = {}; + const field = {}; Field.prototype.init.call(field, params); expect(field.params).to.be.deep.equal(params); @@ -77,10 +77,10 @@ describe('Field', function () { describe('serialize method', function () { it('should return passed value', function () { - let field = new Field(); + const field = new Field(); - let value = {}; - let result = field.serialize(value); + const value = {}; + const result = field.serialize(value); expect(result).to.be.equal(value); }); @@ -88,10 +88,10 @@ describe('Field', function () { describe('deserialize method', function () { it('should return passed value', function () { - let field = new Field(); + const field = new Field(); - let value = {}; - let result = field.deserialize(value); + const value = {}; + const result = field.deserialize(value); expect(result).to.be.equal(value); }); @@ -99,9 +99,9 @@ describe('Field', function () { describe('serializeBlank method', function () { it('should return passed value', function () { - let field = new Field(); + const field = new Field(); - let result = field.serializeBlank(); + const result = field.serializeBlank(); expect(result).to.be.null; }); @@ -109,33 +109,33 @@ describe('Field', function () { describe('deserializeBlank method', function () { it('should return null when field is optional', function () { - let field = new Field({ optional: true }); + const field = new Field({ optional: true }); - let result = field.deserializeBlank(); + const result = field.deserializeBlank(); expect(result).to.be.null; }); it('should return default value when it is specified and field is not optional', function () { - let field = new Field({ default: 'lime in the coconut' }); + const field = new Field({ default: 'lime in the coconut' }); - let result = field.deserializeBlank(); + const result = field.deserializeBlank(); expect(result).to.be.equal('lime in the coconut'); }); it('should return default value when it is specified and field is optional', function() { - let field = new Field({ default: 'lime in the coconut', optional: true }); + const field = new Field({ default: 'lime in the coconut', optional: true }); - let result = field.deserializeBlank(); + const result = field.deserializeBlank(); expect(result).to.be.equal('lime in the coconut'); }); it('should throw error when field is not optional and there is no default value', function () { - let field = new Field(); + const field = new Field(); - let call = () => field.deserializeBlank(); + const call = () => field.deserializeBlank(); expect(call).to.throw(Field.ValidationError, 'Value cannot be empty'); }); @@ -143,43 +143,43 @@ describe('Field', function () { describe('isBlank', function () { it('should return true when value is null', function () { - let field = new Field(); - let result = field.isBlank(null); + const field = new Field(); + const result = field.isBlank(null); expect(result).to.be.true; }); it('should return true when value is undefined', function () { - let field = new Field(); - let result = field.isBlank(undefined); + const field = new Field(); + const result = field.isBlank(undefined); expect(result).to.be.true; }); it('should return true when value is empty string', function () { - let field = new Field(); - let result = field.isBlank(''); + const field = new Field(); + const result = field.isBlank(''); expect(result).to.be.true; }); it('should return false when value is empty array', function () { - let field = new Field(); - let result = field.isBlank([]); + const field = new Field(); + const result = field.isBlank([]); expect(result).to.be.false; }); it('should return false when value is empty object', function () { - let field = new Field(); - let result = field.isBlank({}); + const field = new Field(); + const result = field.isBlank({}); expect(result).to.be.false; }); it('should return false when value is false', function () { - let field = new Field(); - let result = field.isBlank(false); + const field = new Field(); + const result = field.isBlank(false); expect(result).to.be.false; }); diff --git a/test/fields/integer-spec.js b/test/fields/integer-spec.js index 1a381ac..b05f87e 100644 --- a/test/fields/integer-spec.js +++ b/test/fields/integer-spec.js @@ -7,14 +7,14 @@ const IntegerField = require('../../lib/fields/integer'); describe('IntegerField', function () { describe('constructor', function () { it('should create new instance of field', function () { - let field = new IntegerField(); + const field = new IntegerField(); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(IntegerField); }); it('should create new instance of field when all params are specified', function () { - let field = new IntegerField({ + const field = new IntegerField({ default: 15, optional: true, min: 10, @@ -26,43 +26,43 @@ describe('IntegerField', function () { }); it('should throw error when min value is not a number', function () { - let call = () => new IntegerField({ min: null }); + const call = () => new IntegerField({ min: null }); expect(call).to.throw(Error, 'Min value must be a number'); }); it('should throw error when min value is not finite', function () { - let call = () => new IntegerField({ min: -Infinity }); + const call = () => new IntegerField({ min: -Infinity }); expect(call).to.throw(Error, 'Min value must be a number'); }); it('should throw error when min value is not an integer', function () { - let call = () => new IntegerField({ min: 12.3 }); + const call = () => new IntegerField({ min: 12.3 }); expect(call).to.throw(Error, 'Min value must be an integer'); }); it('should throw error when max value is not a number', function () { - let call = () => new IntegerField({ max: true }); + const call = () => new IntegerField({ max: true }); expect(call).to.throw(Error, 'Max value must be a number'); }); it('should throw error when max value is not finite', function () { - let call = () => new IntegerField({ max: NaN }); + const call = () => new IntegerField({ max: NaN }); expect(call).to.throw(Error, 'Max value must be a number'); }); it('should throw error when max value is not an integer', function () { - let call = () => new IntegerField({ max: 0.1 }); + const call = () => new IntegerField({ max: 0.1 }); expect(call).to.throw(Error, 'Max value must be an integer'); }); it('should throw error when min value is greater than max value', function () { - let call = () => new IntegerField({ max: -10, min: 10 }); + const call = () => new IntegerField({ max: -10, min: 10 }); expect(call).to.throw(Error, 'Max value must be greater than min value'); }); @@ -70,92 +70,92 @@ describe('IntegerField', function () { describe('deserialize method', function () { it('should deserialize an integer', function () { - let field = new IntegerField({}); - let result = field.deserialize(0); + const field = new IntegerField({}); + const result = field.deserialize(0); expect(result).to.be.equal(0); }); it('should convert string to number', function () { - let field = new IntegerField({}); - let result = field.deserialize('12345'); + const field = new IntegerField({}); + const result = field.deserialize('12345'); expect(result).to.be.equal(12345); }); it('should throw error when value is a number but isn\'t an integer', function () { - let field = new IntegerField({}); - let call = () => field.deserialize(12345.6); + const field = new IntegerField({}); + const call = () => field.deserialize(12345.6); expect(call).to.throw(Field.ValidationError, 'Value must be an integer'); }); it('should throw error when value is a NaN', function () { - let field = new IntegerField({}); - let call = () => field.deserialize(NaN); + const field = new IntegerField({}); + const call = () => field.deserialize(NaN); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is an infinity', function () { - let field = new IntegerField({}); - let call = () => field.deserialize(Infinity); + const field = new IntegerField({}); + const call = () => field.deserialize(Infinity); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is a string which doesn\'t represent integer', function () { - let field = new IntegerField({}); - let call = () => field.deserialize('123af'); + const field = new IntegerField({}); + const call = () => field.deserialize('123af'); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is neither a string nor a number', function () { - let field = new IntegerField({}); - let call = () => field.deserialize(new Buffer('123')); + const field = new IntegerField({}); + const call = () => field.deserialize(new Buffer('123')); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is less than min value', function () { - let field = new IntegerField({ min: 4 }); - let call = () => field.deserialize(2); + const field = new IntegerField({ min: 4 }); + const call = () => field.deserialize(2); expect(call).to.throw(Field.ValidationError, 'Value cannot be less than 4'); }); it('should throw error when value is greater than max value', function () { - let field = new IntegerField({ max: -4 }); - let call = () => field.deserialize(-2); + const field = new IntegerField({ max: -4 }); + const call = () => field.deserialize(-2); expect(call).to.throw(Field.ValidationError, 'Value cannot be greater than -4'); }); it('shouldn\'t throw error when value is equal to min value', function () { - let field = new IntegerField({ min: -5 }); - let result = field.deserialize(-5); + const field = new IntegerField({ min: -5 }); + const result = field.deserialize(-5); expect(result).to.be.equal(-5); }); it('shouldn\'t throw error when value is equal to max value', function () { - let field = new IntegerField({ max: 5 }); - let result = field.deserialize(5); + const field = new IntegerField({ max: 5 }); + const result = field.deserialize(5); expect(result).to.be.equal(5); }); it('shouldn\'t throw error when value is greater than min value', function () { - let field = new IntegerField({ min: -6 }); - let result = field.deserialize(6); + const field = new IntegerField({ min: -6 }); + const result = field.deserialize(6); expect(result).to.be.equal(6); }); it('shouldn\'t throw error when value is less than max value', function () { - let field = new IntegerField({ max: 6 }); - let result = field.deserialize(-6); + const field = new IntegerField({ max: 6 }); + const result = field.deserialize(-6); expect(result).to.be.equal(-6); }); diff --git a/test/fields/model-spec.js b/test/fields/model-spec.js index 23bd731..149ce70 100644 --- a/test/fields/model-spec.js +++ b/test/fields/model-spec.js @@ -12,8 +12,8 @@ chai.use(spies); describe('ModelField', function() { describe('constructor', function() { it('should create new instance of field using specified model', function() { - let CustomModel = Model.extend({}); - let field = new ModelField(CustomModel); + const CustomModel = Model.extend({}); + const field = new ModelField(CustomModel); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(ModelField); @@ -21,8 +21,8 @@ describe('ModelField', function() { }); it('should create new instance of field using specified object to create a new model', function () { - let field = new Field(); - let modelField = new ModelField({ field }); + const field = new Field(); + const modelField = new ModelField({ field }); expect(modelField).to.be.instanceOf(Field); expect(modelField).to.be.instanceOf(ModelField); @@ -30,7 +30,7 @@ describe('ModelField', function() { }); it('should create new instance of field when all params are specified', function () { - let field = new ModelField({}, { + const field = new ModelField({}, { optional: true }); @@ -39,7 +39,7 @@ describe('ModelField', function() { }); it('should throw error when model is not specified', function() { - let call = () => new ModelField(); + const call = () => new ModelField(); expect(call).to.throw(Error, 'Model is missing'); }); @@ -47,16 +47,16 @@ describe('ModelField', function() { describe('deserialize method', function () { it('should deserialize value', function() { - let field = new ModelField({ foo: new Field() }); - let result = field.deserialize({ foo: 123, bar: 456 }); + const field = new ModelField({ foo: new Field() }); + const result = field.deserialize({ foo: 123, bar: 456 }); expect(result).to.be.not.an.instanceOf(Model); expect(result).to.be.deep.equal({ foo: 123 }); }); it('should throw error when value is not an object', function() { - let field = new ModelField({ foo: new Field() }); - let call = () => field.deserialize(123); + const field = new ModelField({ foo: new Field() }); + const call = () => field.deserialize(123); expect(call).to.throw(Field.ValidationError, 'Value must be an object'); }); @@ -68,7 +68,7 @@ describe('ModelField', function() { } } - let field = new ModelField({ + const field = new ModelField({ foo: new InvalidField({ msg: 'bar' }), egg: new InvalidField({ msg: 'spam' }) }); @@ -83,9 +83,9 @@ describe('ModelField', function() { }); it('should pass specified options to model\'s deserialize method', function () { - let field = new ModelField({}); - let data = { foo: 'bar' }; - let opts = { option: 'option' }; + const field = new ModelField({}); + const data = { foo: 'bar' }; + const opts = { option: 'option' }; field.Model.deserialize = chai.spy(); @@ -103,11 +103,11 @@ describe('ModelField', function() { } } - let field = new ModelField({ + const field = new ModelField({ foo: new CustomField() }); - let result = field.serialize({ + const result = field.serialize({ foo: false }); @@ -115,9 +115,9 @@ describe('ModelField', function() { }); it('should pass specified options to model\'s serialize method', function () { - let field = new ModelField({}); - let data = { foo: 'bar' }; - let opts = { option: 'option' }; + const field = new ModelField({}); + const data = { foo: 'bar' }; + const opts = { option: 'option' }; field.Model.serialize = chai.spy(); diff --git a/test/fields/number-spec.js b/test/fields/number-spec.js index f54e97e..f8e9e61 100644 --- a/test/fields/number-spec.js +++ b/test/fields/number-spec.js @@ -7,14 +7,14 @@ const NumberField = require('../../lib/fields/number'); describe('NumberField', function () { describe('constructor', function () { it('should create new instance of field', function () { - let field = new NumberField(); + const field = new NumberField(); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(NumberField); }); it('should create new instance of field when all params are specified', function () { - let field = new NumberField({ + const field = new NumberField({ default: 15.51, optional: true, min: 10.01, @@ -26,31 +26,31 @@ describe('NumberField', function () { }); it('should throw error when min value is not a number', function () { - let call = () => new NumberField({ min: null }); + const call = () => new NumberField({ min: null }); expect(call).to.throw(Error, 'Min value must be a number'); }); it('should throw error when min value is not finite', function () { - let call = () => new NumberField({ min: -Infinity }); + const call = () => new NumberField({ min: -Infinity }); expect(call).to.throw(Error, 'Min value must be a number'); }); it('should throw error when max value is not a number', function () { - let call = () => new NumberField({ max: true }); + const call = () => new NumberField({ max: true }); expect(call).to.throw(Error, 'Max value must be a number'); }); it('should throw error when max value is not finite', function () { - let call = () => new NumberField({ max: NaN }); + const call = () => new NumberField({ max: NaN }); expect(call).to.throw(Error, 'Max value must be a number'); }); it('should throw error when min value is greater than max value', function () { - let call = () => new NumberField({ max: -10, min: 10 }); + const call = () => new NumberField({ max: -10, min: 10 }); expect(call).to.throw(Error, 'Max value must be greater than min value'); }); @@ -58,85 +58,85 @@ describe('NumberField', function () { describe('deserialize method', function () { it('should deserialize a number', function () { - let field = new NumberField({}); - let result = field.deserialize(12345); + const field = new NumberField({}); + const result = field.deserialize(12345); expect(result).to.be.equal(12345); }); it('should convert string to number', function () { - let field = new NumberField({}); - let result = field.deserialize('12345.1'); + const field = new NumberField({}); + const result = field.deserialize('12345.1'); expect(result).to.be.equal(12345.1); }); it('should throw error when value is a NaN', function () { - let field = new NumberField({}); - let call = () => field.deserialize(NaN); + const field = new NumberField({}); + const call = () => field.deserialize(NaN); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is an infinity', function () { - let field = new NumberField({}); - let call = () => field.deserialize(Infinity); + const field = new NumberField({}); + const call = () => field.deserialize(Infinity); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is a string which doesn\'t represent decimal number', function () { - let field = new NumberField({}); - let call = () => field.deserialize('123af'); + const field = new NumberField({}); + const call = () => field.deserialize('123af'); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is neither a string nor a number', function () { - let field = new NumberField({}); - let call = () => field.deserialize(new Buffer('123')); + const field = new NumberField({}); + const call = () => field.deserialize(new Buffer('123')); expect(call).to.throw(Field.ValidationError, 'Value must be a number'); }); it('should throw error when value is less than min value', function () { - let field = new NumberField({ min: 4 }); - let call = () => field.deserialize(2); + const field = new NumberField({ min: 4 }); + const call = () => field.deserialize(2); expect(call).to.throw(Field.ValidationError, 'Value cannot be less than 4'); }); it('should throw error when value is greater than max value', function () { - let field = new NumberField({ max: -4 }); - let call = () => field.deserialize(-2); + const field = new NumberField({ max: -4 }); + const call = () => field.deserialize(-2); expect(call).to.throw(Field.ValidationError, 'Value cannot be greater than -4'); }); it('shouldn\'t throw error when value is equal to min value', function () { - let field = new NumberField({ min: -5.5 }); - let result = field.deserialize(-5.5); + const field = new NumberField({ min: -5.5 }); + const result = field.deserialize(-5.5); expect(result).to.be.equal(-5.5); }); it('shouldn\'t throw error when value is equal to max value', function () { - let field = new NumberField({ max: 5.5 }); - let result = field.deserialize(5.5); + const field = new NumberField({ max: 5.5 }); + const result = field.deserialize(5.5); expect(result).to.be.equal(5.5); }); it('shouldn\'t throw error when value is greater than min value', function () { - let field = new NumberField({ min: -6 }); - let result = field.deserialize(6); + const field = new NumberField({ min: -6 }); + const result = field.deserialize(6); expect(result).to.be.equal(6); }); it('shouldn\'t throw error when value is less than max value', function () { - let field = new NumberField({ max: 6 }); - let result = field.deserialize(-6); + const field = new NumberField({ max: 6 }); + const result = field.deserialize(-6); expect(result).to.be.equal(-6); }); diff --git a/test/fields/object-spec.js b/test/fields/object-spec.js index 41ac9ee..9acc78f 100644 --- a/test/fields/object-spec.js +++ b/test/fields/object-spec.js @@ -11,8 +11,8 @@ chai.use(spies); describe('ObjectField', function() { describe('constructor', function() { it('should create new instance of field using specified field', function() { - let field = new Field(); - let objectField = new ObjectField(field); + const field = new Field(); + const objectField = new ObjectField(field); expect(objectField).to.be.instanceOf(Field); expect(objectField).to.be.instanceOf(ObjectField); @@ -20,7 +20,7 @@ describe('ObjectField', function() { }); it('should create new instance of field when all params are specified', function () { - let field = new ObjectField(new Field(), { + const field = new ObjectField(new Field(), { blank: true, default: { foo: 'bar' } }); @@ -30,7 +30,7 @@ describe('ObjectField', function() { }); it('should throw error when field is not specified', function() { - let call = () => new ObjectField(); + const call = () => new ObjectField(); expect(call).to.throw('Field is missing'); }); @@ -42,25 +42,25 @@ describe('ObjectField', function() { deserialize() { return true } } - let field = new ObjectField(new CustomField()); + const field = new ObjectField(new CustomField()); - let result = field.deserialize({ foo: 1, bar: 2 }); + const result = field.deserialize({ foo: 1, bar: 2 }); expect(result).to.be.deep.equal({ foo: true, bar: true }); }); it('should deserialize empty object', function () { - let field = new ObjectField(new Field()); + const field = new ObjectField(new Field()); - let result = field.deserialize({}); + const result = field.deserialize({}); expect(result).to.be.deep.equal({}); }); it('should throw error when value is not an object', function() { - let field = new ObjectField(new Field()); + const field = new ObjectField(new Field()); - let call = () => field.deserialize(123); + const call = () => field.deserialize(123); expect(call).to.throw(Field.ValidationError, 'Value must be an object'); }); @@ -70,7 +70,7 @@ describe('ObjectField', function() { deserialize(v) { throw new Field.ValidationError(v) } } - let field = new ObjectField(new InvalidField()); + const field = new ObjectField(new InvalidField()); try { field.deserialize({ foo: 1, bar: 2 }); @@ -82,26 +82,26 @@ describe('ObjectField', function() { }); it('should not intercept errors other than ValidationError', function () { - let error = new Error(); + const error = new Error(); class InvalidField extends Field { deserialize(v) { throw error } } - let field = new ObjectField(new InvalidField()); + const field = new ObjectField(new InvalidField()); - let call = () => field.deserialize({ foo: 'bar' }); + const call = () => field.deserialize({ foo: 'bar' }); expect(call).to.throw(error); }); it('should pass specified options to child-field\'s deserialize method', function () { - let childField = new Field(); - let objectField = new ObjectField(childField); + const childField = new Field(); + const objectField = new ObjectField(childField); childField.deserialize = chai.spy(); - let opts = { option: 'option' }; + const opts = { option: 'option' }; objectField.deserialize({ foo: 'bar' }, opts); @@ -115,28 +115,28 @@ describe('ObjectField', function() { serialize() { return true } } - let field = new ObjectField(new CustomField()); + const field = new ObjectField(new CustomField()); - let result = field.serialize({ foo: false, bar: false }); + const result = field.serialize({ foo: false, bar: false }); expect(result).to.be.deep.equal({ foo: true, bar: true }); }); it('should deserialize empty object', function () { - let field = new ObjectField(new Field()); + const field = new ObjectField(new Field()); - let result = field.serialize({}); + const result = field.serialize({}); expect(result).to.be.deep.equal({}); }); it('should pass specified options to child-field\'s serialize method', function () { - let childField = new Field(); - let objectField = new ObjectField(childField); + const childField = new Field(); + const objectField = new ObjectField(childField); childField.serialize = chai.spy(); - let opts = { option: 'option' }; + const opts = { option: 'option' }; objectField.serialize({ foo: 'bar' }, opts); diff --git a/test/fields/string-spec.js b/test/fields/string-spec.js index 907d377..707c808 100644 --- a/test/fields/string-spec.js +++ b/test/fields/string-spec.js @@ -8,14 +8,14 @@ const StringField = require('../../lib/fields/string'); describe('StringField', function () { describe('constructor', function () { it('should create new instance of field', function () { - let field = new StringField(); + const field = new StringField(); expect(field).to.be.instanceOf(Field); expect(field).to.be.instanceOf(StringField); }); it('should create new instance of field when all params are specified', function () { - let field = new StringField({ + const field = new StringField({ default: 'a b c d e f g', optional: true, trim: true, @@ -28,31 +28,31 @@ describe('StringField', function () { }); it('should throw error when min length isn\'t an integer', function () { - let call = () => new StringField({ minLength: 7.000001 }); + const call = () => new StringField({ minLength: 7.000001 }); expect(call).to.throw(Error, 'Min length must be a positive integer'); }); it('should throw error when min length is negative', function () { - let call = () => new StringField({ minLength: -1 }); + const call = () => new StringField({ minLength: -1 }); expect(call).to.throw(Error, 'Min length must be a positive integer'); }); it('should throw error when max length isn\'t an integer', function () { - let call = () => new StringField({ maxLength: 5.55555 }); + const call = () => new StringField({ maxLength: 5.55555 }); expect(call).to.throw(Error, 'Max length must be a positive integer'); }); it('should throw error when max length is negative', function () { - let call = () => new StringField({ maxLength: -1 }); + const call = () => new StringField({ maxLength: -1 }); expect(call).to.throw(Error, 'Max length must be a positive integer'); }); it('should throw error when min length value is greater than max length value', function () { - let call = () => new StringField({ maxLength: 10, minLength: 100 }); + const call = () => new StringField({ maxLength: 10, minLength: 100 }); expect(call).to.throw(Error, 'Max length must be greater than min length'); }); @@ -60,78 +60,78 @@ describe('StringField', function () { describe('deserialize method', function () { it('should deserialize string', function () { - let field = new StringField({}); - let result = field.deserialize('123456'); + const field = new StringField({}); + const result = field.deserialize('123456'); expect(result).to.be.equal('123456'); }); it('should trim value when trim option is enabled', function () { - let field = new StringField({ trim: true }); - let result = field.deserialize(' 123456 '); + const field = new StringField({ trim: true }); + const result = field.deserialize(' 123456 '); expect(result).to.be.equal('123456'); }); it('shouldn\'t trim value when trim option is disabled', function () { - let field = new StringField({}); - let result = field.deserialize(' 123456 '); + const field = new StringField({}); + const result = field.deserialize(' 123456 '); expect(result).to.be.equal(' 123456 '); }); it('should throw error when value is empty after trimming', function () { - let field = new StringField({ trim: true }); - let call = () => field.deserialize(' \t\n\r'); + const field = new StringField({ trim: true }); + const call = () => field.deserialize(' \t\n\r'); expect(call).to.throw(Field.ValidationError, 'Value cannot be empty'); }); it('should throw error when value is not a string', function () { - let field = new StringField({}); - let call = () => field.deserialize(false); + const field = new StringField({}); + const call = () => field.deserialize(false); expect(call).to.throw(Field.ValidationError, 'Value must be a string'); }); it('should throw error when value\'s length is greater than max length', function () { - let field = new StringField({ maxLength: 5 }); - let call = () => field.deserialize('123456'); + const field = new StringField({ maxLength: 5 }); + const call = () => field.deserialize('123456'); expect(call).to.throw(Field.ValidationError, 'Value\'s length cannot be greater than 5'); }); it('should throw error when value\'s length is less than min length', function () { - let field = new StringField({ minLength: 5 }); - let call = () => field.deserialize('1234'); + const field = new StringField({ minLength: 5 }); + const call = () => field.deserialize('1234'); expect(call).to.throw(Field.ValidationError, 'Value\'s length cannot be less than 5'); }); it('shouldn\'t throw error when value\'s length is equal to max length', function () { - let field = new StringField({ maxLength: 5 }); - let result = field.deserialize('12345'); + const field = new StringField({ maxLength: 5 }); + const result = field.deserialize('12345'); expect(result).to.be.equal('12345'); }); it('shouldn\'t throw error when value\'s length is less than max length', function () { - let field = new StringField({ maxLength: 5 }); - let result = field.deserialize('1234'); + const field = new StringField({ maxLength: 5 }); + const result = field.deserialize('1234'); expect(result).to.be.equal('1234'); }); it('shouldn\'t throw error when value\'s length is equal to min length', function () { - let field = new StringField({ minLength: 5 }); - let result = field.deserialize('12345'); + const field = new StringField({ minLength: 5 }); + const result = field.deserialize('12345'); expect(result).to.be.equal('12345'); }); it('shouldn\'t throw error when value\'s length is greater than min length', function () { - let field = new StringField({ minLength: 5 }); - let result = field.deserialize('123456'); + const field = new StringField({ minLength: 5 }); + const result = field.deserialize('123456'); expect(result).to.be.equal('123456'); }); diff --git a/test/model-spec.js b/test/model-spec.js index 03dfdab..e0d1e2e 100644 --- a/test/model-spec.js +++ b/test/model-spec.js @@ -12,38 +12,38 @@ chai.use(spies); describe('Model', function () { describe('constructor', function () { it('should throw error when try to create instance of Model directly', function () { - let call = () => new Model({}); + const call = () => new Model({}); expect(call).to.throw('You can only create instances of subclasses of Model'); }); it('should throw error when try to create instance of Model which was not created with extend method', function () { - let DataModel = class extends Model {}; + const DataModel = class extends Model {}; - let call = () => new DataModel({}); + const call = () => new DataModel({}); expect(call).to.throw('Model classes must be created using Model.extend method'); }); it('should create instance of subclass of Model', function () { - let DataModel = Model.extend({}); - let model = new DataModel({}); + const DataModel = Model.extend({}); + const model = new DataModel({}); expect(model).to.be.instanceOf(DataModel); expect(model).to.be.instanceOf(Model); }); it('should use deserialize method to deserialize provided data', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); DataModel.deserialize = chai.spy(() => ({ field1: 'foo', field2: 'bar' })); - let data = {}; - let opts = {}; - let result = new DataModel(data, opts); + const data = {}; + const opts = {}; + const result = new DataModel(data, opts); expect(DataModel.deserialize).to.have.been.called.with.exactly(data, opts); expect(result).to.be.instanceOf(DataModel); @@ -58,11 +58,11 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new CustomField() }); - let call = () => new DataModel({ field: true }); + const call = () => new DataModel({ field: true }); expect(call).to.not.throw(); }); @@ -74,11 +74,11 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new CustomField() }); - let call = () => new DataModel({ field: true }); + const call = () => new DataModel({ field: true }); expect(call).to.throw(Error); }); @@ -86,37 +86,37 @@ describe('Model', function () { describe('extend method', function () { it('should create subclass of Model', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); expect(DataModel.prototype).to.be.instanceOf(Model); }); it('should create subclass of subclass of Model', function () { - let DataModel = Model.extend({}); - let ExtraDataModel = DataModel.extend({}); + const DataModel = Model.extend({}); + const ExtraDataModel = DataModel.extend({}); expect(ExtraDataModel.prototype).to.be.instanceOf(Model); expect(ExtraDataModel.prototype).to.be.instanceOf(DataModel); }); it('should create static property "fields" containing all fields of model', function () { - let field1 = new Field(); - let field2 = new Field(); + const field1 = new Field(); + const field2 = new Field(); - let DataModel = Model.extend({ field1, field2 }); + const DataModel = Model.extend({ field1, field2 }); expect(DataModel.fields).to.have.property('field1', field1); expect(DataModel.fields).to.have.property('field2', field2); }); it('should add parent\'s fields to fields property', function () { - let parentField1 = new Field(); - let parentField2 = new Field(); - let ownField2 = new Field(); - let ownField3 = new Field(); + const parentField1 = new Field(); + const parentField2 = new Field(); + const ownField2 = new Field(); + const ownField3 = new Field(); - let ParentModel = Model.extend({ field1: parentField1, field2: parentField2 }); - let DataModel = ParentModel.extend({ field2: ownField2, field3: ownField3 }); + const ParentModel = Model.extend({ field1: parentField1, field2: parentField2 }); + const DataModel = ParentModel.extend({ field2: ownField2, field3: ownField3 }); expect(DataModel.fields).to.have.property('field1', parentField1); expect(DataModel.fields).to.have.property('field2', ownField2); @@ -124,13 +124,13 @@ describe('Model', function () { }); it('should freeze fields property', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); expect(DataModel.fields).to.be.frozen; }); it('should throw error when any of specified properties is not a Field instance', function () { - let call = () => Model.extend({ invalid: true }); + const call = () => Model.extend({ invalid: true }); expect(call).to.throw('All properties passed to extend method must be instances of Field class'); }); @@ -138,27 +138,27 @@ describe('Model', function () { describe('getErrors method', function () { it('should return validation errors', function () { - let DataModel = Model.extend({}); - let error = new Field.ValidationError({}); + const DataModel = Model.extend({}); + const error = new Field.ValidationError({}); DataModel.deserialize = function () { throw error; }; - let model = new DataModel({}); - let result = model.getErrors(); + const model = new DataModel({}); + const result = model.getErrors(); expect(result).to.be.equal(error.message); }); it('should return null when there is no errors', function () { - let DataModel = Model.extend({ + const DataModel = Model.extend({ field1: new Field(), field2: new Field() }); - let model = new DataModel({ field1: 12, field2: false }); - let errors = model.getErrors(); + const model = new DataModel({ field1: 12, field2: false }); + const errors = model.getErrors(); expect(errors).to.be.null; }); @@ -166,11 +166,11 @@ describe('Model', function () { describe('getOptions method', function () { it('should return options passed to constructor', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); - let opts = { option: 'option' }; - let model = new DataModel({}, opts); - let result = model.getOptions(); + const opts = { option: 'option' }; + const model = new DataModel({}, opts); + const result = model.getOptions(); expect(result).to.be.deep.equal(opts); expect(result).to.be.not.equal(opts); @@ -178,10 +178,10 @@ describe('Model', function () { }); it('should return empty object when no options were passed to constructor', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); - let model = new DataModel({}); - let result = model.getOptions(); + const model = new DataModel({}); + const result = model.getOptions(); expect(result).to.be.deep.equal({}); expect(result).to.be.frozen; @@ -190,10 +190,10 @@ describe('Model', function () { describe('getRawData method', function () { it('should return data passed to Model\'s constructor', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); - let data = { foo: 'bar' }; - let model = new DataModel(data); + const data = { foo: 'bar' }; + const model = new DataModel(data); expect(model.getRawData()).to.be.equal(data); }); @@ -201,13 +201,13 @@ describe('Model', function () { describe('toJSON method', function () { it('should call serialize method', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); DataModel.serialize = chai.spy(() => 'SERIALIZED DATA'); - let opts = {}; - let model = new DataModel({}, opts); - let result = model.toJSON(); + const opts = {}; + const model = new DataModel({}, opts); + const result = model.toJSON(); expect(DataModel.serialize).to.be.called.with.exactly(model, opts); expect(result).to.be.deep.equal('SERIALIZED DATA'); @@ -222,11 +222,11 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new CustomField() }); - let result = DataModel.deserialize({ field: false }); + const result = DataModel.deserialize({ field: false }); expect(result).to.have.property('field', 'deserialize'); }); @@ -238,23 +238,23 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new CustomField() }); - let result = DataModel.deserialize({}); + const result = DataModel.deserialize({}); expect(result).to.have.property('field', 'deserializeBlank'); }); it('should use isBlank method to determine if field is empty', function () { - let field = new Field(); + const field = new Field(); field.isBlank = (value) => !!value; field.deserialize = chai.spy(); field.deserializeBlank = chai.spy(); - let DataModel = Model.extend({ + const DataModel = Model.extend({ field1: field, field2: field }); @@ -275,7 +275,7 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field1: new InvalidField({ msg: 'abc' }), field2: new InvalidField({ msg: 'def' }) }); @@ -290,9 +290,9 @@ describe('Model', function () { }); it('should ignore all non-field related properties from specified data object', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); - let result = DataModel.deserialize({ + const result = DataModel.deserialize({ foo: 'bar' }); @@ -300,13 +300,13 @@ describe('Model', function () { }); it('should pass specified options to field\'s deserialize method', function () { - let field = new Field(); + const field = new Field(); field.deserialize = chai.spy(); - let DataModel = Model.extend({ field }); - let data = { field: 'field' }; - let opts = { option: 'option' }; + const DataModel = Model.extend({ field }); + const data = { field: 'field' }; + const opts = { option: 'option' }; DataModel.deserialize(data, opts); @@ -314,12 +314,12 @@ describe('Model', function () { }); it('should pass specified options to field\'s deserializeBlank method', function () { - let field = new Field({ optional: true }); + const field = new Field({ optional: true }); field.deserializeBlank = chai.spy(); - let DataModel = Model.extend({ field }); - let opts = { option: 'option' }; + const DataModel = Model.extend({ field }); + const opts = { option: 'option' }; DataModel.deserialize({}, opts); @@ -327,11 +327,11 @@ describe('Model', function () { }); it('shouldn\'t deserialize missing properties when "partial" option is enabled', function () { - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new Field({ default: true }) }); - let result = DataModel.deserialize({}, { partial: true }); + const result = DataModel.deserialize({}, { partial: true }); expect(result).to.not.have.property('field'); }); @@ -345,11 +345,11 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new CustomField() }); - let result = DataModel.serialize({ field: true }); + const result = DataModel.serialize({ field: true }); expect(result).to.have.property('field', 'serialize'); }); @@ -361,21 +361,21 @@ describe('Model', function () { } } - let DataModel = Model.extend({ + const DataModel = Model.extend({ field1: new CustomField(), field2: new CustomField() }); - let result = DataModel.serialize({}); + const result = DataModel.serialize({}); expect(result).to.have.property('field1', 'serializeBlank'); expect(result).to.have.property('field2', 'serializeBlank'); }); it('shouldn\'t copy values of non-field properties', function () { - let DataModel = Model.extend({}); + const DataModel = Model.extend({}); - let result = DataModel.serialize({ + const result = DataModel.serialize({ foo: 'bar' }); @@ -383,18 +383,18 @@ describe('Model', function () { }); it('should use isBlank method to determine if field is empty', function () { - let field = new Field(); + const field = new Field(); field.isBlank = (value) => !!value; field.serialize = chai.spy(); field.serializeBlank = chai.spy(); - let DataModel = Model.extend({ + const DataModel = Model.extend({ field1: field, field2: field }); - let result = DataModel.serialize({ + const result = DataModel.serialize({ field1: null, field2: true }); @@ -404,13 +404,13 @@ describe('Model', function () { }); it('should pass options to field\'s serialize method', function () { - let field = new Field(); + const field = new Field(); field.serialize = chai.spy(); - let DataModel = Model.extend({ field }); - let data = { field: 'field' }; - let opts = { option: 'option' }; + const DataModel = Model.extend({ field }); + const data = { field: 'field' }; + const opts = { option: 'option' }; DataModel.serialize(data, opts); @@ -418,12 +418,12 @@ describe('Model', function () { }); it('should pass options to field\'s serializeBlank method', function () { - let field = new Field(); + const field = new Field(); field.serializeBlank = chai.spy(); - let DataModel = Model.extend({ field }); - let opts = { option: 'option' }; + const DataModel = Model.extend({ field }); + const opts = { option: 'option' }; DataModel.serialize({}, opts); @@ -431,11 +431,11 @@ describe('Model', function () { }); it('shouldn\'t serialize missing properties when "partial" option is enabled', function () { - let DataModel = Model.extend({ + const DataModel = Model.extend({ field: new Field({ default: true }) }); - let result = DataModel.serialize({}, { partial: true }); + const result = DataModel.serialize({}, { partial: true }); expect(result).to.not.have.property('field'); });