-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from strongloop/feature/change-hook
Add change / delete events
- Loading branch information
Showing
3 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var should = require('./init.js'); | ||
|
||
describe('events', function() { | ||
beforeEach(function(done) { | ||
var test = this; | ||
this.db = getSchema(); | ||
this.TestModel = this.db.define('TestModel'); | ||
this.db.automigrate(function(err) { | ||
if(err) return done(err); | ||
test.TestModel.create(function(err, inst) { | ||
if(err) return done(err); | ||
test.inst = inst; | ||
done(); | ||
}); | ||
}); | ||
this.shouldEmitEvent = function(eventName, listener, done) { | ||
var timeout = setTimeout(function() { | ||
done(new Error('did not emit ' + eventName)); | ||
}, 100); | ||
this.TestModel.on(eventName, function() { | ||
clearTimeout(timeout); | ||
listener.apply(this, arguments); | ||
done(); | ||
}); | ||
} | ||
}); | ||
|
||
describe('changed', function() { | ||
it('should be emitted after save', function(done) { | ||
var model = new this.TestModel({name: 'foobar'}); | ||
this.shouldEmitEvent('changed', assertValidChangedArgs, done); | ||
model.save(); | ||
}); | ||
it('should be emitted after upsert', function(done) { | ||
this.shouldEmitEvent('changed', assertValidChangedArgs, done); | ||
this.TestModel.upsert({name: 'batbaz'}); | ||
}); | ||
it('should be emitted after create', function(done) { | ||
this.shouldEmitEvent('changed', assertValidChangedArgs, done); | ||
this.TestModel.create({name: '...'}); | ||
}); | ||
it('should be emitted after updateAttributes', function(done) { | ||
var test = this; | ||
this.TestModel.create({name: 'bazzy'}, function(err, model) { | ||
// prevent getting the changed event from "create" | ||
process.nextTick(function() { | ||
test.shouldEmitEvent('changed', assertValidChangedArgs, done); | ||
model.updateAttributes({name: 'foo'}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('deleted', function() { | ||
it('should be emitted after destroy', function(done) { | ||
this.shouldEmitEvent('deleted', assertValidDeletedArgs, done); | ||
this.inst.destroy(); | ||
}); | ||
it('should be emitted after deleteById', function(done) { | ||
this.shouldEmitEvent('deleted', assertValidDeletedArgs, done); | ||
this.TestModel.deleteById(this.inst.id); | ||
}); | ||
}); | ||
|
||
describe('deletedAll', function() { | ||
it('should be emitted after destroyAll', function(done) { | ||
this.shouldEmitEvent('deletedAll', function(where) { | ||
where.name.should.equal('foo'); | ||
}, done); | ||
this.TestModel.destroyAll({name: 'foo'}); | ||
}); | ||
}); | ||
}); | ||
|
||
function assertValidChangedArgs(obj) { | ||
obj.should.have.property('id'); | ||
} | ||
|
||
function assertValidDeletedArgs(id) { | ||
id.should.be.ok; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters