2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2014-01-29 19:03:04 +00:00
|
|
|
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) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (err) return done(err);
|
2014-01-29 19:03:04 +00:00
|
|
|
test.TestModel.create(function(err, inst) {
|
2016-04-01 11:48:17 +00:00
|
|
|
if (err) return done(err);
|
2014-01-29 19:03:04 +00:00
|
|
|
test.inst = inst;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.shouldEmitEvent = function(eventName, listener, done) {
|
|
|
|
this.TestModel.on(eventName, function() {
|
|
|
|
listener.apply(this, arguments);
|
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
};
|
2014-01-29 19:03:04 +00:00
|
|
|
});
|
2015-02-13 17:34:40 +00:00
|
|
|
|
2014-01-29 19:03:04 +00:00
|
|
|
describe('changed', function() {
|
|
|
|
it('should be emitted after save', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
var model = new this.TestModel({ name: 'foobar' });
|
2014-01-29 19:03:04 +00:00
|
|
|
this.shouldEmitEvent('changed', assertValidChangedArgs, done);
|
|
|
|
model.save();
|
|
|
|
});
|
|
|
|
it('should be emitted after upsert', function(done) {
|
|
|
|
this.shouldEmitEvent('changed', assertValidChangedArgs, done);
|
2016-04-01 11:48:17 +00:00
|
|
|
this.TestModel.upsert({ name: 'batbaz' });
|
2014-01-29 19:03:04 +00:00
|
|
|
});
|
|
|
|
it('should be emitted after create', function(done) {
|
|
|
|
this.shouldEmitEvent('changed', assertValidChangedArgs, done);
|
2016-04-01 11:48:17 +00:00
|
|
|
this.TestModel.create({ name: '...' });
|
2014-01-29 19:03:04 +00:00
|
|
|
});
|
|
|
|
it('should be emitted after updateAttributes', function(done) {
|
|
|
|
var test = this;
|
2016-04-01 11:48:17 +00:00
|
|
|
this.TestModel.create({ name: 'bazzy' }, function(err, model) {
|
2014-01-29 19:03:04 +00:00
|
|
|
// prevent getting the changed event from "create"
|
|
|
|
process.nextTick(function() {
|
|
|
|
test.shouldEmitEvent('changed', assertValidChangedArgs, done);
|
2016-04-01 11:48:17 +00:00
|
|
|
model.updateAttributes({ name: 'foo' });
|
2014-01-29 19:03:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-13 17:34:40 +00:00
|
|
|
|
2014-01-29 19:03:04 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2015-02-13 17:34:40 +00:00
|
|
|
|
2014-01-29 19:03:04 +00:00
|
|
|
describe('deletedAll', function() {
|
|
|
|
it('should be emitted after destroyAll', function(done) {
|
|
|
|
this.shouldEmitEvent('deletedAll', function(where) {
|
|
|
|
where.name.should.equal('foo');
|
|
|
|
}, done);
|
2016-04-01 11:48:17 +00:00
|
|
|
this.TestModel.destroyAll({ name: 'foo' });
|
2014-01-29 19:03:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function assertValidChangedArgs(obj) {
|
|
|
|
obj.should.have.property('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertValidDeletedArgs(id) {
|
|
|
|
id.should.be.ok;
|
2015-02-13 17:34:40 +00:00
|
|
|
}
|