From 922304d24b777bf782231d4518fbc3e3a6b7b4ab Mon Sep 17 00:00:00 2001 From: Marten Schilstra Date: Fri, 12 Aug 2016 20:33:53 +0200 Subject: [PATCH] Add Ember Suave and fix all errors --- .jscsrc | 3 + addon/errors.js | 14 +- addon/index.js | 2 +- addon/messages.js | 59 +++--- addon/mixin.js | 172 +++++++++++------- addon/patterns.js | 4 +- addon/validators/base.js | 88 +++++---- addon/validators/local/absence.js | 11 +- addon/validators/local/acceptance.js | 10 +- addon/validators/local/confirmation.js | 18 +- addon/validators/local/exclusion.js | 18 +- addon/validators/local/format.js | 14 +- addon/validators/local/inclusion.js | 20 +- addon/validators/local/length.js | 62 ++++--- addon/validators/local/numericality.js | 39 ++-- addon/validators/local/presence.js | 10 +- package.json | 1 + tests/dummy/app/app.js | 4 +- tests/dummy/app/controllers/foo.js | 4 +- tests/dummy/app/router.js | 4 +- tests/helpers/destroy-app.js | 4 +- tests/helpers/module-for-acceptance.js | 4 +- tests/helpers/start-app.js | 8 +- tests/unit/conditional-validators-test.js | 86 +++++---- tests/unit/controller-test.js | 2 +- tests/unit/errors-test.js | 60 ++---- .../unit/helpers/validate-properties-test.js | 3 +- tests/unit/validate-test.js | 145 +++++++-------- tests/unit/validators/base-test.js | 48 ++--- tests/unit/validators/local/absence-test.js | 43 ++--- .../unit/validators/local/acceptance-test.js | 35 ++-- .../validators/local/confirmation-test.js | 37 ++-- tests/unit/validators/local/exclusion-test.js | 33 ++-- tests/unit/validators/local/format-test.js | 29 +-- tests/unit/validators/local/inclusion-test.js | 33 ++-- tests/unit/validators/local/length-test.js | 57 +++--- .../validators/local/numericality-test.js | 99 +++++----- tests/unit/validators/local/presence-test.js | 25 ++- .../unit/validators/remote/uniqueness_test.js | 157 ---------------- 39 files changed, 720 insertions(+), 745 deletions(-) create mode 100644 .jscsrc delete mode 100644 tests/unit/validators/remote/uniqueness_test.js diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 00000000..e24512d0 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,3 @@ +{ + "preset": "ember-suave" +} diff --git a/addon/errors.js b/addon/errors.js index 9d2cee96..f5ee250d 100644 --- a/addon/errors.js +++ b/addon/errors.js @@ -1,11 +1,15 @@ import Ember from 'ember'; -var get = Ember.get; -var set = Ember.set; +const { + A: emberArray, + Object: EmberObject, + get, + set +} = Ember; -export default Ember.Object.extend({ - unknownProperty: function(property) { - set(this, property, Ember.A()); +export default EmberObject.extend({ + unknownProperty(property) { + set(this, property, emberArray()); return get(this, property); } }); diff --git a/addon/index.js b/addon/index.js index 80cb7368..3c8cd626 100644 --- a/addon/index.js +++ b/addon/index.js @@ -2,5 +2,5 @@ import Mixin from 'ember-validations/mixin'; export default Mixin; export function validator(callback) { - return { callback: callback }; + return { callback }; } diff --git a/addon/messages.js b/addon/messages.js index d40a4901..d954934e 100644 --- a/addon/messages.js +++ b/addon/messages.js @@ -1,40 +1,45 @@ import Ember from 'ember'; +const { I18n } = Ember; + export default { - render: function(attribute, context) { - if (Ember.I18n) { - return Ember.I18n.t('errors.' + attribute, context); + render(attribute, context) { + if (I18n) { + return I18n.t(`errors.${attribute}`, context); } else { - var regex = new RegExp("{{(.*?)}}"), - attributeName = ""; + let regex = new RegExp('{{(.*?)}}'); + let attributeName = ''; + if (regex.test(this.defaults[attribute])) { attributeName = regex.exec(this.defaults[attribute])[1]; } + return this.defaults[attribute].replace(regex, context[attributeName]); } }, + defaults: { - inclusion: "is not included in the list", - exclusion: "is reserved", - invalid: "is invalid", - confirmation: "doesn't match {{attribute}}", - accepted: "must be accepted", - empty: "can't be empty", - blank: "can't be blank", - present: "must be blank", - tooLong: "is too long (maximum is {{count}} characters)", - tooShort: "is too short (minimum is {{count}} characters)", - wrongLength: "is the wrong length (should be {{count}} characters)", - notANumber: "is not a number", - notAnInteger: "must be an integer", - greaterThan: "must be greater than {{count}}", - greaterThanOrEqualTo: "must be greater than or equal to {{count}}", - equalTo: "must be equal to {{count}}", - lessThan: "must be less than {{count}}", - lessThanOrEqualTo: "must be less than or equal to {{count}}", - otherThan: "must be other than {{count}}", - odd: "must be odd", - even: "must be even", - url: "is not a valid URL" + inclusion: 'is not included in the list', + exclusion: 'is reserved', + invalid: 'is invalid', + confirmation: 'doesn\'t match {{attribute}}', + accepted: 'must be accepted', + empty: 'can\'t be empty', + blank: 'can\'t be blank', + present: 'must be blank', + tooLong: 'is too long (maximum is {{count}} characters)', + tooShort: 'is too short (minimum is {{count}} characters)', + wrongLength: 'is the wrong length (should be {{count}} characters)', + notANumber: 'is not a number', + notAnInteger: 'must be an integer', + greaterThan: 'must be greater than {{count}}', + greaterThanOrEqualTo: 'must be greater than or equal to {{count}}', + equalTo: 'must be equal to {{count}}', + lessThan: 'must be less than {{count}}', + lessThanOrEqualTo: 'must be less than or equal to {{count}}', + otherThan: 'must be other than {{count}}', + odd: 'must be odd', + even: 'must be even', + url: 'is not a valid URL' } }; diff --git a/addon/mixin.js b/addon/mixin.js index f8d22db8..20b71fdb 100644 --- a/addon/mixin.js +++ b/addon/mixin.js @@ -3,37 +3,50 @@ import Errors from 'ember-validations/errors'; import Base from 'ember-validations/validators/base'; import getOwner from 'ember-getowner-polyfill'; -var get = Ember.get; -var set = Ember.set; - -var setValidityMixin = Ember.Mixin.create({ - isValid: Ember.computed('validators.@each.isValid', function() { - var compactValidators = get(this, 'validators').compact(); - var filteredValidators = compactValidators.filter(function(validator) { - return !get(validator, 'isValid'); - }); +const { + A: emberArray, + ArrayProxy, + Mixin, + RSVP: { all, reject }, + computed, + computed: { alias, not }, + get, + inject: { service }, + isArray, + isNone, + isPresent, + set, + warn +} = Ember; + +const setValidityMixin = Mixin.create({ + isValid: computed('validators.@each.isValid', function() { + let compactValidators = get(this, 'validators').compact(); + let filteredValidators = compactValidators.filter((validator) => !get(validator, 'isValid')); return get(filteredValidators, 'length') === 0; }), - isInvalid: Ember.computed.not('isValid') + + isInvalid: not('isValid') }); -var pushValidatableObject = function(model, property) { - var content = get(model, property); +const pushValidatableObject = function(model, property) { + let content = get(model, property); model.removeObserver(property, pushValidatableObject); - if (Ember.isArray(content)) { - model.validators.pushObject(ArrayValidatorProxy.create({model: model, property: property, contentBinding: 'model.' + property})); + + if (isArray(content)) { + model.validators.pushObject(ArrayValidatorProxy.create({ model, property, contentBinding: `model.${property}` })); } else { model.validators.pushObject(content); } }; -var lookupValidator = function(validatorName) { - var owner = getOwner(this); - var service = owner.lookup('service:validations'); - var validators = []; - var cache; +const lookupValidator = function(validatorName) { + let owner = getOwner(this); + let service = get(this, 'validationService'); + let validators = []; + let cache; if (service) { cache = get(service, 'cache'); @@ -44,19 +57,19 @@ var lookupValidator = function(validatorName) { if (cache[validatorName]) { validators = validators.concat(cache[validatorName]); } else { - var local = owner.resolveRegistration('validator:local/' + validatorName); - var remote = owner.resolveRegistration('validator:remote/' + validatorName); + let local = owner.resolveRegistration(`validator:local/${validatorName}`); + let remote = owner.resolveRegistration(`validator:remote/${validatorName}`); if (local || remote) { validators = validators.concat([local, remote]); } else { - var base = owner.resolveRegistration('validator:'+validatorName); + let base = owner.resolveRegistration(`validator:${validatorName}`); if (base) { validators = validators.concat([base]); } else { - local = owner.resolveRegistration('ember-validations@validator:local/'+validatorName); - remote = owner.resolveRegistration('ember-validations@validator:remote/'+validatorName); + local = owner.resolveRegistration(`ember-validations@validator:local/${validatorName}`); + remote = owner.resolveRegistration(`ember-validations@validator:remote/${validatorName}`); if (local || remote) { validators = validators.concat([local, remote]); @@ -67,46 +80,64 @@ var lookupValidator = function(validatorName) { cache[validatorName] = validators; } - Ember.warn('Could not find the "'+validatorName+'" validator.', !Ember.isEmpty(validators), {id: 'ember-validations.faild-to-find-validator'}); + warn(`Could not find the "${validatorName}" validator.`, isPresent(validators), { + id: 'ember-validations.faild-to-find-validator' + }); return validators; }; -var ArrayValidatorProxy = Ember.ArrayProxy.extend(setValidityMixin, { - validate: function() { +const ArrayValidatorProxy = ArrayProxy.extend(setValidityMixin, { + init() { + this._validate(); + }, + + validate() { return this._validate(); }, - _validate: Ember.on('init', function() { - var promises = get(this, 'content').invoke('_validate').without(undefined); - return Ember.RSVP.all(promises); - }), - validators: Ember.computed.alias('content') + + _validate() { + let promises = get(this, 'content').invoke('_validate').without(undefined); + return all(promises); + }, + + validators: alias('content') }); -export default Ember.Mixin.create(setValidityMixin, { - init: function() { - this._super(); +export default Mixin.create(setValidityMixin, { + validationService: service('validations'), + + init() { + this._super(...arguments); this.errors = Errors.create(); this.dependentValidationKeys = {}; - this.validators = Ember.A(); + this.validators = emberArray(); + if (get(this, 'validations') === undefined) { this.validations = {}; } + this.buildValidators(); - this.validators.forEach(function(validator) { + + this.validators.forEach((validator) => { validator.addObserver('errors.[]', this, function(sender) { - var errors = Ember.A(); - this.validators.forEach(function(validator) { + let errors = emberArray(); + + this.validators.forEach((validator) => { if (validator.property === sender.property) { errors.addObjects(validator.errors); } - }, this); - set(this, 'errors.' + sender.property, errors); + }); + + set(this, `errors.${sender.property}`, errors); }); - }, this); + }); + + this._validate(); }, - buildValidators: function() { - var property; + + buildValidators() { + let property; for (property in this.validations) { if (this.validations[property].constructor === Object) { @@ -116,10 +147,11 @@ export default Ember.Mixin.create(setValidityMixin, { } } }, - buildRuleValidator: function(property) { - var pushValidator = function(validator) { + + buildRuleValidator(property) { + let pushValidator = (validator, validatorName) => { if (validator) { - this.validators.pushObject(validator.create({model: this, property: property, options: this.validations[property][validatorName]})); + this.validators.pushObject(validator.create({ model: this, property, options: this.validations[property][validatorName] })); } }; @@ -127,46 +159,54 @@ export default Ember.Mixin.create(setValidityMixin, { this.validations[property] = { inline: this.validations[property] }; } - var createInlineClass = function(callback) { + let createInlineClass = (callback) => { return Base.extend({ - call: function() { - var errorMessage = this.callback.call(this); + call() { + let errorMessage = this.callback.call(this); if (errorMessage) { this.errors.pushObject(errorMessage); } }, - callback: callback + + callback }); }; - for (var validatorName in this.validations[property]) { + Object.keys(this.validations[property]).forEach((validatorName) => { if (validatorName === 'inline') { - pushValidator.call(this, createInlineClass(this.validations[property][validatorName].callback)); + let validator = createInlineClass(this.validations[property][validatorName].callback); + pushValidator(validator, validatorName); } else if (this.validations[property].hasOwnProperty(validatorName)) { - lookupValidator.call(this, validatorName).forEach(pushValidator, this); + lookupValidator.call(this, validatorName).forEach((validator) => { + return pushValidator.call(this, validator, validatorName); + }); } - } + }); }, - buildObjectValidator: function(property) { - if (Ember.isNone(get(this, property))) { + + buildObjectValidator(property) { + if (isNone(get(this, property))) { this.addObserver(property, this, pushValidatableObject); } else { pushValidatableObject(this, property); } }, - validate: function() { - var self = this; - return this._validate().then(function(vals) { - var errors = get(self, 'errors'); + + validate() { + return this._validate().then((vals) => { + let errors = get(this, 'errors'); + if (vals.indexOf(false) > -1) { - return Ember.RSVP.reject(errors); + return reject(errors); } + return errors; }); }, - _validate: Ember.on('init', function() { - var promises = this.validators.invoke('_validate').without(undefined); - return Ember.RSVP.all(promises); - }) + + _validate() { + let promises = this.validators.invoke('_validate').without(undefined); + return all(promises); + } }); diff --git a/addon/patterns.js b/addon/patterns.js index 8ab2cdd4..ec7904fa 100644 --- a/addon/patterns.js +++ b/addon/patterns.js @@ -1,6 +1,8 @@ import Ember from 'ember'; -export default Ember.Namespace.create({ +const { Namespace } = Ember; + +export default Namespace.create({ numericality: /^(-|\+)?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d*)?$/, blank: /^\s*$/ }); diff --git a/addon/validators/base.js b/addon/validators/base.js index 46280673..aef562e1 100644 --- a/addon/validators/base.js +++ b/addon/validators/base.js @@ -1,89 +1,104 @@ import Ember from 'ember'; -var get = Ember.get; -var set = Ember.set; +const { + A: emberArray, + Object: EmberObject, + RSVP: { reject, resolve }, + computed: { empty, not }, + get, + on, + set +} = Ember; -export default Ember.Object.extend({ - init: function() { - set(this, 'errors', Ember.A()); - this.dependentValidationKeys = Ember.A(); +export default EmberObject.extend({ + init() { + set(this, 'errors', emberArray()); + this.dependentValidationKeys = emberArray(); this.conditionals = { 'if': get(this, 'options.if'), unless: get(this, 'options.unless') }; this.model.addObserver(this.property, this, this._validate); }, - addObserversForDependentValidationKeys: Ember.on('init', function() { + + addObserversForDependentValidationKeys: on('init', function() { this.dependentValidationKeys.forEach(function(key) { this.model.addObserver(key, this, this._validate); }, this); }), - pushConditionalDependentValidationKeys: Ember.on('init', function() { - Ember.A(['if', 'unless']).forEach((conditionalKind) => { - const conditional = this.conditionals[conditionalKind]; - if (typeof(conditional) === 'string' && typeof(this.model[conditional]) !== 'function') { + + pushConditionalDependentValidationKeys: on('init', function() { + emberArray(['if', 'unless']).forEach((conditionalKind) => { + let conditional = this.conditionals[conditionalKind]; + if (typeof conditional === 'string' && typeof this.model[conditional] !== 'function') { this.dependentValidationKeys.pushObject(conditional); } }); }), - pushDependentValidationKeyToModel: Ember.on('init', function() { - var model = get(this, 'model'); + + pushDependentValidationKeyToModel: on('init', function() { + let model = get(this, 'model'); if (model.dependentValidationKeys[this.property] === undefined) { - model.dependentValidationKeys[this.property] = Ember.A(); + model.dependentValidationKeys[this.property] = emberArray(); } model.dependentValidationKeys[this.property].addObjects(this.dependentValidationKeys); }), - call: function () { + + call() { throw 'Not implemented!'; }, - unknownProperty: function(key) { - var model = get(this, 'model'); + + unknownProperty(key) { + let model = get(this, 'model'); if (model) { return get(model, key); } }, - isValid: Ember.computed.empty('errors.[]'), - isInvalid: Ember.computed.not('isValid'), - validate: function() { - var self = this; - return this._validate().then(function(success) { + + isValid: empty('errors.[]'), + isInvalid: not('isValid'), + + validate() { + return this._validate().then((success) => { // Convert validation failures to rejects. - var errors = get(self, 'model.errors'); + let errors = get(this, 'model.errors'); if (success) { return errors; } else { - return Ember.RSVP.reject(errors); + return reject(errors); } }); }, - _validate: Ember.on('init', function() { + + _validate: on('init', function() { this.errors.clear(); if (this.canValidate()) { this.call(); } if (get(this, 'isValid')) { - return Ember.RSVP.resolve(true); + return resolve(true); } else { - return Ember.RSVP.resolve(false); + return resolve(false); } }), - canValidate: function() { - if (typeof(this.conditionals) === 'object') { + + canValidate() { + if (typeof this.conditionals === 'object') { if (this.conditionals['if']) { - if (typeof(this.conditionals['if']) === 'function') { + if (typeof this.conditionals['if'] === 'function') { return this.conditionals['if'](this.model, this.property); - } else if (typeof(this.conditionals['if']) === 'string') { - if (typeof(this.model[this.conditionals['if']]) === 'function') { + } else if (typeof this.conditionals['if'] === 'string') { + if (typeof this.model[this.conditionals['if']] === 'function') { return this.model[this.conditionals['if']](); } else { return get(this.model, this.conditionals['if']); } } } else if (this.conditionals.unless) { - if (typeof(this.conditionals.unless) === 'function') { + if (typeof this.conditionals.unless === 'function') { return !this.conditionals.unless(this.model, this.property); - } else if (typeof(this.conditionals.unless) === 'string') { - if (typeof(this.model[this.conditionals.unless]) === 'function') { + } else if (typeof this.conditionals.unless === 'string') { + if (typeof this.model[this.conditionals.unless] === 'function') { return !this.model[this.conditionals.unless](); } else { return !get(this.model, this.conditionals.unless); @@ -96,7 +111,8 @@ export default Ember.Object.extend({ return true; } }, - compare: function (a, b, operator) { + + compare(a, b, operator) { switch (operator) { case '==': return a == b; // jshint ignore:line case '===': return a === b; diff --git a/addon/validators/local/absence.js b/addon/validators/local/absence.js index 845a1e77..c3717a5d 100644 --- a/addon/validators/local/absence.js +++ b/addon/validators/local/absence.js @@ -2,12 +2,11 @@ import Ember from 'ember'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, isPresent, set } = Ember; export default Base.extend({ - init: function() { - this._super(); + init() { + this._super(...arguments); /*jshint expr:true*/ if (this.options === true) { set(this, 'options', {}); @@ -17,8 +16,8 @@ export default Base.extend({ set(this, 'options.message', Messages.render('present', this.options)); } }, - call: function() { - if (!Ember.isEmpty(get(this.model, this.property))) { + call() { + if (isPresent(get(this.model, this.property))) { this.errors.pushObject(this.options.message); } } diff --git a/addon/validators/local/acceptance.js b/addon/validators/local/acceptance.js index 0864c07d..e406600f 100644 --- a/addon/validators/local/acceptance.js +++ b/addon/validators/local/acceptance.js @@ -2,12 +2,11 @@ import Ember from 'ember'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, set } = Ember; export default Base.extend({ - init: function() { - this._super(); + init() { + this._super(...arguments); /*jshint expr:true*/ if (this.options === true) { set(this, 'options', {}); @@ -17,7 +16,8 @@ export default Base.extend({ set(this, 'options.message', Messages.render('accepted', this.options)); } }, - call: function() { + + call() { if (this.options.accept) { if (get(this.model, this.property) !== this.options.accept) { this.errors.pushObject(this.options.message); diff --git a/addon/validators/local/confirmation.js b/addon/validators/local/confirmation.js index a1ff869d..351ec1e3 100644 --- a/addon/validators/local/confirmation.js +++ b/addon/validators/local/confirmation.js @@ -2,14 +2,13 @@ import Ember from 'ember'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, isPresent, set } = Ember; export default Base.extend({ - init: function() { + init() { this.originalProperty = this.property; - this.property = this.property + 'Confirmation'; - this._super(); + this.property = `${this.property}Confirmation`; + this._super(...arguments); this.dependentValidationKeys.pushObject(this.originalProperty); /*jshint expr:true*/ if (this.options === true) { @@ -17,11 +16,12 @@ export default Base.extend({ set(this, 'options', { message: Messages.render('confirmation', this.options) }); } }, - call: function() { - var original = get(this.model, this.originalProperty); - var confirmation = get(this.model, this.property); - if(!Ember.isEmpty(original) || !Ember.isEmpty(confirmation)) { + call() { + let original = get(this.model, this.originalProperty); + let confirmation = get(this.model, this.property); + + if (isPresent(original) || isPresent(confirmation)) { if (original !== confirmation) { this.errors.pushObject(this.options.message); } diff --git a/addon/validators/local/exclusion.js b/addon/validators/local/exclusion.js index 9d082d1e..1931b9a4 100644 --- a/addon/validators/local/exclusion.js +++ b/addon/validators/local/exclusion.js @@ -1,13 +1,14 @@ import Ember from 'ember'; +import jquery from 'jquery'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, isEmpty, set } = Ember; +const { inArray } = jquery; export default Base.extend({ - init: function() { - this._super(); + init() { + this._super(...arguments); if (this.options.constructor === Array) { set(this, 'options', { 'in': this.options }); } @@ -16,16 +17,17 @@ export default Base.extend({ set(this, 'options.message', Messages.render('exclusion', this.options)); } }, - call: function() { + call() { /*jshint expr:true*/ - var lower, upper; + let lower; + let upper; - if (Ember.isEmpty(get(this.model, this.property))) { + if (isEmpty(get(this.model, this.property))) { if (this.options.allowBlank === undefined) { this.errors.pushObject(this.options.message); } } else if (this.options['in']) { - if (Ember.$.inArray(get(this.model, this.property), this.options['in']) !== -1) { + if (inArray(get(this.model, this.property), this.options['in']) !== -1) { this.errors.pushObject(this.options.message); } } else if (this.options.range) { diff --git a/addon/validators/local/format.js b/addon/validators/local/format.js index d6ed2ce0..029a7dbf 100644 --- a/addon/validators/local/format.js +++ b/addon/validators/local/format.js @@ -2,12 +2,11 @@ import Ember from 'ember'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, isEmpty, set } = Ember; export default Base.extend({ - init: function() { - this._super(); + init() { + this._super(...arguments); if (this.options.constructor === RegExp) { set(this, 'options', { 'with': this.options }); } @@ -15,9 +14,10 @@ export default Base.extend({ if (this.options.message === undefined) { set(this, 'options.message', Messages.render('invalid', this.options)); } - }, - call: function() { - if (Ember.isEmpty(get(this.model, this.property))) { + }, + + call() { + if (isEmpty(get(this.model, this.property))) { if (this.options.allowBlank === undefined) { this.errors.pushObject(this.options.message); } diff --git a/addon/validators/local/inclusion.js b/addon/validators/local/inclusion.js index 869b37f1..1dcb3439 100644 --- a/addon/validators/local/inclusion.js +++ b/addon/validators/local/inclusion.js @@ -1,13 +1,14 @@ import Ember from 'ember'; +import jquery from 'jquery'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, isEmpty, set } = Ember; +const { inArray } = jquery; export default Base.extend({ - init: function() { - this._super(); + init() { + this._super(...arguments); if (this.options.constructor === Array) { set(this, 'options', { 'in': this.options }); } @@ -16,14 +17,17 @@ export default Base.extend({ set(this, 'options.message', Messages.render('inclusion', this.options)); } }, - call: function() { - var lower, upper; - if (Ember.isEmpty(get(this.model, this.property))) { + + call() { + let lower; + let upper; + + if (isEmpty(get(this.model, this.property))) { if (this.options.allowBlank === undefined) { this.errors.pushObject(this.options.message); } } else if (this.options['in']) { - if (Ember.$.inArray(get(this.model, this.property), this.options['in']) === -1) { + if (inArray(get(this.model, this.property), this.options['in']) === -1) { this.errors.pushObject(this.options.message); } } else if (this.options.range) { diff --git a/addon/validators/local/length.js b/addon/validators/local/length.js index e98c7700..8b44cddf 100644 --- a/addon/validators/local/length.js +++ b/addon/validators/local/length.js @@ -2,15 +2,16 @@ import Ember from 'ember'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; -var set = Ember.set; +const { get, isEmpty, set } = Ember; export default Base.extend({ - init: function() { - var index, key; - this._super(); + init() { + let index; + let key; + + this._super(...arguments); /*jshint expr:true*/ - if (typeof(this.options) === 'number') { + if (typeof this.options === 'number') { set(this, 'options', { 'is': this.options }); } @@ -25,56 +26,61 @@ export default Base.extend({ } } - this.options.tokenizer = this.options.tokenizer || function(value) { return value.toString().split(''); }; - // if (typeof(this.options.tokenizer) === 'function') { - // debugger; - // // this.tokenizedLength = new Function('value', 'return ' - // } else { - // this.tokenizedLength = new Function('value', 'return (value || "").' + (this.options.tokenizer || 'split("")') + '.length'); - // } + this.options.tokenizer = this.options.tokenizer || ((value) => value.toString().split('')); }, + CHECKS: { - 'is' : '==', - 'minimum' : '>=', - 'maximum' : '<=' + 'is': '==', + 'minimum': '>=', + 'maximum': '<=' }, + MESSAGES: { - 'is' : 'wrongLength', - 'minimum' : 'tooShort', - 'maximum' : 'tooLong' + 'is': 'wrongLength', + 'minimum': 'tooShort', + 'maximum': 'tooLong' }, - getValue: function(key) { + + getValue(key) { if (this.options[key].constructor === String) { return get(this.model, this.options[key]) || 0; } else { return this.options[key]; } }, - messageKeys: function() { + + messageKeys() { return Object.keys(this.MESSAGES); }, - checkKeys: function() { + + checkKeys() { return Object.keys(this.CHECKS); }, - renderMessageFor: function(key) { - var options = {count: this.getValue(key)}, _key; + + renderMessageFor(key) { + let options = { count: this.getValue(key) }; + let _key; + for (_key in this.options) { options[_key] = this.options[_key]; } return this.options.messages[this.MESSAGES[key]] || Messages.render(this.MESSAGES[key], options); }, - renderBlankMessage: function() { + + renderBlankMessage() { if (this.options.is) { return this.renderMessageFor('is'); } else if (this.options.minimum) { return this.renderMessageFor('minimum'); } }, - call: function() { - var key, comparisonResult; - if (Ember.isEmpty(get(this.model, this.property))) { + call() { + let key; + let comparisonResult; + + if (isEmpty(get(this.model, this.property))) { if (this.options.allowBlank === undefined && (this.options.is || this.options.minimum)) { this.errors.pushObject(this.renderBlankMessage()); } diff --git a/addon/validators/local/numericality.js b/addon/validators/local/numericality.js index c9bd6a7d..bfea0c85 100644 --- a/addon/validators/local/numericality.js +++ b/addon/validators/local/numericality.js @@ -1,15 +1,20 @@ import Ember from 'ember'; +import jquery from 'jquery'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; import Patterns from 'ember-validations/patterns'; -var get = Ember.get; +const { get, isEmpty } = Ember; +const { inArray } = jquery; export default Base.extend({ - init: function() { + init() { /*jshint expr:true*/ - var index, keys, key; - this._super(); + let index; + let keys; + let key; + + this._super(...arguments); if (this.options === true) { this.options = {}; @@ -29,10 +34,10 @@ export default Base.extend({ } keys = Object.keys(this.CHECKS).concat(['odd', 'even']); - for(index = 0; index < keys.length; index++) { + for (index = 0; index < keys.length; index++) { key = keys[index]; - var prop = this.options[key]; + let prop = this.options[key]; // I have no idea what the hell is going on here. This seems to do nothing. // The observer's key is being set to the values in the options hash? if (key in this.options && isNaN(prop)) { @@ -40,7 +45,7 @@ export default Base.extend({ } if (prop !== undefined && this.options.messages[key] === undefined) { - if (Ember.$.inArray(key, Object.keys(this.CHECKS)) !== -1) { + if (inArray(key, Object.keys(this.CHECKS)) !== -1) { this.options.count = prop; } this.options.messages[key] = Messages.render(key, this.options); @@ -50,17 +55,21 @@ export default Base.extend({ } } }, + CHECKS: { - equalTo : '===', - greaterThan : '>', - greaterThanOrEqualTo : '>=', - lessThan : '<', - lessThanOrEqualTo : '<=' + equalTo: '===', + greaterThan: '>', + greaterThanOrEqualTo: '>=', + lessThan: '<', + lessThanOrEqualTo: '<=' }, - call: function() { - var check, checkValue, comparisonResult; - if (Ember.isEmpty(get(this.model, this.property))) { + call() { + let check; + let checkValue; + let comparisonResult; + + if (isEmpty(get(this.model, this.property))) { if (this.options.allowBlank === undefined) { this.errors.pushObject(this.options.messages.numericality); } diff --git a/addon/validators/local/presence.js b/addon/validators/local/presence.js index 23754e11..5cb628d6 100644 --- a/addon/validators/local/presence.js +++ b/addon/validators/local/presence.js @@ -2,11 +2,11 @@ import Ember from 'ember'; import Base from 'ember-validations/validators/base'; import Messages from 'ember-validations/messages'; -var get = Ember.get; +const { get, isBlank } = Ember; export default Base.extend({ - init: function() { - this._super(); + init() { + this._super(...arguments); /*jshint expr:true*/ if (this.options === true) { this.options = {}; @@ -16,8 +16,8 @@ export default Base.extend({ this.options.message = Messages.render('blank', this.options); } }, - call: function() { - if (Ember.isBlank(get(this.model, this.property))) { + call() { + if (isBlank(get(this.model, this.property))) { this.errors.pushObject(this.options.message); } } diff --git a/package.json b/package.json index 7ee5f265..cc425dfa 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "ember-export-application-global": "^1.0.5", "ember-load-initializers": "^0.5.1", "ember-resolver": "^2.0.3", + "ember-suave": "4.0.0", "ember-test-container": "0.1.0", "loader.js": "^4.0.1" }, diff --git a/tests/dummy/app/app.js b/tests/dummy/app/app.js index 831ad610..df552024 100644 --- a/tests/dummy/app/app.js +++ b/tests/dummy/app/app.js @@ -3,11 +3,13 @@ import Resolver from './resolver'; import loadInitializers from 'ember-load-initializers'; import config from './config/environment'; +const { Application: EmberApplication } = Ember; + let App; Ember.MODEL_FACTORY_INJECTIONS = true; -App = Ember.Application.extend({ +App = EmberApplication.extend({ modulePrefix: config.modulePrefix, podModulePrefix: config.podModulePrefix, Resolver diff --git a/tests/dummy/app/controllers/foo.js b/tests/dummy/app/controllers/foo.js index 55ec0308..8070e24b 100644 --- a/tests/dummy/app/controllers/foo.js +++ b/tests/dummy/app/controllers/foo.js @@ -1,7 +1,9 @@ import Ember from 'ember'; import EmberValidations from 'ember-validations'; -export default Ember.Controller.extend(EmberValidations, { +const { Controller } = Ember; + +export default Controller.extend(EmberValidations, { validations: { foo: { presence: true diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js index cdc25787..397d1a35 100644 --- a/tests/dummy/app/router.js +++ b/tests/dummy/app/router.js @@ -1,7 +1,9 @@ import Ember from 'ember'; import config from './config/environment'; -const Router = Ember.Router.extend({ +const { Router: EmberRouter } = Ember; + +const Router = EmberRouter.extend({ location: config.locationType, rootURL: config.rootURL }); diff --git a/tests/helpers/destroy-app.js b/tests/helpers/destroy-app.js index c3d4d1ab..926d7563 100644 --- a/tests/helpers/destroy-app.js +++ b/tests/helpers/destroy-app.js @@ -1,5 +1,7 @@ import Ember from 'ember'; +const { run } = Ember; + export default function destroyApp(application) { - Ember.run(application, 'destroy'); + run(application, 'destroy'); } diff --git a/tests/helpers/module-for-acceptance.js b/tests/helpers/module-for-acceptance.js index 76996fd0..c1184b8c 100644 --- a/tests/helpers/module-for-acceptance.js +++ b/tests/helpers/module-for-acceptance.js @@ -11,12 +11,12 @@ export default function(name, options = {}) { this.application = startApp(); if (options.beforeEach) { - return options.beforeEach.apply(this, arguments); + return options.beforeEach.call(this, ...arguments); } }, afterEach() { - let afterEach = options.afterEach && options.afterEach.apply(this, arguments); + let afterEach = options.afterEach && options.afterEach.call(this, ...arguments); return Promise.resolve(afterEach).then(() => destroyApp(this.application)); } }); diff --git a/tests/helpers/start-app.js b/tests/helpers/start-app.js index e098f1d5..83f842c5 100644 --- a/tests/helpers/start-app.js +++ b/tests/helpers/start-app.js @@ -2,13 +2,15 @@ import Ember from 'ember'; import Application from '../../app'; import config from '../../config/environment'; +const { merge, run } = Ember; + export default function startApp(attrs) { let application; - let attributes = Ember.merge({}, config.APP); - attributes = Ember.merge(attributes, attrs); // use defaults, but you can override; + let attributes = merge({}, config.APP); + attributes = merge(attributes, attrs); // use defaults, but you can override; - Ember.run(() => { + run(() => { application = Application.create(attributes); application.setupForTesting(); application.injectTestHelpers(); diff --git a/tests/unit/conditional-validators-test.js b/tests/unit/conditional-validators-test.js index b76c5d7d..68884be2 100644 --- a/tests/unit/conditional-validators-test.js +++ b/tests/unit/conditional-validators-test.js @@ -2,17 +2,23 @@ import Ember from 'ember'; import { moduleFor, test } from 'ember-qunit'; import Mixin from 'ember-validations/mixin'; -var user, User, promise; -var get = Ember.get; -var set = Ember.set; -var run = Ember.run; - +let user; +let User; +let promise; + +const { + Object: EmberObject, + get, + isEmpty, + run, + set +} = Ember; moduleFor('object:user', 'Conditional Validations', { integration: true, beforeEach() { - User = Ember.Object.extend(Mixin); + User = EmberObject.extend(Mixin); this.registry.register('object:user', User); } }); @@ -24,7 +30,7 @@ test('if with function', function(assert) { validations: { firstName: { presence: { - if: function() { + if() { return false; } } @@ -36,18 +42,18 @@ test('if with function', function(assert) { user = this.subject(); promise = user.validate().then(() => { - assert.ok(Ember.isEmpty(get(user.errors, 'firstName'))); + assert.ok(isEmpty(get(user.errors, 'firstName'))); - var validator = get(user.validators, 'firstObject'); + let validator = get(user.validators, 'firstObject'); - validator.conditionals['if'] = function(model, property) { - assert.equal(user, model, "the conditional validator is passed the model being validated"); - assert.equal(property, 'firstName', "the conditional validator is passed the name of the property being validated"); + validator.conditionals.if = function(model, property) { + assert.equal(user, model, 'the conditional validator is passed the model being validated'); + assert.equal(property, 'firstName', 'the conditional validator is passed the name of the property being validated'); return true; }; user.validate().catch(() => { - assert.deepEqual(get(user.errors, 'firstName'), ["can't be blank"]); + assert.deepEqual(get(user.errors, 'firstName'), ['can\'t be blank']); }); }); }); @@ -66,19 +72,18 @@ test('if with property reference', function(assert) { } }); - run(() => { user = this.subject(); set(user, 'canValidate', false); promise = user.validate().then(() => { - assert.ok(Ember.isEmpty(get(user.errors, 'firstName'))); + assert.ok(isEmpty(get(user.errors, 'firstName'))); set(user, 'canValidate', true); user.validate().catch(() => { - assert.deepEqual(get(user.errors, 'firstName'), ["can't be blank"]); + assert.deepEqual(get(user.errors, 'firstName'), ['can\'t be blank']); set(user, 'canValidate', false); assert.deepEqual(get(user.errors, 'firstName'), []); }); @@ -97,21 +102,24 @@ test('if with function reference', function(assert) { } } }, - canValidate: function() { + + canValidate() { return false; } }); run(() => { user = this.subject(); - promise = user.validate().then(function(){ - assert.ok(Ember.isEmpty(get(user.errors, 'firstName'))); + promise = user.validate().then(function() { + assert.ok(isEmpty(get(user.errors, 'firstName'))); set(user, 'canValidate', true); + user.canValidate = function() { return true; }; - user.validate().then(null, function(){ - assert.deepEqual(get(user.errors, 'firstName'), ["can't be blank"]); + + user.validate().catch(function() { + assert.deepEqual(get(user.errors, 'firstName'), ['can\'t be blank']); }); }); }); @@ -125,7 +133,7 @@ test('unless with function', function(assert) { validations: { firstName: { presence: { - unless: function() { + unless() { return true; } } @@ -135,16 +143,16 @@ test('unless with function', function(assert) { run(() => { user = this.subject(); - promise = user.validate().then(function(){ - assert.ok(Ember.isEmpty(get(user.errors, 'firstName'))); - var validator = get(user.validators, 'firstObject'); - validator.conditionals['unless'] = function(model, property) { - assert.equal(user, model, "the conditional validator is passed the model being validated"); - assert.equal(property, 'firstName', "the conditional validator is passed the name of the property being validated"); + promise = user.validate().then(function() { + assert.ok(isEmpty(get(user.errors, 'firstName'))); + let validator = get(user.validators, 'firstObject'); + validator.conditionals.unless = function(model, property) { + assert.equal(user, model, 'the conditional validator is passed the model being validated'); + assert.equal(property, 'firstName', 'the conditional validator is passed the name of the property being validated'); return false; }; - user.validate().then(null, function(){ - assert.deepEqual(get(user.errors, 'firstName'), ["can't be blank"]); + user.validate().catch(function() { + assert.deepEqual(get(user.errors, 'firstName'), ['can\'t be blank']); }); }); }); @@ -166,11 +174,11 @@ test('unless with property reference', function(assert) { run(() => { user = this.subject(); - promise = user.validate().then(function(){ - assert.ok(Ember.isEmpty(get(user.errors, 'firstName'))); + promise = user.validate().then(function() { + assert.ok(isEmpty(get(user.errors, 'firstName'))); set(user, 'canValidate', false); - user.validate().then(null, function(){ - assert.deepEqual(get(user.errors, 'firstName'), ["can't be blank"]); + user.validate().catch(function() { + assert.deepEqual(get(user.errors, 'firstName'), ['can\'t be blank']); set(user, 'canValidate', true); assert.deepEqual(get(user.errors, 'firstName'), []); }); @@ -189,21 +197,21 @@ test('unless with function reference', function(assert) { } } }, - canValidate: function() { + canValidate() { return true; } }); run(() => { user = this.subject(); - promise = user.validate().then(function(){ - assert.ok(Ember.isEmpty(get(user.errors, 'firstName'))); + promise = user.validate().then(function() { + assert.ok(isEmpty(get(user.errors, 'firstName'))); set(user, 'canValidate', true); user.canValidate = function() { return false; }; - user.validate().then(null, function(){ - assert.deepEqual(get(user.errors, 'firstName'), ["can't be blank"]); + user.validate().catch(function() { + assert.deepEqual(get(user.errors, 'firstName'), ['can\'t be blank']); }); }); }); diff --git a/tests/unit/controller-test.js b/tests/unit/controller-test.js index 5a93b499..bc1a79dd 100644 --- a/tests/unit/controller-test.js +++ b/tests/unit/controller-test.js @@ -8,6 +8,6 @@ moduleFor('controller:foo', 'Controller sanity test', { }); test('does not blow up', function(assert) { - var controller = this.subject(); + let controller = this.subject(); assert.ok(controller); }); diff --git a/tests/unit/errors-test.js b/tests/unit/errors-test.js index 26870b82..98711bf3 100644 --- a/tests/unit/errors-test.js +++ b/tests/unit/errors-test.js @@ -2,16 +2,21 @@ import Ember from 'ember'; import { moduleFor, test } from 'ember-qunit'; import Mixin from 'ember-validations/mixin'; -var user, User; -var get = Ember.get; -var set = Ember.set; -var run = Ember.run; +let user; +let User; + +const { + Object: EmberObject, + get, + run, + set +} = Ember; moduleFor('object:user', 'Errors test', { integration: true, - beforeEach: function() { - User = Ember.Object.extend(Mixin, { + beforeEach() { + User = EmberObject.extend(Mixin, { validations: { name: { presence: true @@ -26,7 +31,8 @@ moduleFor('object:user', 'Errors test', { this.registry.register('object:user', User); }, - afterEach: function() { + afterEach() { + // jscs:disable disallowDirectPropertyAccess delete Ember.I18n; } }); @@ -39,7 +45,7 @@ test('validations are run on instantiation - invalid', function(assert) { }); test('validations are run on instantiation - valid', function(assert) { - run(() => user = this.subject({name: 'Brian', age: 33})); + run(() => user = this.subject({ name: 'Brian', age: 33 })); assert.ok(get(user, 'isValid')); assert.ok(Ember.isEmpty(get(user, 'errors.name'))); assert.ok(Ember.isEmpty(get(user, 'errors.age'))); @@ -66,41 +72,3 @@ test('when errors are resolved', function(assert) { assert.ok(Ember.isEmpty(get(user, 'errors.name'))); assert.ok(Ember.isEmpty(get(user, 'errors.age'))); }); - -// test('validations use Ember.I18n.t to render the message if Ember.I18n is present', function() { - // Ember.I18n = { - // translations: { - // errors: { - // blank: 'muss ausgefüllt werden', - // notANumber: 'ist keine Zahl' - // } - // }, - // lookupKey: function(key, hash) { - // var firstKey, idx, remainingKeys; - - // if (hash[key] !== null && hash[key] !== undefined) { return hash[key]; } - - // if ((idx = key.indexOf('.')) !== -1) { - // firstKey = key.substr(0, idx); - // remainingKeys = key.substr(idx + 1); - // hash = hash[firstKey]; - // if (hash) { return Ember.I18n.lookupKey(remainingKeys, hash); } - // } - // }, - // t: function(key, context) { - // return Ember.I18n.lookupKey(key, Ember.I18n.translations); - // } - // }; - - // run(function() { - // user = User.create(); - // }); - // equal(get(user, 'isValid'), false); - // assert.deepEqual(get(user, 'errors.name'), ['muss ausgefüllt werden']); - // assert.deepEqual(get(user, 'errors.age'), ['muss ausgefüllt werden', 'ist keine Zahl']); - // run(function() { - // set(user, 'age', 'thirty three'); - // }); - // equal(get(user, 'isValid'), false); - // assert.deepEqual(get(user, 'errors.age'), ['ist keine Zahl']); -// }); diff --git a/tests/unit/helpers/validate-properties-test.js b/tests/unit/helpers/validate-properties-test.js index dbb6be10..691d857c 100644 --- a/tests/unit/helpers/validate-properties-test.js +++ b/tests/unit/helpers/validate-properties-test.js @@ -24,11 +24,10 @@ testValidPropertyValues('baz', ['Winston', '12345', null, undefined, ''], functi subject.set('isBaz', false); }); - moduleFor('controller:foo', 'Unit - Ensure validate properties test helpers fail when invalid', { integration: true, - beforeEach: function(assert) { + beforeEach(assert) { // use inverse of deepEqual to ensure the test helpers fail when invalid assert.deepEqual = assert.notDeepEqual; } diff --git a/tests/unit/validate-test.js b/tests/unit/validate-test.js index 74d3995c..2d38286f 100644 --- a/tests/unit/validate-test.js +++ b/tests/unit/validate-test.js @@ -3,16 +3,27 @@ import { moduleFor, test } from 'ember-qunit'; import EmberValidations, { validator } from 'ember-validations'; import Base from 'ember-validations/validators/base'; -var user, User, promise; -var get = Ember.get; -var set = Ember.set; -var run = Ember.run; +let user; +let User; +let promise; + +const { + A: emberArray, + ArrayController, + K, + Object: EmberObject, + ObjectController, + get, + isEmpty, + run, + set +} = Ember; moduleFor('object:user', 'Validate test', { integration: true, - beforeEach: function() { - User = Ember.Object.extend(EmberValidations, { + beforeEach() { + User = EmberObject.extend(EmberValidations, { validations: { firstName: { presence: true, @@ -42,25 +53,25 @@ test('returns a promise', function(assert) { test('isInvalid tracks isValid', function(assert) { assert.equal(get(user, 'isInvalid'), true); - run(() => user.setProperties({firstName: 'Brian', lastName: 'Cardarella'})); + run(() => user.setProperties({ firstName: 'Brian', lastName: 'Cardarella' })); assert.equal(get(user, 'isInvalid'), false); }); test('runs all validations', function(assert) { - run(function(){ - promise = user.validate().then(null, function(errors){ + run(() => { + promise = user.validate().catch(function(errors) { assert.deepEqual(get(errors, 'firstName'), ["can't be blank", 'is the wrong length (should be 5 characters)']); - assert.deepEqual(get(errors, 'lastName'), ["is invalid"]); + assert.deepEqual(get(errors, 'lastName'), ['is invalid']); assert.equal(get(user, 'isValid'), false); set(user, 'firstName', 'Bob'); - user.validate('firstName').then(null, function(errors){ + user.validate('firstName').catch(function(errors) { assert.deepEqual(get(errors, 'firstName'), ['is the wrong length (should be 5 characters)']); assert.equal(get(user, 'isValid'), false); set(user, 'firstName', 'Brian'); set(user, 'lastName', 'Cardarella'); - user.validate().then(function(errors){ - assert.ok(Ember.isEmpty(get(errors, 'firstName'))); - assert.ok(Ember.isEmpty(get(errors, 'lastName'))); + user.validate().then(function(errors) { + assert.ok(isEmpty(get(errors, 'firstName'))); + assert.ok(isEmpty(get(errors, 'lastName'))); assert.equal(get(user, 'isValid'), true); }); }); @@ -70,11 +81,13 @@ test('runs all validations', function(assert) { return promise; }); -if (Ember.ObjectController) { +if (ObjectController) { test('can be mixed into an controller', function(assert) { - var Controller, controller, user; + let Controller; + let controller; + let user; - Controller = Ember.ObjectController.extend(EmberValidations, { + Controller = ObjectController.extend(EmberValidations, { validations: { name: { presence: true @@ -87,7 +100,7 @@ if (Ember.ObjectController) { run(() => controller = this.container.lookupFactory('controller:user').create()); assert.equal(get(controller, 'isValid'), false); - user = Ember.Object.create(); + user = EmberObject.create(); run(() => set(controller, 'model', user)); assert.equal(get(controller, 'isValid'), false); @@ -96,15 +109,18 @@ if (Ember.ObjectController) { }); } -if (Ember.ArrayController) { +if (ObjectController && ArrayController) { moduleFor('controller:user', 'Array controller', { integration: true }); test('can be mixed into an array controller', function(assert) { - var Controller, controller, user, UserController; + let Controller; + let controller; + let user; + let UserController; - UserController = Ember.ObjectController.extend(EmberValidations, { + UserController = ObjectController.extend(EmberValidations, { validations: { name: { presence: true @@ -114,7 +130,7 @@ if (Ember.ArrayController) { this.registry.register('controller:user', UserController); - Controller = Ember.ArrayController.extend(EmberValidations, { + Controller = ArrayController.extend(EmberValidations, { itemController: 'User', validations: { '[]': true @@ -127,7 +143,7 @@ if (Ember.ArrayController) { assert.equal(get(controller, 'isValid'), true); - user = Ember.Object.create(); + user = EmberObject.create(); run(() => controller.pushObject(user)); @@ -141,13 +157,14 @@ if (Ember.ArrayController) { }); } -var Profile, profile; +let Profile; +let profile; moduleFor('object:profile', 'Relationship validators', { integration: true, - beforeEach: function() { - Profile = Ember.Object.extend(EmberValidations, { + beforeEach() { + Profile = EmberObject.extend(EmberValidations, { validations: { title: { presence: true @@ -155,7 +172,7 @@ moduleFor('object:profile', 'Relationship validators', { } }); - User = Ember.Object.extend(EmberValidations); + User = EmberObject.extend(EmberValidations); this.registry.register('object:profile', Profile); this.registry.register('object:user', User); @@ -185,22 +202,9 @@ test('validates other validatable property', function(assert) { assert.equal(get(user, 'isValid'), true); }); -// test('validates custom validator', function() { - // run(function() { - // user = User.create({ - // profile: profile, - // validations: [AgeValidator] - // }); - // }); - // equal(get(user, 'isValid'), false); - // run(function() { - // set(user, 'age', 22); - // }); - // equal(get(user, 'isValid'), true); -// }); - test('validates array of validable objects', function(assert) { - var friend1, friend2; + let friend1; + let friend2; run(() => { user = this.factory('object:user').create({ @@ -213,7 +217,7 @@ test('validates array of validable objects', function(assert) { assert.equal(get(user, 'isValid'), true); run(function() { - set(user, 'friends', Ember.A()); + set(user, 'friends', emberArray()); }); assert.equal(get(user, 'isValid'), true); @@ -261,9 +265,9 @@ test('validates array of validable objects', function(assert) { assert.equal(get(user, 'isValid'), true); }); - test('revalidates arrays when they are replaced', function(assert) { - var friend1, friend2; + let friend1; + let friend2; run(() => { user = this.factory('object:user').create({ @@ -276,7 +280,7 @@ test('revalidates arrays when they are replaced', function(assert) { assert.equal(get(user, 'isValid'), true); run(function() { - set(user, 'friends', Ember.A()); + set(user, 'friends', emberArray()); }); assert.equal(get(user, 'isValid'), true); @@ -292,7 +296,7 @@ test('revalidates arrays when they are replaced', function(assert) { }); run(function() { - set(user, 'friends', Ember.A([friend1])); + set(user, 'friends', emberArray([friend1])); }); assert.equal(get(user, 'isValid'), false); @@ -312,7 +316,7 @@ test('revalidates arrays when they are replaced', function(assert) { } }); - set(user, 'friends', Ember.A([friend1, friend2])); + set(user, 'friends', emberArray([friend1, friend2])); }); assert.equal(get(user, 'isValid'), false); @@ -327,22 +331,22 @@ test('revalidates arrays when they are replaced', function(assert) { moduleFor('object:user', 'validator class lookup order', { integration: true, - beforeEach: function() { - User = Ember.Object.extend(EmberValidations); + beforeEach() { + User = EmberObject.extend(EmberValidations); this.registry.register('object:user', User); } }); test('should lookup in project namespace first', function(assert) { - var dummyValidatorCalled = false; - var nativeValidatorCalled = false; + let dummyValidatorCalled = false; + let nativeValidatorCalled = false; this.registry.register('ember-validations@validator:local/presence', Base.extend({ init() { this._super(...arguments); nativeValidatorCalled = true; }, - call: Ember.K + call: K })); this.registry.register('validator:local/presence', Base.extend({ @@ -350,7 +354,7 @@ test('should lookup in project namespace first', function(assert) { this._super(...arguments); dummyValidatorCalled = true; }, - call: Ember.K + call: K })); run(() => { @@ -368,16 +372,15 @@ test('should lookup in project namespace first', function(assert) { }); test('will lookup both local and remote validators of similar name', function(assert) { - var localValidatorCalled = false; - var remoteValidatorCalled = false; - + let localValidatorCalled = false; + let remoteValidatorCalled = false; this.registry.register('validator:local/uniqueness', Base.extend({ init() { this._super(...arguments); localValidatorCalled = true; }, - call: Ember.K + call: K })); this.registry.register('validator:remote/uniqueness', Base.extend({ @@ -385,7 +388,7 @@ test('will lookup both local and remote validators of similar name', function(as this._super(...arguments); remoteValidatorCalled = true; }, - call: Ember.K + call: K })); run(() => { @@ -403,16 +406,15 @@ test('will lookup both local and remote validators of similar name', function(as }); test('should prefer lookup in just "validators" before "native"', function(assert) { - var dummyValidatorCalled = false; - var nativeValidatorCalled = false; - + let dummyValidatorCalled = false; + let nativeValidatorCalled = false; this.registry.register('ember-validations@validator:remote/uniqueness', Base.extend({ init() { this._super(...arguments); nativeValidatorCalled = true; }, - call: Ember.K + call: K })); this.registry.register('validator:presence', Base.extend({ @@ -420,7 +422,7 @@ test('should prefer lookup in just "validators" before "native"', function(asser this._super(...arguments); dummyValidatorCalled = true; }, - call: Ember.K + call: K })); run(() => { @@ -438,16 +440,15 @@ test('should prefer lookup in just "validators" before "native"', function(asser }); test('should store validators in cache for faster lookup', function(assert) { - var validatorResolvedCount = 0; - - var oldResolveRegistration = this.registry.resolveRegistration; + let validatorResolvedCount = 0; + let oldResolveRegistration = this.registry.resolveRegistration; this.registry.resolveRegistration = (fullName) => { validatorResolvedCount += 1; return oldResolveRegistration.call(this.registry, fullName); }; - var user2; + let user2; run(() => { user = this.subject({ @@ -479,13 +480,13 @@ test('should store validators in cache for faster lookup', function(assert) { moduleFor('object:user', 'inline validations', { integration: true, - beforeEach: function() { - User = Ember.Object.extend(EmberValidations); + beforeEach() { + User = EmberObject.extend(EmberValidations); this.registry.register('object:user', User); } }); -test("mixed validation syntax", function(assert) { +test('mixed validation syntax', function(assert) { run(() => { user = this.subject({ validations: { @@ -501,7 +502,7 @@ test("mixed validation syntax", function(assert) { assert.deepEqual(['it failed'], get(user, 'errors.name')); }); -test("concise validation syntax", function(assert) { +test('concise validation syntax', function(assert) { run(() => { user = this.subject({ validations: { diff --git a/tests/unit/validators/base-test.js b/tests/unit/validators/base-test.js index 9a7ed441..d05a8578 100644 --- a/tests/unit/validators/base-test.js +++ b/tests/unit/validators/base-test.js @@ -2,22 +2,30 @@ import Ember from 'ember'; import { moduleFor, test } from 'ember-qunit'; import Base from 'ember-validations/validators/base'; -var model, Model, CustomValidator, validator; -var get = Ember.get; -var run = Ember.run; +let model; +let Model; +let CustomValidator; +let validator; + +const { + Object: EmberObject, + get, + run +} = Ember; moduleFor('object:model', 'Base Validator', { integration: true, - beforeEach: function() { - Model = Ember.Object.extend({ + beforeEach() { + Model = EmberObject.extend({ dependentValidationKeys: {} }); CustomValidator = Base.extend({ - init: function() { + init() { this._super(); this.dependentValidationKeys.pushObject('otherAttribute'); }, - call: function() {} + call() { + } }); this.registry.register('object:model', Model); @@ -26,37 +34,31 @@ moduleFor('object:model', 'Base Validator', { }); test('when value is not empty', function(assert) { - run(function() { - validator = CustomValidator.create({model: model, property: 'attribute'}); - }); + run(() => validator = CustomValidator.create({ model, property: 'attribute' })); assert.equal(get(validator, 'isValid'), true); }); test('validator has isInvalid flag', function(assert) { - run(function() { - validator = CustomValidator.create({model: model, property: 'attribute'}); - }); + run(() => validator = CustomValidator.create({ model, property: 'attribute' })); assert.equal(get(validator, 'isInvalid'), false); }); test('generates dependentValidationKeys on the model', function(assert) { - run(function() { - validator = CustomValidator.create({model: model, property: 'attribute'}); - }); - assert.deepEqual(get(model, 'dependentValidationKeys'), {attribute: ['otherAttribute']}); + run(() => validator = CustomValidator.create({ model, property: 'attribute' })); + assert.deepEqual(get(model, 'dependentValidationKeys'), { attribute: ['otherAttribute'] }); }); test('inactive validators should be considered valid', function(assert) { - var canValidate = true; - run(function() { + let canValidate = true; + run(() => { validator = CustomValidator.create({ - model: model, + model, property: 'attribute', - canValidate: function() { + canValidate() { return canValidate; }, - call: function() { - this.errors.pushObject("nope"); + call() { + this.errors.pushObject('nope'); } }); }); diff --git a/tests/unit/validators/local/absence-test.js b/tests/unit/validators/local/absence-test.js index fa9e76fc..903bfbc0 100644 --- a/tests/unit/validators/local/absence-test.js +++ b/tests/unit/validators/local/absence-test.js @@ -2,48 +2,45 @@ import Ember from 'ember'; import { module, test } from 'qunit'; import Absence from 'ember-validations/validators/local/absence'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Absence Validator', { - setup: function() { - Model = Ember.Object.extend({ + setup() { + Model = EmberObject.extend({ dependentValidationKeys: {} }); - run(function() { - model = Model.create(); - }); + run(() => model = Model.create()); } }); test('when value is not empty', function(assert) { options = { message: 'failed validation' }; - run(function(){ - validator = Absence.create({model: model, property: 'attribute', options: options}); - }); + run(() => validator = Absence.create({ model, property: 'attribute', options })); assert.deepEqual(validator.errors, []); - run(function() { - set(model, 'attribute', 'not empty'); - }); + run(() => set(model, 'attribute', 'not empty')); assert.deepEqual(validator.errors, ['failed validation']); }); test('when value is made empty', function(assert) { set(model, 'attribute', 'not empty'); options = { message: 'failed validation' }; - run(function(){ - validator = Absence.create({model: model, property: 'attribute', options: options}); - set(model, 'attribute', undefined); - }); + run(() => validator = Absence.create({ model, property: 'attribute', options })); + run(() => set(model, 'attribute', undefined)); assert.deepEqual(validator.errors, []); }); test('when options is true', function(assert) { options = true; - run(function(){ - validator = Absence.create({model: model, property: 'attribute', options: options}); - set(model, 'attribute', 'not empty'); - }); - assert.deepEqual(validator.errors, ["must be blank"]); + run(() => validator = Absence.create({ model, property: 'attribute', options })); + run(() => set(model, 'attribute', 'not empty')); + assert.deepEqual(validator.errors, ['must be blank']); }); diff --git a/tests/unit/validators/local/acceptance-test.js b/tests/unit/validators/local/acceptance-test.js index 568dd440..1d09b751 100644 --- a/tests/unit/validators/local/acceptance-test.js +++ b/tests/unit/validators/local/acceptance-test.js @@ -2,25 +2,30 @@ import Ember from 'ember'; import { module, test } from 'qunit'; import Acceptance from 'ember-validations/validators/local/acceptance'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Acceptance Validator', { - setup: function() { - Model = Ember.Object.extend({ + setup() { + Model = EmberObject.extend({ dependentValidationKeys: {} }); - run(function() { - model = Model.create(); - }); + run(() => model = Model.create()); } }); test('when attribute is true', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', true); }); assert.deepEqual(validator.errors, []); @@ -29,7 +34,7 @@ test('when attribute is true', function(assert) { test('when attribute is not true', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', false); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -38,7 +43,7 @@ test('when attribute is not true', function(assert) { test('when attribute is value of 1', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, []); @@ -47,7 +52,7 @@ test('when attribute is value of 1', function(assert) { test('when attribute value is 2 and accept value is 2', function(assert) { options = { message: 'failed validation', accept: 2 }; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', 2); }); assert.deepEqual(validator.errors, []); @@ -56,7 +61,7 @@ test('when attribute value is 2 and accept value is 2', function(assert) { test('when attribute value is 1 and accept value is 2', function(assert) { options = { message: 'failed validation', accept: 2 }; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -65,7 +70,7 @@ test('when attribute value is 1 and accept value is 2', function(assert) { test('when options is true', function(assert) { options = true; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', false); }); assert.deepEqual(validator.errors, ['must be accepted']); @@ -74,7 +79,7 @@ test('when options is true', function(assert) { test('when no message is passed', function(assert) { options = { accept: 2 }; run(function() { - validator = Acceptance.create({model: model, property: 'attribute', options: options}); + validator = Acceptance.create({ model, property: 'attribute', options }); set(model, 'attribute', false); }); assert.deepEqual(validator.errors, ['must be accepted']); diff --git a/tests/unit/validators/local/confirmation-test.js b/tests/unit/validators/local/confirmation-test.js index beaac3e3..de83c228 100644 --- a/tests/unit/validators/local/confirmation-test.js +++ b/tests/unit/validators/local/confirmation-test.js @@ -3,16 +3,24 @@ import { moduleFor, test } from 'ember-qunit'; import Confirmation from 'ember-validations/validators/local/confirmation'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var get = Ember.get; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + get, + isEmpty, + run, + set +} = Ember; moduleFor('object:model', 'Confirmation Validator', { integration: true, - beforeEach: function() { - Model = Ember.Object.extend(Mixin); + beforeEach() { + Model = EmberObject.extend(Mixin); this.registry.register('object:model', Model); run(() => model = this.subject()); } @@ -21,7 +29,7 @@ moduleFor('object:model', 'Confirmation Validator', { test('when values match', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Confirmation.create({model: model, property: 'attribute', options: options}); + validator = Confirmation.create({ model, property: 'attribute', options }); set(model, 'attribute', 'test'); set(model, 'attributeConfirmation', 'test'); }); @@ -39,7 +47,7 @@ test('when values match', function(assert) { test('when values do not match', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Confirmation.create({model: model, property: 'attribute', options: options}); + validator = Confirmation.create({ model, property: 'attribute', options }); set(model, 'attribute', 'test'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -47,31 +55,32 @@ test('when values do not match', function(assert) { test('when original is null', function(assert) { run(function() { - validator = Confirmation.create({model: model, property: 'attribute'}); + validator = Confirmation.create({ model, property: 'attribute' }); model.set('attribute', null); }); - assert.ok(Ember.isEmpty(validator.errors)); + assert.ok(isEmpty(validator.errors)); }); test('when confirmation is null', function(assert) { run(function() { - validator = Confirmation.create({model: model, property: 'attribute'}); + validator = Confirmation.create({ model, property: 'attribute' }); model.set('attributeConfirmation', null); }); - assert.ok(Ember.isEmpty(validator.errors)); + assert.ok(isEmpty(validator.errors)); }); test('when options is true', function(assert) { options = true; run(function() { - validator = Confirmation.create({model: model, property: 'attribute', options: options}); + validator = Confirmation.create({ model, property: 'attribute', options }); set(model, 'attribute', 'test'); }); assert.deepEqual(validator.errors, ["doesn't match attribute"]); }); test('message integration on model, prints message on Confirmation property', function(assert) { - var otherModel, OtherModel = this.container.lookupFactory('object:model').extend({ + let otherModel; + let OtherModel = this.container.lookupFactory('object:model').extend({ validations: { attribute: { confirmation: true diff --git a/tests/unit/validators/local/exclusion-test.js b/tests/unit/validators/local/exclusion-test.js index 80ec5f1b..51b2cd1c 100644 --- a/tests/unit/validators/local/exclusion-test.js +++ b/tests/unit/validators/local/exclusion-test.js @@ -3,13 +3,20 @@ import { module, test } from 'qunit'; import Exclusion from 'ember-validations/validators/local/exclusion'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Exclusion Validator', { - setup: function() { - Model = Ember.Object.extend(Mixin); + setup() { + Model = EmberObject.extend(Mixin); run(function() { model = Model.create(); }); @@ -19,7 +26,7 @@ module('Exclusion Validator', { test('when value is not in the list', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3] }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 4); }); assert.deepEqual(validator.errors, []); @@ -28,7 +35,7 @@ test('when value is not in the list', function(assert) { test('when value is in the list', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3] }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -37,7 +44,7 @@ test('when value is in the list', function(assert) { test('when allowing blank', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3], allowBlank: true }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); }); assert.deepEqual(validator.errors, []); }); @@ -45,7 +52,7 @@ test('when allowing blank', function(assert) { test('when not allowing blank', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3] }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -54,7 +61,7 @@ test('when not allowing blank', function(assert) { test('when value is not in the range', function(assert) { options = { 'message': 'failed validation', 'range': [1, 3] }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 4); }); assert.deepEqual(validator.errors, []); @@ -63,7 +70,7 @@ test('when value is not in the range', function(assert) { test('when value is in the range', function(assert) { options = { 'message': 'failed validation', 'range': [1, 3] }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -72,7 +79,7 @@ test('when value is in the range', function(assert) { test('when options is an array', function(assert) { options = [1, 2, 3]; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is reserved']); @@ -81,7 +88,7 @@ test('when options is an array', function(assert) { test('when no message is passed', function(assert) { options = { in: [1, 2, 3] }; run(function() { - validator = Exclusion.create({model: model, property: 'attribute', options: options}); + validator = Exclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is reserved']); diff --git a/tests/unit/validators/local/format-test.js b/tests/unit/validators/local/format-test.js index f876bf27..f4590644 100644 --- a/tests/unit/validators/local/format-test.js +++ b/tests/unit/validators/local/format-test.js @@ -3,13 +3,20 @@ import { module, test } from 'qunit'; import Format from 'ember-validations/validators/local/format'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Format Validator', { - setup: function() { - Model = Ember.Object.extend(Mixin); + setup() { + Model = EmberObject.extend(Mixin); run(function() { model = Model.create(); }); @@ -19,7 +26,7 @@ module('Format Validator', { test('when matching format', function(assert) { options = { 'message': 'failed validation', 'with': /\d+/ }; run(function() { - validator = Format.create({model: model, property: 'attribute', options: options}); + validator = Format.create({ model, property: 'attribute', options }); set(model, 'attribute', '123'); }); assert.deepEqual(validator.errors, []); @@ -28,7 +35,7 @@ test('when matching format', function(assert) { test('when not matching format', function(assert) { options = { 'message': 'failed validation', 'with': /\d+/ }; run(function() { - validator = Format.create({model: model, property: 'attribute', options: options}); + validator = Format.create({ model, property: 'attribute', options }); set(model, 'attribute', 'abc'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -37,7 +44,7 @@ test('when not matching format', function(assert) { test('when allowing blank', function(assert) { options = { 'message': 'failed validation', 'with': /\d+/, 'allowBlank': true }; run(function() { - validator = Format.create({model: model, property: 'attribute', options: options}); + validator = Format.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, []); @@ -46,7 +53,7 @@ test('when allowing blank', function(assert) { test('when not allowing blank', function(assert) { options = { 'message': 'failed validation', 'with': /\d+/ }; run(function() { - validator = Format.create({model: model, property: 'attribute', options: options}); + validator = Format.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -55,7 +62,7 @@ test('when not allowing blank', function(assert) { test('when options is regexp', function(assert) { options = /\d+/; run(function() { - validator = Format.create({model: model, property: 'attribute', options: options}); + validator = Format.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is invalid']); @@ -64,7 +71,7 @@ test('when options is regexp', function(assert) { test('when no message is passed', function(assert) { options = { 'with': /\d+/ }; run(function() { - validator = Format.create({model: model, property: 'attribute', options: options}); + validator = Format.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is invalid']); diff --git a/tests/unit/validators/local/inclusion-test.js b/tests/unit/validators/local/inclusion-test.js index 8d7202d6..c9134767 100644 --- a/tests/unit/validators/local/inclusion-test.js +++ b/tests/unit/validators/local/inclusion-test.js @@ -3,13 +3,20 @@ import { module, test } from 'qunit'; import Inclusion from 'ember-validations/validators/local/inclusion'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Inclusion Validator', { - setup: function() { - Model = Ember.Object.extend(Mixin); + setup() { + Model = EmberObject.extend(Mixin); run(function() { model = Model.create(); }); @@ -19,7 +26,7 @@ module('Inclusion Validator', { test('when value is in the list', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3] }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, []); @@ -28,7 +35,7 @@ test('when value is in the list', function(assert) { test('when value is not in the list', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3] }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 4); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -37,7 +44,7 @@ test('when value is not in the list', function(assert) { test('when allowing blank', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3], allowBlank: true }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, []); @@ -46,7 +53,7 @@ test('when allowing blank', function(assert) { test('when not allowing blank', function(assert) { options = { 'message': 'failed validation', 'in': [1, 2, 3] }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -55,7 +62,7 @@ test('when not allowing blank', function(assert) { test('when value is in the range', function(assert) { options = { 'message': 'failed validation', 'range': [1, 3] }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, []); @@ -64,7 +71,7 @@ test('when value is in the range', function(assert) { test('when value is not in the range', function(assert) { options = { 'message': 'failed validation', 'range': [1, 3] }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', 4); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -73,7 +80,7 @@ test('when value is not in the range', function(assert) { test('when options is array', function(assert) { options = [1, 2, 3]; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is not included in the list']); @@ -82,7 +89,7 @@ test('when options is array', function(assert) { test('when no message is passed', function(assert) { options = { in: [1, 2, 3] }; run(function() { - validator = Inclusion.create({model: model, property: 'attribute', options: options}); + validator = Inclusion.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is not included in the list']); diff --git a/tests/unit/validators/local/length-test.js b/tests/unit/validators/local/length-test.js index 13e22fe5..7c099ba3 100644 --- a/tests/unit/validators/local/length-test.js +++ b/tests/unit/validators/local/length-test.js @@ -3,13 +3,20 @@ import { module, test } from 'qunit'; import Length from 'ember-validations/validators/local/length'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Length Validator', { - setup: function() { - Model = Ember.Object.extend(Mixin); + setup() { + Model = EmberObject.extend(Mixin); run(function() { model = Model.create(); }); @@ -19,7 +26,7 @@ module('Length Validator', { test('when allowed length is 3 and value length is 3', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '123'); }); assert.deepEqual(validator.errors, []); @@ -28,7 +35,7 @@ test('when allowed length is 3 and value length is 3', function(assert) { test('when allowed length is 3 and value length is 4', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '1234'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -37,7 +44,7 @@ test('when allowed length is 3 and value length is 4', function(assert) { test('when allowed length is 3 and value length is 2', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '12'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -46,7 +53,7 @@ test('when allowed length is 3 and value length is 2', function(assert) { test('when allowing blank and allowed length is 3', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3, allowBlank: true }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, []); @@ -55,7 +62,7 @@ test('when allowing blank and allowed length is 3', function(assert) { test('when allowing blank and minimum length is 3 and maximum length is 100', function(assert) { options = { messages: { tooShort: 'failed minimum validation', tooLong: 'failed maximum validation' }, minimum: 3, maximum: 100, allowBlank: true }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, []); @@ -64,16 +71,16 @@ test('when allowing blank and minimum length is 3 and maximum length is 100', fu test('when not allowing blank and allowed length is 3', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['failed validation']); }); test('when allowed length is 3 and a different tokenizer', function(assert) { - options = { messages: { wrongLength: 'failed validation' }, is: 3, tokenizer: function(value) { return value.split(' '); } }; + options = { messages: { wrongLength: 'failed validation' }, is: 3, tokenizer: (value) => value.split(' ') }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', 'one two three'); }); assert.deepEqual(validator.errors, []); @@ -82,7 +89,7 @@ test('when allowed length is 3 and a different tokenizer', function(assert) { test('when allowed length minimum is 3 and value length is 3', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '123'); }); assert.deepEqual(validator.errors, []); @@ -91,7 +98,7 @@ test('when allowed length minimum is 3 and value length is 3', function(assert) test('when allowed length minimum is 3 and value length is 2', function(assert) { options = { messages: { tooShort: 'failed validation' }, minimum: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '12'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -100,7 +107,7 @@ test('when allowed length minimum is 3 and value length is 2', function(assert) test('when allowed length maximum is 3 and value length is 3', function(assert) { options = { messages: { wrongLength: 'failed validation' }, is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '123'); }); assert.deepEqual(validator.errors, []); @@ -109,7 +116,7 @@ test('when allowed length maximum is 3 and value length is 3', function(assert) test('when allowed length maximum is 3 and value length is 4', function(assert) { options = { messages: { tooLong: 'failed validation' }, maximum: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '1234'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -118,7 +125,7 @@ test('when allowed length maximum is 3 and value length is 4', function(assert) test('when allowed length maximum is 3 and value is blank', function(assert) { options = { maximum: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, []); @@ -128,7 +135,7 @@ test('when options is a number', function(assert) { set(model, 'attribute', '1234'); options = 3; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is the wrong length (should be 3 characters)']); @@ -137,7 +144,7 @@ test('when options is a number', function(assert) { test('when options is a number and value is undefined', function(assert) { options = 3; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is the wrong length (should be 3 characters)']); @@ -146,7 +153,7 @@ test('when options is a number and value is undefined', function(assert) { test('when allowed length is 3, value length is 4 and no message is set', function(assert) { options = { is: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '1234'); }); assert.deepEqual(validator.errors, ['is the wrong length (should be 3 characters)']); @@ -155,7 +162,7 @@ test('when allowed length is 3, value length is 4 and no message is set', functi test('when allowed length minimum is 3, value length is 2 and no message is set', function(assert) { options = { minimum: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '12'); }); assert.deepEqual(validator.errors, ['is too short (minimum is 3 characters)']); @@ -164,7 +171,7 @@ test('when allowed length minimum is 3, value length is 2 and no message is set' test('when allowed length maximum is 3, value length is 4 and no message is set', function(assert) { options = { maximum: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '1234'); }); assert.deepEqual(validator.errors, ['is too long (maximum is 3 characters)']); @@ -173,7 +180,7 @@ test('when allowed length maximum is 3, value length is 4 and no message is set' test('when value is non-string, then the value is still checked', function(assert) { options = { maximum: 3 }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', 1234); }); assert.deepEqual(validator.errors, ['is too long (maximum is 3 characters)']); @@ -182,7 +189,7 @@ test('when value is non-string, then the value is still checked', function(asser test('when using a property instead of a number', function(assert) { options = { is: 'countProperty' }; run(function() { - validator = Length.create({model: model, property: 'attribute', options: options}); + validator = Length.create({ model, property: 'attribute', options }); set(model, 'attribute', '123'); }); assert.deepEqual(validator.errors, ['is the wrong length (should be 0 characters)']); diff --git a/tests/unit/validators/local/numericality-test.js b/tests/unit/validators/local/numericality-test.js index 1a73fcc7..03f8cc0f 100644 --- a/tests/unit/validators/local/numericality-test.js +++ b/tests/unit/validators/local/numericality-test.js @@ -3,13 +3,20 @@ import { module, test } from 'qunit'; import Numericality from 'ember-validations/validators/local/numericality'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Numericality Validator', { - setup: function() { - Model = Ember.Object.extend(Mixin); + setup() { + Model = EmberObject.extend(Mixin); run(function() { model = Model.create(); }); @@ -19,7 +26,7 @@ module('Numericality Validator', { test('when value is a number', function(assert) { options = { messages: { numericality: 'failed validation' } }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 123); }); assert.deepEqual(validator.errors, []); @@ -28,7 +35,7 @@ test('when value is a number', function(assert) { test('when value is a decimal number', function(assert) { options = { messages: { numericality: 'failed validation' } }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 123.456); }); assert.deepEqual(validator.errors, []); @@ -37,7 +44,7 @@ test('when value is a decimal number', function(assert) { test('when value is not a number', function(assert) { options = { messages: { numericality: 'failed validation' } }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 'abc123'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -46,7 +53,7 @@ test('when value is not a number', function(assert) { test('when no value', function(assert) { options = { messages: { numericality: 'failed validation' } }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -55,7 +62,7 @@ test('when no value', function(assert) { test('when no value and allowing blank', function(assert) { options = { messages: { numericality: 'failed validation' }, allowBlank: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, []); @@ -64,7 +71,7 @@ test('when no value and allowing blank', function(assert) { test('when bad value and allowing blank', function(assert) { options = { messages: { numericality: 'failed validation' }, allowBlank: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 'abc123'); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -73,7 +80,7 @@ test('when bad value and allowing blank', function(assert) { test('when only allowing integers and value is integer', function(assert) { options = { messages: { onlyInteger: 'failed validation', numericality: 'failed validation' }, onlyInteger: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 123); }); assert.deepEqual(validator.errors, []); @@ -82,7 +89,7 @@ test('when only allowing integers and value is integer', function(assert) { test('when only allowing integers and value is not integer', function(assert) { options = { messages: { onlyInteger: 'failed integer validation', numericality: 'failed validation' }, onlyInteger: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 123.456); }); assert.deepEqual(validator.errors, ['failed integer validation']); @@ -91,7 +98,7 @@ test('when only allowing integers and value is not integer', function(assert) { test('when only integer and no message is passed', function(assert) { options = { onlyInteger: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 1.1); }); assert.deepEqual(validator.errors, ['must be an integer']); @@ -100,7 +107,7 @@ test('when only integer and no message is passed', function(assert) { test('when only integer is passed directly', function(assert) { options = 'onlyInteger'; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 1.1); }); assert.deepEqual(validator.errors, ['must be an integer']); @@ -109,7 +116,7 @@ test('when only integer is passed directly', function(assert) { test('when only allowing values greater than 10 and value is greater than 10', function(assert) { options = { messages: { greaterThan: 'failed validation', numericality: 'failed validation' }, greaterThan: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, []); @@ -118,7 +125,7 @@ test('when only allowing values greater than 10 and value is greater than 10', f test('when only allowing values greater than 10 and value is 10', function(assert) { options = { messages: { greaterThan: 'failed validation', numericality: 'failed validation' }, greaterThan: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -127,7 +134,7 @@ test('when only allowing values greater than 10 and value is 10', function(asser test('when only allowing values greater than or assert.deepEqual to 10 and value is 10', function(assert) { options = { messages: { greaterThanOrEqualTo: 'failed validation', numericality: 'failed validation' }, greaterThanOrEqualTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, []); @@ -136,7 +143,7 @@ test('when only allowing values greater than or assert.deepEqual to 10 and value test('when only allowing values greater than or assert.deepEqual to 10 and value is 9', function(assert) { options = { messages: { greaterThanOrEqualTo: 'failed validation', numericality: 'failed validation' }, greaterThanOrEqualTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 9); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -145,7 +152,7 @@ test('when only allowing values greater than or assert.deepEqual to 10 and value test('when only allowing values less than 10 and value is less than 10', function(assert) { options = { messages: { lessThan: 'failed validation', numericality: 'failed validation' }, lessThan: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 9); }); assert.deepEqual(validator.errors, []); @@ -154,7 +161,7 @@ test('when only allowing values less than 10 and value is less than 10', functio test('when only allowing values less than 10 and value is 10', function(assert) { options = { messages: { lessThan: 'failed validation', numericality: 'failed validation' }, lessThan: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -163,7 +170,7 @@ test('when only allowing values less than 10 and value is 10', function(assert) test('when only allowing values less than or assert.deepEqual to 10 and value is 10', function(assert) { options = { messages: { lessThanOrEqualTo: 'failed validation', numericality: 'failed validation' }, lessThanOrEqualTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, []); @@ -172,16 +179,16 @@ test('when only allowing values less than or assert.deepEqual to 10 and value is test('when only allowing values less than or assert.deepEqual to 10 and value is 11', function(assert) { options = { messages: { lessThanOrEqualTo: 'failed validation', numericality: 'failed validation' }, lessThanOrEqualTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); - assert.deepEqual(validator.errors, ['failed validation']); + assert.deepEqual(validator.errors, ['failed validation']); }); }); test('when only allowing values equal to 10 and value is 10', function(assert) { options = { messages: { equalTo: 'failed validation', numericality: 'failed validation' }, equalTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, []); @@ -190,7 +197,7 @@ test('when only allowing values equal to 10 and value is 10', function(assert) { test('when only allowing values equal to 10 and value is 11', function(assert) { options = { messages: { equalTo: 'failed equal validation', numericality: 'failed validation' }, equalTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, ['failed equal validation']); @@ -199,7 +206,7 @@ test('when only allowing values equal to 10 and value is 11', function(assert) { test('when only allowing value equal to 0 and value is 1', function(assert) { options = { messages: { equalTo: 'failed equal validation', numericality: 'failed validation' }, equalTo: 0 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 1); }); assert.deepEqual(validator.errors, ['failed equal validation']); @@ -208,7 +215,7 @@ test('when only allowing value equal to 0 and value is 1', function(assert) { test('when only allowing odd values and the value is odd', function(assert) { options = { messages: { odd: 'failed validation', numericality: 'failed validation' }, odd: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, []); @@ -217,7 +224,7 @@ test('when only allowing odd values and the value is odd', function(assert) { test('when only allowing odd values and the value is even', function(assert) { options = { messages: { odd: 'failed validation', numericality: 'failed validation' }, odd: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -226,7 +233,7 @@ test('when only allowing odd values and the value is even', function(assert) { test('when only allowing even values and the value is even', function(assert) { options = { messages: { even: 'failed validation', numericality: 'failed validation' }, even: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, []); @@ -235,7 +242,7 @@ test('when only allowing even values and the value is even', function(assert) { test('when only allowing even values and the value is odd', function(assert) { options = { messages: { even: 'failed validation', numericality: 'failed validation' }, even: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -244,7 +251,7 @@ test('when only allowing even values and the value is odd', function(assert) { test('when value refers to another present property', function(assert) { options = { messages: { greaterThan: 'failed to be greater', numericality: 'failed validation' }, greaterThan: 'attribute_2' }; run(function() { - validator = Numericality.create({model: model, property: 'attribute_1', options: options}); + validator = Numericality.create({ model, property: 'attribute_1', options }); set(model, 'attribute_1', 0); set(model, 'attribute_2', 1); }); @@ -259,7 +266,7 @@ test('when value refers to another present property', function(assert) { test('when options is true', function(assert) { options = true; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['is not a number']); @@ -268,7 +275,7 @@ test('when options is true', function(assert) { test('when equal to and no message is passed', function(assert) { options = { equalTo: 11 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['must be equal to 11']); @@ -277,7 +284,7 @@ test('when equal to and no message is passed', function(assert) { test('when greater than and no message is passed', function(assert) { options = { greaterThan: 11 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['must be greater than 11']); @@ -286,7 +293,7 @@ test('when greater than and no message is passed', function(assert) { test('when greater than or equal to and no message is passed', function(assert) { options = { greaterThanOrEqualTo: 11 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['must be greater than or equal to 11']); @@ -295,7 +302,7 @@ test('when greater than or equal to and no message is passed', function(assert) test('when less than and no message is passed', function(assert) { options = { lessThan: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, ['must be less than 10']); @@ -304,7 +311,7 @@ test('when less than and no message is passed', function(assert) { test('when less than or equal to and no message is passed', function(assert) { options = { lessThanOrEqualTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, ['must be less than or equal to 10']); @@ -313,7 +320,7 @@ test('when less than or equal to and no message is passed', function(assert) { test('when odd and no message is passed', function(assert) { options = { odd: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 10); }); assert.deepEqual(validator.errors, ['must be odd']); @@ -322,7 +329,7 @@ test('when odd and no message is passed', function(assert) { test('when even and no message is passed', function(assert) { options = { even: true }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); assert.deepEqual(validator.errors, ['must be even']); @@ -331,7 +338,7 @@ test('when even and no message is passed', function(assert) { test('when other messages are passed but not a numericality message', function(assert) { options = { messages: { greaterThan: 'failed validation' } }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 'abc'); }); assert.deepEqual(validator.errors, ['is not a number']); @@ -340,16 +347,16 @@ test('when other messages are passed but not a numericality message', function(a test('when greaterThan fails and a greaterThan message is passed but not a numericality message', function(assert) { options = { greaterThan: 11, messages: { greaterThan: 'custom message' } }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); model.set('attribute', 10); }); assert.deepEqual(validator.errors, ['custom message']); }); test("numericality validators don't call addObserver on null props", function(assert) { - var stubbedObserverCalls = 0; + let stubbedObserverCalls = 0; - var realAddObserver = model.addObserver; + let realAddObserver = model.addObserver; model.addObserver = function(_, path) { stubbedObserverCalls += 1; if (!path) { @@ -360,10 +367,10 @@ test("numericality validators don't call addObserver on null props", function(as options = { lessThanOrEqualTo: 10 }; run(function() { - validator = Numericality.create({model: model, property: 'attribute', options: options}); + validator = Numericality.create({ model, property: 'attribute', options }); set(model, 'attribute', 11); }); model.addObserver = realAddObserver; - assert.equal(1, stubbedObserverCalls, "stubbed addObserver was called"); + assert.equal(1, stubbedObserverCalls, 'stubbed addObserver was called'); }); diff --git a/tests/unit/validators/local/presence-test.js b/tests/unit/validators/local/presence-test.js index 34fa2dc3..74b244ed 100644 --- a/tests/unit/validators/local/presence-test.js +++ b/tests/unit/validators/local/presence-test.js @@ -3,13 +3,20 @@ import { module, test } from 'qunit'; import Presence from 'ember-validations/validators/local/presence'; import Mixin from 'ember-validations/mixin'; -var model, Model, options, validator; -var set = Ember.set; -var run = Ember.run; +let model; +let Model; +let options; +let validator; + +const { + Object: EmberObject, + run, + set +} = Ember; module('Presence Validator', { - setup: function() { - Model = Ember.Object.extend(Mixin); + setup() { + Model = EmberObject.extend(Mixin); run(function() { model = Model.create(); }); @@ -19,7 +26,7 @@ module('Presence Validator', { test('when value is not empty', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Presence.create({model: model, property: 'attribute', options: options}); + validator = Presence.create({ model, property: 'attribute', options }); set(model, 'attribute', 'not empty'); }); assert.deepEqual(validator.errors, []); @@ -28,7 +35,7 @@ test('when value is not empty', function(assert) { test('when value is empty', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Presence.create({model: model, property: 'attribute', options: options}); + validator = Presence.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ['failed validation']); @@ -37,7 +44,7 @@ test('when value is empty', function(assert) { test('when options is true', function(assert) { options = true; run(function() { - validator = Presence.create({model: model, property: 'attribute', options: options}); + validator = Presence.create({ model, property: 'attribute', options }); set(model, 'attribute', ''); }); assert.deepEqual(validator.errors, ["can't be blank"]); @@ -46,7 +53,7 @@ test('when options is true', function(assert) { test('when value is blank', function(assert) { options = { message: 'failed validation' }; run(function() { - validator = Presence.create({model: model, property: 'attribute', options: options}); + validator = Presence.create({ model, property: 'attribute', options }); model.set('attribute', ' '); }); assert.deepEqual(validator.errors, ['failed validation']); diff --git a/tests/unit/validators/remote/uniqueness_test.js b/tests/unit/validators/remote/uniqueness_test.js deleted file mode 100644 index c591474a..00000000 --- a/tests/unit/validators/remote/uniqueness_test.js +++ /dev/null @@ -1,157 +0,0 @@ -if (false) {} // remove this line. It is only here to fix issue #232 -// module('Uniqueness options', { - // setup: function() { - // Ember.Validations.forms['new_user'] = { - // type: 'ActionView::Helpers::FormBuilder', - // input_tag: '
', - // label_tag: '
', - // validators: {'user[email]':{"uniqueness":[{"message": "must be unique", "scope":{'name':"pass"}}]},"presence":[{"message": "must be present"}]} - // } - - // $('#qunit-fixture') - // .append($('
', { - // action: '/users', - // 'data-validate': true, - // method: 'post', - // id: 'new_user' - // })) - // .find('form') - // .append($('', { - // name: 'user[name]', - // id: 'user_name', - // type: 'text' - // })) - // .append($('', { - // name: 'user[email]', - // id: 'user_email', - // type: 'text' - // })) - - // $('form#new_user').call(); - // } -// }); - -// test('when matching uniqueness on a non-nested resource', function() { - // var element = $(''); - // var options = { 'message': "failed validation" }; - // element.val('nottaken@test.com'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), undefined); -// }); - -// test('when matching uniqueness on a non-nested resource', function() { - // var element = $(''); - // var options = { 'message': "failed validation" }; - // element.val('taken@test.com'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), "failed validation"); -// }); - -// test('when matching uniqueness on a nested singular resource', function() { - // var element = $(''); - // var options = { 'message': "failed validation" }; - // element.val('nottaken@test.com'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), undefined); -// }); - -// test('when matching uniqueness on a nested singular resource', function() { - // var element = $(''); - // var options = { 'message': "failed validation" }; - // element.val('taken@test.com'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), "failed validation"); -// }); - -// test('when using scopes with no replacement', function() { - // var element = $(''); - // var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } }; - // element.val('test'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), "failed validation"); -// }); - -// test('when using scopes with replacement', function() { - // var element = $(''); - // var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } }; - // element.val('test') - // $('#qunit-fixture').append('').find('input[name="person[name]"]').val('other name'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), undefined); -// }); - -// test('when validating by scope and mixed focus order', function() { - // var unique_element = $('#user_email'), scope_element = $('#user_name'); - // unique_element.val('free@test.com'); - // unique_element.trigger('change'); - // unique_element.trigger('focusout'); - // equal($('.message[for="user_email"]').text(), ''); - - // scope_element.val('test name'); - // scope_element.trigger('change'); - // scope_element.trigger('focusout'); - // equal($('.message[for="user_email"]').text(), 'must be unique'); -// }); - -// test('when using scopes with replacement as checkboxes', function() { - // var element = $(''); - // var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } }; - // element.val('test') - // $('#qunit-fixture') - // .append('') - // .find('input[name="person[name]"]').val('other name'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), undefined); - // $('[name="person[name]"]:checkbox')[0].checked = true; - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), 'failed validation'); -// }); - -// test('when matching uniqueness on a resource with a defined class name', function() { - // var element = $(''); - // var options = { 'message': "failed validation", 'class': "active_record_test_module/user2" }; - // element.val('nottaken@test.com'); - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), 'failed validation'); -// }); - -// test('when allowing blank', function() { - // var element = $(''); - // var options = { 'message': "failed validation", 'with': /\d+/, 'allowBlank': true }; - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), undefined); -// }); - -// test('when not allowing blank', function() { - // var element = $(''); - // var options = { 'message': "failed validation", 'with': /\d+/ }; - // equal(Ember.Validations.validators.remote.uniqueness(model, property, options), "failed validation"); -// }); - -// test('when matching local uniqueness for nested has-many resources', function() { - // $('#qunit-fixture') - // .append($('', { - // action: '/users', - // 'data-validate': true, - // method: 'post', - // id: 'new_user_2' - // })) - // .find('form') - // .append($('', { - // name: 'profile[user_attributes][0][email]', - // id: 'user_0_email', - // })) - // .append($('', { - // name: 'profile[user_attributes][1][email]', - // id: 'user_1_email', - // })); - - // Ember.Validations.forms['new_user_2'] = { - // type: 'ActionView::Helpers::FormBuilder', - // input_tag: '
', - // label_tag: '
', - // validators: { 'user[email]':{"uniqueness":[{"message": "must be unique"}]}} - // } - // $('form#new_user_2').call(); - - // var user_0_email = $('#user_0_email'), - // user_1_email = $('#user_1_email'), - // options = { 'message': "must be unique" }; - - // user_0_email.val('not-locally-unique'); - // user_1_email.val('not-locally-unique'); - - // equal(Ember.Validations.validators.remote.uniqueness(user_1_email, options), undefined); - // equal(Ember.Validations.validators.local.uniqueness(user_1_email, options), "must be unique"); -// });