95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
|
var HttpContext = require('../lib/http-context.js');
|
||
|
var Resource = require('../lib/resource.js');
|
||
|
|
||
|
describe('HttpContext', function(){
|
||
|
var ctx;
|
||
|
var resource;
|
||
|
|
||
|
function createRequest() {
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
function createResponse() {
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
beforeEach(function(){
|
||
|
resource = new Resource({path: '/foo'});
|
||
|
ctx = new HttpContext(resource, createRequest(), createResponse());
|
||
|
});
|
||
|
|
||
|
describe('.emit(ev, arg, done)', function(){
|
||
|
it('should emit events on a resource', function(done) {
|
||
|
var emitted, data;
|
||
|
|
||
|
resource.once('foo', function (arg, ctx, fn) {
|
||
|
emitted = true;
|
||
|
data = arg;
|
||
|
fn();
|
||
|
});
|
||
|
|
||
|
ctx.emit('foo', {bar: true}, function () {
|
||
|
assert(emitted, 'event should be emitted');
|
||
|
assert(data, 'arg should be supplied');
|
||
|
assert(data.bar, 'arg should be the correct object');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should handle multiple args', function(done) {
|
||
|
var emitted, data;
|
||
|
|
||
|
resource.once('foo', function (arg1, arg2, arg3, arg4, ctx, fn) {
|
||
|
emitted = true;
|
||
|
assert(arg1 === 1, 'arg1 should equal 1');
|
||
|
assert(arg2 === 2, 'arg2 should equal 2');
|
||
|
assert(arg3 === 3, 'arg3 should equal 3');
|
||
|
assert(arg4 === 4, 'arg4 should equal 4');
|
||
|
fn();
|
||
|
});
|
||
|
|
||
|
ctx.emit('foo', 1, 2, 3, 4, function (fn) {
|
||
|
assert(emitted, 'event should be emitted');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should have an after event', function(done) {
|
||
|
var emitted, emittedAfter;
|
||
|
|
||
|
ctx.done = done;
|
||
|
|
||
|
resource.once('foo', function (arg1, arg2, arg3, arg4, ctx, fn) {
|
||
|
emitted = true;
|
||
|
fn();
|
||
|
});
|
||
|
|
||
|
resource.once('after:foo', function (arg1, arg2, arg3, arg4, ctx, fn) {
|
||
|
emittedAfter = true;
|
||
|
fn();
|
||
|
});
|
||
|
|
||
|
ctx.emit('foo', 1, 2, 3, 4, function (fn) {
|
||
|
assert(emitted, 'event should be emitted');
|
||
|
fn();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should be able to emit synchronously', function(done) {
|
||
|
var emitted, data;
|
||
|
|
||
|
resource.once('foo', function (arg1, arg2, arg3, arg4, ctx) {
|
||
|
emitted = true;
|
||
|
assert(arg1 === 1, 'arg1 should equal 1');
|
||
|
assert(arg2 === 2, 'arg2 should equal 2');
|
||
|
assert(arg3 === 3, 'arg3 should equal 3');
|
||
|
assert(arg4 === 4, 'arg4 should equal 4');
|
||
|
});
|
||
|
|
||
|
ctx.emit('foo', 1, 2, 3, 4, function () {
|
||
|
assert(emitted);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|