Skip to content

Commit

Permalink
Tests green like the "Rey de COPAS"!
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed May 14, 2015
1 parent d04cabb commit c5dac81
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
75 changes: 75 additions & 0 deletions app/mixins/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,79 @@
import Ember from 'ember';

export default Ember.Mixin.create({
validationErrors: {},
isValidNow: true,
validate: function() {
var store = this.get('store'),
errors = null;

// Clean all the current errors
this.get('errors').clear();
this.set('validationErrors',{});
this.set('isValidNow',true);
errors = this.get('validationErrors');

// Check for presence:
this._validatePresence();
// Check for valid emails:
this._validateEmail();
// Check relations:
this._validateRelations();
if (!this.get('isValidNow')) {
// It may be invalid because of its relations
if(Object.keys(errors).length !== 0){
this.transitionTo('updated.uncommitted');
store.recordWasInvalid(this, errors);
}
return false;
}else{
return true;
}
},
_validatePresence: function() {
var _this = this,
errors = this.get('validationErrors'),
validations = this.get('validations');
if(validations && validations.hasOwnProperty('presence')) {
validations.presence.forEach(function(property) {
if (Ember.isBlank(_this.get(property))){
if (!Ember.isArray(errors[property])) {errors[property] = [];}
_this.set('isValidNow',false);
errors[property].push(['This field is required']);
}
});
}
},
_validateEmail: function() {
var _this = this,
errors = this.get('validationErrors'),
validations = this.get('validations');
if(validations && validations.hasOwnProperty('email')) {
validations.email.forEach(function(property) {
if (_this.get(property) && _this.get(property).match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i) === null){
if (!Ember.isArray(errors[property])) {errors[property] = [];}
_this.set('isValidNow',false);
errors[property].push(['Enter a valid email address']);
}
});
}
},
_validateRelations: function() {
var _this = this,
validations = this.get('validations');
if(validations && validations.hasOwnProperty("relations")) {
if(validations.relations.hasOwnProperty("hasMany")) {
validations.relations.hasMany.forEach(function(relation) {
if(_this.get(relation)){
_this.get(relation).forEach(function(objRelation) {
if(!objRelation.validate()){
_this.set('isValidNow',false);
}
});
}
});
}
}
}

});
Empty file removed tests/dummy/app/models/.gitkeep
Empty file.
18 changes: 18 additions & 0 deletions tests/dummy/app/models/fake-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import DS from 'ember-data';
import Validator from '../mixins/validator';

export default DS.Model.extend(Validator,{
name: DS.attr('string'),
email: DS.attr('string'),

otherFakes: DS.hasMany('other-model'),

validations: {
presence: ['name','email'],
email: ['email'],
relations: {
hasMany:['otherFakes']
}
}

});
13 changes: 13 additions & 0 deletions tests/dummy/app/models/other-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import DS from 'ember-data';
import Validator from '../mixins/validator';

export default DS.Model.extend(Validator,{
name: DS.attr('string'),
email: DS.attr('string'),

validations: {
presence: ['name','email'],
email: ['email']
}

});
46 changes: 46 additions & 0 deletions tests/unit/mixins/validator-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* jshint expr:true */
import { expect } from 'chai';
import { describeModel } from 'ember-mocha';
import {
describe,
it
Expand All @@ -14,4 +15,49 @@ describe('ValidatorMixin', function() {
var subject = ValidatorObject.create();
expect(subject).to.be.ok;
});

describeModel('fake-model','Fake model with simple validations',
{
// Specify the other units that are required for this test.
needs: ['model:other-model']
},
function() {
// Replace this with your real tests.
it('exists', function() {
var model = this.subject();
// var store = this.store();
expect(model).to.be.ok;
});

it('validates the presence of the attributes set on `validations.presence`', function() {
var model = this.subject();
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('email').mapBy('message')[0][0]).to.equal('This field is required');
expect(model.get('errors').errorsFor('name').mapBy('message')[0][0]).to.equal('This field is required');
});

it('validates the email format of the attributes set on `validations.email`', function() {
var model = this.subject({email:'adsfasdf$'});
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('email').mapBy('message')[0][0]).to.equal('Enter a valid email address');
});

it('validates the relations specified on `validations.relations`', function() {
var model = this.subject({email:'thiisagoo@email.con',name:'Jose Rene Higuita'}),
store = model.get('store'),
otherFakes = null;

Ember.run(function() {
otherFakes = model.get('otherFakes');

otherFakes.pushObject(store.createRecord('other-model'));
expect(model.validate()).to.equal(false);
});

});

}
);


});

0 comments on commit c5dac81

Please sign in to comment.