Skip to content

Commit

Permalink
tests to verify changes saved in hooks persists
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed Mar 1, 2016
1 parent fe94906 commit 4f0efb3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/integration/hooks/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
});
});

describe('preserves changes to instance', function() {
it('beforeValidate', function(){
var hookCalled = 0;

this.User.beforeValidate(function(user, options) {
user.mood = 'happy';
hookCalled++;
});

return this.User.create({mood: 'sad', username: 'leafninja'}).then(function(user) {
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('leafninja');
expect(hookCalled).to.equal(1);
});
});

it('afterValidate', function() {
var hookCalled = 0;

this.User.afterValidate(function(user, options) {
user.mood = 'neutral';
hookCalled++;
});

return this.User.create({mood: 'sad', username: 'fireninja'}).then(function(user) {
expect(user.mood).to.equal('neutral');
expect(user.username).to.equal('fireninja');
expect(hookCalled).to.equal(1);
});
});

it('beforeCreate', function(){
var hookCalled = 0;

this.User.beforeCreate(function(user, options) {
user.mood = 'happy';
hookCalled++;
});

return this.User.create({username: 'akira'}).then(function(user) {
expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(1);
});
});

});

});

});
31 changes: 31 additions & 0 deletions test/integration/hooks/updateAttributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
});
});

describe('preserves changes to instance', function() {
it('beforeValidate', function(){

this.User.beforeValidate(function(user, options) {
user.mood = 'happy';
});

return this.User.create({username: 'fireninja', mood: 'invalid'}).then(function(user) {
return user.updateAttributes({username: 'hero'});
}).then(function(user) {
expect(user.username).to.equal('hero');
expect(user.mood).to.equal('happy');
});
});

it('afterValidate', function() {

this.User.afterValidate(function(user, options) {
user.mood = 'sad';
});

return this.User.create({username: 'fireninja', mood: 'nuetral'}).then(function(user) {
return user.updateAttributes({username: 'spider'});
}).then(function(user) {
expect(user.username).to.equal('spider');
expect(user.mood).to.equal('sad');
});
});
});

});

});

0 comments on commit 4f0efb3

Please sign in to comment.