2015-01-19 12:39:31 +00:00
|
|
|
var ModelBuilder = require('../').ModelBuilder;
|
|
|
|
var should = require('./init');
|
|
|
|
|
|
|
|
describe('async observer', function() {
|
|
|
|
var TestModel;
|
|
|
|
beforeEach(function defineTestModel() {
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
TestModel = modelBuilder.define('TestModel', { name: String });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls registered async observers', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
TestModel.observe('before', pushAndNext(notifications, 'before'));
|
|
|
|
TestModel.observe('after', pushAndNext(notifications, 'after'));
|
|
|
|
|
|
|
|
TestModel.notifyObserversOf('before', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.push('call');
|
|
|
|
TestModel.notifyObserversOf('after', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
notifications.should.eql(['before', 'call', 'after']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows multiple observers for the same operation', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
TestModel.observe('event', pushAndNext(notifications, 'one'));
|
|
|
|
TestModel.observe('event', pushAndNext(notifications, 'two'));
|
|
|
|
|
|
|
|
TestModel.notifyObserversOf('event', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.should.eql(['one', 'two']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inherits observers from base model', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
TestModel.observe('event', pushAndNext(notifications, 'base'));
|
|
|
|
|
|
|
|
var Child = TestModel.extend('Child');
|
|
|
|
Child.observe('event', pushAndNext(notifications, 'child'));
|
|
|
|
|
|
|
|
Child.notifyObserversOf('event', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.should.eql(['base', 'child']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not modify observers in the base model', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
TestModel.observe('event', pushAndNext(notifications, 'base'));
|
|
|
|
|
|
|
|
var Child = TestModel.extend('Child');
|
|
|
|
Child.observe('event', pushAndNext(notifications, 'child'));
|
|
|
|
|
|
|
|
TestModel.notifyObserversOf('event', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.should.eql(['base']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('always calls inherited observers', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
TestModel.observe('event', pushAndNext(notifications, 'base'));
|
|
|
|
|
|
|
|
var Child = TestModel.extend('Child');
|
|
|
|
// Important: there are no observers on the Child model
|
|
|
|
|
|
|
|
Child.notifyObserversOf('event', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.should.eql(['base']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-11 11:08:35 +00:00
|
|
|
it('can remove observers', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
|
|
|
|
function call(ctx, next) {
|
|
|
|
notifications.push('call');
|
|
|
|
process.nextTick(next);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestModel.observe('event', call);
|
|
|
|
TestModel.removeObserver('event', call);
|
|
|
|
|
|
|
|
TestModel.notifyObserversOf('event', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.should.eql([]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can clear all observers', function(done) {
|
|
|
|
var notifications = [];
|
|
|
|
|
|
|
|
function call(ctx, next) {
|
|
|
|
notifications.push('call');
|
|
|
|
process.nextTick(next);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestModel.observe('event', call);
|
|
|
|
TestModel.observe('event', call);
|
|
|
|
TestModel.observe('event', call);
|
|
|
|
TestModel.clearObservers('event');
|
|
|
|
|
|
|
|
TestModel.notifyObserversOf('event', {}, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
notifications.should.eql([]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-19 12:39:31 +00:00
|
|
|
it('handles no observers', function(done) {
|
|
|
|
TestModel.notifyObserversOf('no-observers', {}, function(err) {
|
|
|
|
// the test passes when no error was raised
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes context to final callback', function(done) {
|
|
|
|
var context = {};
|
|
|
|
TestModel.notifyObserversOf('event', context, function(err, ctx) {
|
|
|
|
(ctx || "null").should.equal(context);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-17 16:31:42 +00:00
|
|
|
|
2015-05-20 22:02:44 +00:00
|
|
|
describe('notifyObserversAround', function() {
|
|
|
|
var notifications;
|
|
|
|
beforeEach(function() {
|
|
|
|
notifications = [];
|
|
|
|
TestModel.observe('before execute',
|
|
|
|
pushAndNext(notifications, 'before execute'));
|
|
|
|
TestModel.observe('after execute',
|
|
|
|
pushAndNext(notifications, 'after execute'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should notify before/after observers', function(done) {
|
|
|
|
var context = {};
|
|
|
|
|
|
|
|
function work(done) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
done(null, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TestModel.notifyObserversAround('execute', context, work,
|
|
|
|
function(err, result) {
|
|
|
|
notifications.should.eql(['before execute', 'after execute']);
|
|
|
|
result.should.eql(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow work with context', function(done) {
|
|
|
|
var context = {};
|
|
|
|
|
|
|
|
function work(context, done) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
done(null, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TestModel.notifyObserversAround('execute', context, work,
|
|
|
|
function(err, result) {
|
|
|
|
notifications.should.eql(['before execute', 'after execute']);
|
|
|
|
result.should.eql(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should notify before/after observers with multiple results',
|
|
|
|
function(done) {
|
|
|
|
var context = {};
|
|
|
|
|
|
|
|
function work(done) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
done(null, 1, 2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TestModel.notifyObserversAround('execute', context, work,
|
|
|
|
function(err, r1, r2) {
|
|
|
|
r1.should.eql(1);
|
|
|
|
r2.should.eql(2);
|
|
|
|
notifications.should.eql(['before execute', 'after execute']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-17 16:31:42 +00:00
|
|
|
it('resolves promises returned by observers', function(done) {
|
|
|
|
TestModel.observe('event', function(ctx) {
|
|
|
|
return Promise.resolve('value-to-ignore');
|
|
|
|
});
|
|
|
|
TestModel.notifyObserversOf('event', {}, function(err, ctx) {
|
|
|
|
// the test times out when the promises are not supported
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles rejected promise returned by an observer', function(done) {
|
|
|
|
var testError = new Error('expected test error');
|
|
|
|
TestModel.observe('event', function(ctx) {
|
|
|
|
return Promise.reject(testError);
|
|
|
|
});
|
|
|
|
TestModel.notifyObserversOf('event', {}, function(err, ctx) {
|
|
|
|
err.should.eql(testError);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-02-17 16:41:46 +00:00
|
|
|
|
|
|
|
it('returns a promise when no callback is provided', function() {
|
|
|
|
var context = { value: 'a-test-context' };
|
|
|
|
var p = TestModel.notifyObserversOf('event', context);
|
|
|
|
(p !== undefined).should.be.true;
|
|
|
|
return p.then(function(result) {
|
|
|
|
result.should.eql(context);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a rejected promise when no callback is provided', function() {
|
|
|
|
var testError = new Error('expected test error');
|
|
|
|
TestModel.observe('event', function(ctx, next) { next(testError); });
|
|
|
|
var p = TestModel.notifyObserversOf('event', context);
|
|
|
|
return p.then(
|
|
|
|
function(result) {
|
|
|
|
throw new Error('The promise should have been rejected.');
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
err.should.eql(testError);
|
|
|
|
});
|
|
|
|
});
|
2015-01-19 12:39:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function pushAndNext(array, value) {
|
|
|
|
return function(ctx, next) {
|
|
|
|
array.push(value);
|
|
|
|
process.nextTick(next);
|
|
|
|
};
|
|
|
|
}
|