Commit 94d622d9 authored by Randall Leeds's avatar Randall Leeds

Get guest tests passing

parent 4fffc02d
Promise = global.Promise ? require('es6-promise').Promise Promise = global.Promise ? require('es6-promise').Promise
Annotator = require('annotator') Annotator = require('annotator')
Guest = require('../guest') Guest = require('../guest')
anchoring = require('../anchoring/html')
assert = chai.assert assert = chai.assert
sinon.assert.expose(assert, prefix: '') sinon.assert.expose(assert, prefix: '')
waitForSync = (annotation) ->
if annotation.$anchors?
return Promise.resolve()
else
return new Promise(setTimeout).then(-> waitForSync(annotation))
describe 'Guest', -> describe 'Guest', ->
sandbox = null sandbox = null
fakeCrossFrame = null fakeCrossFrame = null
...@@ -127,14 +135,8 @@ describe 'Guest', -> ...@@ -127,14 +135,8 @@ describe 'Guest', ->
beforeEach -> beforeEach ->
guest = createGuest() guest = createGuest()
guest.plugins.Document = {uri: -> 'http://example.com'}
options = Annotator.Plugin.CrossFrame.lastCall.args[1] options = Annotator.Plugin.CrossFrame.lastCall.args[1]
it 'applies a "uri" property to the formatted object', ->
ann = {$$tag: 'tag1'}
formatted = options.formatter(ann)
assert.equal(formatted.uri, 'http://example.com/')
it 'keeps an existing uri property', -> it 'keeps an existing uri property', ->
ann = {$$tag: 'tag1', uri: 'http://example.com/foo'} ann = {$$tag: 'tag1', uri: 'http://example.com/foo'}
formatted = options.formatter(ann) formatted = options.formatter(ann)
...@@ -177,52 +179,60 @@ describe 'Guest', -> ...@@ -177,52 +179,60 @@ describe 'Guest', ->
describe 'on "focusAnnotations" event', -> describe 'on "focusAnnotations" event', ->
it 'focuses any annotations with a matching tag', -> it 'focuses any annotations with a matching tag', ->
highlight0 = {setFocused: sandbox.stub()} highlight0 = $('<span></span>')
highlight1 = {setFocused: sandbox.stub()} highlight1 = $('<span></span>')
guest = createGuest() guest = createGuest()
guest.anchored = [ guest.anchors = [
{annotation: {$$tag: 'tag1'}, highlight: highlight0} {annotation: {$$tag: 'tag1'}, highlights: highlight0.toArray()}
{annotation: {$$tag: 'tag2'}, highlight: highlight1} {annotation: {$$tag: 'tag2'}, highlights: highlight1.toArray()}
] ]
emitGuestEvent('focusAnnotations', 'ctx', ['tag1']) emitGuestEvent('focusAnnotations', 'ctx', ['tag1'])
assert.called(highlight0.setFocused) assert.isTrue(highlight0.hasClass('annotator-hl-focused'))
assert.calledWith(highlight0.setFocused, true)
it 'unfocuses any annotations without a matching tag', -> it 'unfocuses any annotations without a matching tag', ->
highlight0 = {setFocused: sandbox.stub()} highlight0 = $('<span class="annotator-hl-focused"></span>')
highlight1 = {setFocused: sandbox.stub()} highlight1 = $('<span class="annotator-hl-focused"></span>')
guest = createGuest() guest = createGuest()
guest.anchored = [ guest.anchors = [
{annotation: {$$tag: 'tag1'}, highlight: highlight0} {annotation: {$$tag: 'tag1'}, highlights: highlight0.toArray()}
{annotation: {$$tag: 'tag2'}, highlight: highlight1} {annotation: {$$tag: 'tag2'}, highlights: highlight1.toArray()}
] ]
emitGuestEvent('focusAnnotations', 'ctx', ['tag1']) emitGuestEvent('focusAnnotations', 'ctx', ['tag1'])
assert.called(highlight1.setFocused) assert.isFalse(highlight1.hasClass('annotator-hl-focused'))
assert.calledWith(highlight1.setFocused, false)
describe 'on "scrollToAnnotation" event', -> describe 'on "scrollToAnnotation" event', ->
beforeEach ->
$.fn.scrollintoview = sandbox.stub()
afterEach ->
delete $.fn.scrollintoview
it 'scrolls to the anchor with the matching tag', -> it 'scrolls to the anchor with the matching tag', ->
highlight = {scrollToView: sandbox.stub()} highlight = $('<span></span>')
guest = createGuest() guest = createGuest()
guest.anchored = [ guest.anchors = [
{annotation: {$$tag: 'tag1'}, highlight: highlight} {annotation: {$$tag: 'tag1'}, highlights: highlight.toArray()}
] ]
emitGuestEvent('scrollToAnnotation', 'ctx', 'tag1') emitGuestEvent('scrollToAnnotation', 'ctx', 'tag1')
assert.called(highlight.scrollToView) assert.calledOn($.fn.scrollintoview, sinon.match(highlight))
describe 'on "getDocumentInfo" event', -> describe 'on "getDocumentInfo" event', ->
guest = null guest = null
beforeEach -> beforeEach ->
sandbox.stub(document, 'title', 'hi')
guest = createGuest() guest = createGuest()
guest.plugins.PDF = guest.plugins.PDF =
uri: sandbox.stub().returns('http://example.com') uri: sandbox.stub().returns(window.location.href)
getMetaData: sandbox.stub() getMetadata: sandbox.stub()
afterEach ->
sandbox.restore()
it 'calls the callback with the href and pdf metadata', (done) -> it 'calls the callback with the href and pdf metadata', (done) ->
assertComplete = (payload) -> assertComplete = (payload) ->
try try
assert.equal(payload.uri, 'http://example.com/') assert.equal(payload.uri, document.location.href)
assert.equal(payload.metadata, metadata) assert.equal(payload.metadata, metadata)
done() done()
catch e catch e
...@@ -231,27 +241,27 @@ describe 'Guest', -> ...@@ -231,27 +241,27 @@ describe 'Guest', ->
ctx = {complete: assertComplete, delayReturn: sandbox.stub()} ctx = {complete: assertComplete, delayReturn: sandbox.stub()}
metadata = {title: 'hi'} metadata = {title: 'hi'}
promise = Promise.resolve(metadata) promise = Promise.resolve(metadata)
guest.plugins.PDF.getMetaData.returns(promise) guest.plugins.PDF.getMetadata.returns(promise)
emitGuestEvent('getDocumentInfo', ctx) emitGuestEvent('getDocumentInfo', ctx)
it 'calls the callback with the href and document metadata if pdf check fails', (done) -> it 'calls the callback with the href and basic metadata if pdf fails', (done) ->
assertComplete = (payload) -> assertComplete = (payload) ->
try try
assert.equal(payload.uri, 'http://example.com/') assert.equal(payload.uri, window.location.href)
assert.equal(payload.metadata, metadata) assert.deepEqual(payload.metadata, metadata)
done() done()
catch e catch e
done(e) done(e)
ctx = {complete: assertComplete, delayReturn: sandbox.stub()} ctx = {complete: assertComplete, delayReturn: sandbox.stub()}
metadata = {title: 'hi'} metadata = {title: 'hi', link: [{href: window.location.href}]}
guest.plugins.Document = {metadata: metadata}
promise = Promise.reject(new Error('Not a PDF document')) promise = Promise.reject(new Error('Not a PDF document'))
guest.plugins.PDF.getMetaData.returns(promise) guest.plugins.PDF.getMetadata.returns(promise)
emitGuestEvent('getDocumentInfo', ctx) emitGuestEvent('getDocumentInfo', ctx)
it 'notifies the channel that the return value is async', -> it 'notifies the channel that the return value is async', ->
delete guest.plugins.PDF delete guest.plugins.PDF
...@@ -272,42 +282,77 @@ describe 'Guest', -> ...@@ -272,42 +282,77 @@ describe 'Guest', ->
guest.onAdderMouseup(event) guest.onAdderMouseup(event)
assert.isTrue(event.isPropagationStopped()) assert.isTrue(event.isPropagationStopped())
describe 'annotation sync', -> describe 'createAnnotation()', ->
it 'calls sync for createAnnotation', -> it 'adds metadata to the annotation object', (done) ->
guest = createGuest()
guest.createAnnotation({})
assert.called(fakeCrossFrame.sync)
it 'calls sync for setupAnnotation', (done) ->
guest = createGuest() guest = createGuest()
guest.plugins.Document = {uri: -> 'http://example.com'} sinon.stub(guest, 'getDocumentInfo').returns(Promise.resolve({
guest.setupAnnotation({}) metadata: {title: 'hello'}
uri: 'http://example.com/'
}))
annotation = {}
guest.createAnnotation(annotation)
setTimeout -> setTimeout ->
assert.called(fakeCrossFrame.sync) assert.equal(annotation.uri, 'http://example.com/')
assert.deepEqual(annotation.document, {title: 'hello'})
done() done()
it 'treats an argument as the annotation object', ->
guest = createGuest()
annotation = {foo: 'bar'}
annotation = guest.createAnnotation(annotation)
assert.equal(annotation.foo, 'bar')
describe 'setupAnnotation()', -> describe 'setupAnnotation()', ->
el = null
range = null
beforeEach ->
el = document.createElement('span')
txt = document.createTextNode('hello')
el.appendChild(txt)
document.body.appendChild(el)
range = document.createRange()
range.selectNode(el)
afterEach ->
document.body.removeChild(el)
sandbox.restore()
it "doesn't declare annotation without targets as orphans", (done) -> it "doesn't declare annotation without targets as orphans", (done) ->
guest = createGuest() guest = createGuest()
annotation = target: [] annotation = target: []
guest.setupAnnotation(annotation) guest.setupAnnotation(annotation)
setTimeout -> waitForSync(annotation).then ->
assert.isFalse !!annotation.$orphan assert.isFalse(annotation.$orphan)
done() done()
it "doesn't declare annotations with a working target as orphans", (done) -> it "doesn't declare annotations with a working target as orphans", (done) ->
guest = createGuest() guest = createGuest()
annotation = target: ["test target"] annotation = target: [{selector: "test"}]
sandbox.stub(anchoring, 'anchor').returns(range)
guest.setupAnnotation(annotation) guest.setupAnnotation(annotation)
setTimeout -> waitForSync(annotation).then ->
assert.isFalse !!annotation.$orphan assert.isFalse(annotation.$orphan)
done() done()
it "declares annotations with broken targets as orphans", (done) -> it "declares annotations with broken targets as orphans", (done) ->
guest = createGuest() guest = createGuest()
sandbox.stub(guest, 'anchorTarget').returns(Promise.reject())
annotation = target: [{selector: 'broken selector'}] annotation = target: [{selector: 'broken selector'}]
sandbox.stub(anchoring, 'anchor').throws()
guest.setupAnnotation(annotation) guest.setupAnnotation(annotation)
setTimeout -> waitForSync(annotation).then ->
assert !!annotation.$orphan assert.isTrue(annotation.$orphan)
done()
it 'updates the cross frame and bucket bar plugins', (done) ->
guest = createGuest()
guest.plugins.CrossFrame =
sync: sinon.stub()
guest.plugins.BucketBar =
update: sinon.stub()
annotation = {}
guest.setupAnnotation(annotation)
waitForSync(annotation).then ->
assert.called(guest.plugins.BucketBar.update)
assert.called(guest.plugins.CrossFrame.sync)
done() done()
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