Convert annotation-sync-test.coffee to JavaScript

`annotation-sync-test.js` is the unmodified output of
`coffee -bc src/annotator/test/annotation-sync-test.coffee`.
parent 967f89f7
EventEmitter = require('tiny-emitter')
AnnotationSync = require('../annotation-sync')
describe 'AnnotationSync', ->
sandbox = sinon.sandbox.create()
publish = null
fakeBridge = null
createAnnotationSync = null
createChannel = -> {call: sandbox.stub()}
options = null
PARENT_WINDOW = 'PARENT_WINDOW'
beforeEach ->
listeners = {}
publish = (method, args...) -> listeners[method](args...)
fakeWindow = parent: PARENT_WINDOW
fakeBridge =
on: sandbox.spy((method, fn) -> listeners[method] = fn)
call: sandbox.stub()
onConnect: sandbox.stub()
links: []
emitter = new EventEmitter();
options =
on: emitter.on.bind(emitter)
emit: emitter.emit.bind(emitter)
createAnnotationSync = ->
new AnnotationSync(fakeBridge, options)
afterEach: -> sandbox.restore()
describe 'channel event handlers', ->
assertBroadcast = (channelEvent, publishEvent) ->
it 'broadcasts the "' + publishEvent + '" event over the local event bus', ->
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
eventStub = sinon.stub()
options.on(publishEvent, eventStub)
publish(channelEvent, {msg: ann}, ->)
assert.calledWith(eventStub, ann)
assertReturnValue = (channelEvent) ->
it 'calls back with a formatted annotation', (done) ->
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
callback = (err, ret) ->
assert.isNull(err)
assert.deepEqual(ret, {tag: 'tag1', msg: ann})
done()
publish(channelEvent, {msg: ann}, callback)
assertCacheState = (channelEvent) ->
it 'removes an existing entry from the cache before the event is triggered', ->
options.emit = -> assert(!annSync.cache['tag1'])
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
annSync.cache['tag1'] = ann
publish(channelEvent, {msg: ann}, ->)
it 'ensures the annotation is inserted in the cache', ->
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
publish(channelEvent, {msg: ann}, ->)
assert.equal(annSync.cache['tag1'], ann)
describe 'the "deleteAnnotation" event', ->
assertBroadcast('deleteAnnotation', 'annotationDeleted')
assertReturnValue('deleteAnnotation')
it 'removes an existing entry from the cache before the event is triggered', ->
options.emit = -> assert(!annSync.cache['tag1'])
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
annSync.cache['tag1'] = ann
publish('deleteAnnotation', {msg: ann}, ->)
it 'removes the annotation from the cache', ->
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
publish('deleteAnnotation', {msg: ann}, ->)
assert(!annSync.cache['tag1'])
describe 'the "loadAnnotations" event', ->
it 'publishes the "annotationsLoaded" event', ->
loadedStub = sinon.stub()
options.on('annotationsLoaded', loadedStub)
annSync = createAnnotationSync()
annotations = [{id: 1, $tag: 'tag1'}, {id: 2, $tag: 'tag2'}, {id: 3, $tag: 'tag3'}]
bodies = ({msg: ann, tag: ann.$tag} for ann in annotations)
publish('loadAnnotations', bodies, ->)
assert.calledWith(loadedStub, annotations)
describe 'event handlers', ->
describe 'the "beforeAnnotationCreated" event', ->
it 'proxies the event over the bridge', ->
ann = {id: 1}
annSync = createAnnotationSync()
options.emit('beforeAnnotationCreated', ann)
assert.called(fakeBridge.call)
assert.calledWith(fakeBridge.call, 'beforeCreateAnnotation',
{msg: ann, tag: ann.$tag}, sinon.match.func)
it 'returns early if the annotation has a tag', ->
ann = {id: 1, $tag: 'tag1'}
annSync = createAnnotationSync()
options.emit('beforeAnnotationCreated', ann)
assert.notCalled(fakeBridge.call)
// Generated by CoffeeScript 1.10.0
var AnnotationSync, EventEmitter,
slice = [].slice;
EventEmitter = require('tiny-emitter');
AnnotationSync = require('../annotation-sync');
describe('AnnotationSync', function() {
var PARENT_WINDOW, createAnnotationSync, createChannel, fakeBridge, options, publish, sandbox;
sandbox = sinon.sandbox.create();
publish = null;
fakeBridge = null;
createAnnotationSync = null;
createChannel = function() {
return {
call: sandbox.stub()
};
};
options = null;
PARENT_WINDOW = 'PARENT_WINDOW';
beforeEach(function() {
var emitter, fakeWindow, listeners;
listeners = {};
publish = function() {
var args, method;
method = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return listeners[method].apply(listeners, args);
};
fakeWindow = {
parent: PARENT_WINDOW
};
fakeBridge = {
on: sandbox.spy(function(method, fn) {
return listeners[method] = fn;
}),
call: sandbox.stub(),
onConnect: sandbox.stub(),
links: []
};
emitter = new EventEmitter();
options = {
on: emitter.on.bind(emitter),
emit: emitter.emit.bind(emitter)
};
return createAnnotationSync = function() {
return new AnnotationSync(fakeBridge, options);
};
});
({
afterEach: function() {
return sandbox.restore();
}
});
describe('channel event handlers', function() {
var assertBroadcast, assertCacheState, assertReturnValue;
assertBroadcast = function(channelEvent, publishEvent) {
return it('broadcasts the "' + publishEvent + '" event over the local event bus', function() {
var ann, annSync, eventStub;
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
eventStub = sinon.stub();
options.on(publishEvent, eventStub);
publish(channelEvent, {
msg: ann
}, function() {});
return assert.calledWith(eventStub, ann);
});
};
assertReturnValue = function(channelEvent) {
return it('calls back with a formatted annotation', function(done) {
var ann, annSync, callback;
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
callback = function(err, ret) {
assert.isNull(err);
assert.deepEqual(ret, {
tag: 'tag1',
msg: ann
});
return done();
};
return publish(channelEvent, {
msg: ann
}, callback);
});
};
assertCacheState = function(channelEvent) {
it('removes an existing entry from the cache before the event is triggered', function() {
var ann, annSync;
options.emit = function() {
return assert(!annSync.cache['tag1']);
};
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
annSync.cache['tag1'] = ann;
return publish(channelEvent, {
msg: ann
}, function() {});
});
return it('ensures the annotation is inserted in the cache', function() {
var ann, annSync;
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
publish(channelEvent, {
msg: ann
}, function() {});
return assert.equal(annSync.cache['tag1'], ann);
});
};
describe('the "deleteAnnotation" event', function() {
assertBroadcast('deleteAnnotation', 'annotationDeleted');
assertReturnValue('deleteAnnotation');
it('removes an existing entry from the cache before the event is triggered', function() {
var ann, annSync;
options.emit = function() {
return assert(!annSync.cache['tag1']);
};
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
annSync.cache['tag1'] = ann;
return publish('deleteAnnotation', {
msg: ann
}, function() {});
});
return it('removes the annotation from the cache', function() {
var ann, annSync;
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
publish('deleteAnnotation', {
msg: ann
}, function() {});
return assert(!annSync.cache['tag1']);
});
});
return describe('the "loadAnnotations" event', function() {
return it('publishes the "annotationsLoaded" event', function() {
var ann, annSync, annotations, bodies, loadedStub;
loadedStub = sinon.stub();
options.on('annotationsLoaded', loadedStub);
annSync = createAnnotationSync();
annotations = [
{
id: 1,
$tag: 'tag1'
}, {
id: 2,
$tag: 'tag2'
}, {
id: 3,
$tag: 'tag3'
}
];
bodies = (function() {
var i, len, 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() {});
return assert.calledWith(loadedStub, annotations);
});
});
});
return describe('event handlers', function() {
return describe('the "beforeAnnotationCreated" event', function() {
it('proxies the event over the bridge', function() {
var ann, annSync;
ann = {
id: 1
};
annSync = createAnnotationSync();
options.emit('beforeAnnotationCreated', ann);
assert.called(fakeBridge.call);
return assert.calledWith(fakeBridge.call, 'beforeCreateAnnotation', {
msg: ann,
tag: ann.$tag
}, sinon.match.func);
});
return it('returns early if the annotation has a tag', function() {
var ann, annSync;
ann = {
id: 1,
$tag: 'tag1'
};
annSync = createAnnotationSync();
options.emit('beforeAnnotationCreated', ann);
return 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