Tidy up annotation-sync-test.js

Tidy the CoffeeScript-generated annotation-sync-test.js into something
more human readable.
parent a11fabc1
'use strict'; 'use strict';
var AnnotationSync; var EventEmitter = require('tiny-emitter');
var EventEmitter;
var slice = [].slice;
EventEmitter = require('tiny-emitter'); var AnnotationSync = require('../annotation-sync');
AnnotationSync = require('../annotation-sync');
describe('AnnotationSync', function() { describe('AnnotationSync', function() {
var createAnnotationSync; var createAnnotationSync;
var fakeBridge; var fakeBridge;
var options; var options;
var publish; var publish;
var sandbox; var sandbox = sinon.sandbox.create();
sandbox = sinon.sandbox.create();
publish = null;
fakeBridge = null;
createAnnotationSync = null;
options = null;
beforeEach(function() { beforeEach(function() {
var emitter; var emitter = new EventEmitter();
var listeners; var listeners = {};
listeners = {};
publish = function() {
var method = arguments[0];
var args = slice.call(arguments, 1);
return listeners[method].apply(listeners, args); createAnnotationSync = function() {
return new AnnotationSync(fakeBridge, options);
}; };
fakeBridge = { fakeBridge = {
on: sandbox.spy(function(method, fn) { on: sandbox.spy(function(method, fn) {
return listeners[method] = fn; listeners[method] = fn;
}), }),
call: sandbox.stub(), call: sandbox.stub(),
onConnect: sandbox.stub(), onConnect: sandbox.stub(),
links: [], links: [],
}; };
emitter = new EventEmitter();
options = { options = {
on: emitter.on.bind(emitter), on: emitter.on.bind(emitter),
emit: emitter.emit.bind(emitter), emit: emitter.emit.bind(emitter),
}; };
return createAnnotationSync = function() {
return new AnnotationSync(fakeBridge, options); publish = function() {
var method = arguments[0];
var args = [].slice.call(arguments, 1);
listeners[method].apply(listeners, args);
}; };
}); });
afterEach(function() { afterEach(function() {
return sandbox.restore(); sandbox.restore();
}); });
describe('channel event handlers', function() { describe('channel event handlers', function() {
var assertBroadcast; describe('the "deleteAnnotation" event', function() {
var assertReturnValue; it('broadcasts the "annotationDeleted" event over the local event bus', function() {
assertBroadcast = function(channelEvent, publishEvent) { var ann = {id: 1, $tag: 'tag1'};
return it('broadcasts the "' + publishEvent + '" event over the local event bus', function() { var eventStub = sinon.stub();
var ann; options.on('annotationDeleted', eventStub);
var eventStub;
ann = {
id: 1,
$tag: 'tag1',
};
createAnnotationSync(); createAnnotationSync();
eventStub = sinon.stub();
options.on(publishEvent, eventStub); publish('deleteAnnotation', {msg: ann}, function() {});
publish(channelEvent, {
msg: ann, assert.calledWith(eventStub, ann);
}, function() {});
return assert.calledWith(eventStub, ann);
}); });
};
assertReturnValue = function(channelEvent) { it('calls back with a formatted annotation', function(done) {
return it('calls back with a formatted annotation', function(done) { var ann = {id: 1, $tag: 'tag1'};
var ann; var callback = function(err, ret) {
var callback;
ann = {
id: 1,
$tag: 'tag1',
};
createAnnotationSync();
callback = function(err, ret) {
assert.isNull(err); assert.isNull(err);
assert.deepEqual(ret, { assert.deepEqual(ret, {tag: 'tag1', msg: ann});
tag: 'tag1', done();
msg: ann,
});
return done();
}; };
return publish(channelEvent, { createAnnotationSync();
msg: ann,
}, callback); publish('deleteAnnotation', {msg: ann}, callback);
}); });
};
describe('the "deleteAnnotation" event', function() {
assertBroadcast('deleteAnnotation', 'annotationDeleted');
assertReturnValue('deleteAnnotation');
it('removes an existing entry from the cache before the event is triggered', function() { it('removes an existing entry from the cache before the event is triggered', function() {
var ann; var ann = {id: 1, $tag: 'tag1'};
var annSync; var annSync = createAnnotationSync();
options.emit = function() {
return assert(!annSync.cache.tag1);
};
ann = {
id: 1,
$tag: 'tag1',
};
annSync = createAnnotationSync();
annSync.cache.tag1 = ann; annSync.cache.tag1 = ann;
return publish('deleteAnnotation', { options.emit = function() { assert(!annSync.cache.tag1); };
msg: ann,
}, function() {}); publish('deleteAnnotation', {msg: ann}, function() {});
}); });
return it('removes the annotation from the cache', function() {
var ann; it('removes the annotation from the cache', function() {
var annSync; var ann = {id: 1, $tag: 'tag1'};
ann = { var annSync = createAnnotationSync();
id: 1, annSync.cache.tag1 = ann;
$tag: 'tag1',
}; publish('deleteAnnotation', {msg: ann}, function() {});
annSync = createAnnotationSync();
publish('deleteAnnotation', { assert(!annSync.cache.tag1);
msg: ann,
}, function() {});
return assert(!annSync.cache.tag1);
}); });
}); });
return describe('the "loadAnnotations" event', function() {
return it('publishes the "annotationsLoaded" event', function() { describe('the "loadAnnotations" event', function() {
var ann; it('publishes the "annotationsLoaded" event', function() {
var annotations; var annotations = [
var bodies; {id: 1, $tag: 'tag1'},
var loadedStub; {id: 2, $tag: 'tag2'},
{id: 3, $tag: 'tag3'},
loadedStub = sinon.stub(); ];
var bodies = [
{msg: annotations[0], tag: annotations[0].$tag},
{msg: annotations[1], tag: annotations[1].$tag},
{msg: annotations[2], tag: annotations[2].$tag},
];
var loadedStub = sinon.stub();
options.on('annotationsLoaded', loadedStub); options.on('annotationsLoaded', loadedStub);
createAnnotationSync(); createAnnotationSync();
annotations = [
{
id: 1,
$tag: 'tag1',
}, {
id: 2,
$tag: 'tag2',
}, {
id: 3,
$tag: 'tag3',
},
];
bodies = (function() {
var i;
var len;
var results;
results = [];
for (i = 0, len = annotations.length; i < len; i++) {
ann = annotations[i];
results.push({
msg: ann,
tag: ann.$tag,
});
}
return results;
})();
publish('loadAnnotations', bodies, function() {}); publish('loadAnnotations', bodies, function() {});
return assert.calledWith(loadedStub, annotations);
assert.calledWith(loadedStub, annotations);
}); });
}); });
}); });
return describe('event handlers', function() {
return describe('the "beforeAnnotationCreated" event', function() { describe('event handlers', function() {
describe('the "beforeAnnotationCreated" event', function() {
it('proxies the event over the bridge', function() { it('proxies the event over the bridge', function() {
var ann; var ann = {id: 1};
ann = {
id: 1,
};
createAnnotationSync(); createAnnotationSync();
options.emit('beforeAnnotationCreated', ann); options.emit('beforeAnnotationCreated', ann);
assert.called(fakeBridge.call); assert.called(fakeBridge.call);
return assert.calledWith(fakeBridge.call, 'beforeCreateAnnotation', { assert.calledWith(
msg: ann, fakeBridge.call, 'beforeCreateAnnotation', {msg: ann, tag: ann.$tag},
tag: ann.$tag, sinon.match.func);
}, sinon.match.func);
}); });
return it('returns early if the annotation has a tag', function() {
var ann; it('returns early if the annotation has a tag', function() {
ann = { var ann = {id: 1, $tag: 'tag1'};
id: 1,
$tag: 'tag1',
};
createAnnotationSync(); createAnnotationSync();
options.emit('beforeAnnotationCreated', ann); options.emit('beforeAnnotationCreated', ann);
return assert.notCalled(fakeBridge.call);
assert.notCalled(fakeBridge.call);
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment