diff --git a/src/lib/annotations/Annotator.js b/src/lib/annotations/Annotator.js index 9491af690..7a35704ce 100644 --- a/src/lib/annotations/Annotator.js +++ b/src/lib/annotations/Annotator.js @@ -500,12 +500,13 @@ class Annotator extends EventEmitter { Object.keys(threadMap).forEach((threadID) => { const annotations = threadMap[threadID]; const firstAnnotation = annotations[0]; + if (!firstAnnotation || !this.isModeAnnotatable(firstAnnotation.type)) { + return; + } // Bind events on valid annotation thread const thread = this.createAnnotationThread(annotations, firstAnnotation.location, firstAnnotation.type); - if (thread) { - this.bindCustomListenersOnThread(thread); - } + this.bindCustomListenersOnThread(thread); }); this.emit('annotationsfetched'); @@ -603,6 +604,10 @@ class Annotator extends EventEmitter { * @return {void} */ bindCustomListenersOnThread(thread) { + if (!thread) { + return; + } + // Thread was deleted, remove from thread map thread.addListener('threaddeleted', () => { const page = thread.location.page || 1; diff --git a/src/lib/annotations/__tests__/Annotator-test.js b/src/lib/annotations/__tests__/Annotator-test.js index 809d0475a..56e1bf783 100644 --- a/src/lib/annotations/__tests__/Annotator-test.js +++ b/src/lib/annotations/__tests__/Annotator-test.js @@ -345,6 +345,7 @@ describe('lib/annotations/Annotator', () => { stubs.threadPromise = Promise.resolve(threadMap); stubs.serviceMock.expects('getThreadMap').returns(stubs.threadPromise); sandbox.stub(annotator, 'emit'); + sandbox.stub(annotator, 'isModeAnnotatable').returns(true); }); it('should reset and create a new thread map by fetching annotation data from the server', () => { @@ -357,7 +358,7 @@ describe('lib/annotations/Annotator', () => { return stubs.threadPromise.then(() => { expect(Object.keys(annotator.threads).length === 0).to.be.true; expect(annotator.createAnnotationThread).to.be.calledTwice; - expect(annotator.bindCustomListenersOnThread).to.be.calledOnce; + expect(annotator.bindCustomListenersOnThread).to.be.calledTwice; expect(result).to.be.an.object; }); }); @@ -459,6 +460,11 @@ describe('lib/annotations/Annotator', () => { stubs.threadMock.expects('addListener').withArgs('threadcleanup', sinon.match.func); annotator.bindCustomListenersOnThread(stubs.thread); }); + + it('should do nothing when given thread is empty', () => { + expect(annotator.bindCustomListenersOnThread).to.not.throw(undefined); + expect(annotator.bindCustomListenersOnThread).to.not.throw(null); + }) }); describe('unbindCustomListenersOnThread()', () => {