From 2468a37159f8f14b0044306e64519a287ffccc57 Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Thu, 3 Aug 2017 09:36:17 -0700 Subject: [PATCH] Chore: Unit tests --- .../annotations/__tests__/Annotator-test.js | 32 +++++ src/lib/viewers/BaseViewer.js | 23 ++-- src/lib/viewers/__tests__/BaseViewer-test.js | 109 +++++++----------- 3 files changed, 86 insertions(+), 78 deletions(-) diff --git a/src/lib/annotations/__tests__/Annotator-test.js b/src/lib/annotations/__tests__/Annotator-test.js index cef6b0d3d..34b473717 100644 --- a/src/lib/annotations/__tests__/Annotator-test.js +++ b/src/lib/annotations/__tests__/Annotator-test.js @@ -380,6 +380,22 @@ describe('lib/annotations/Annotator', () => { }); }); + describe('bindDOMListeners()', () => { + it('should add a listener for scaling the annotator', () => { + sandbox.stub(annotator, 'addListener'); + annotator.bindDOMListeners(); + expect(annotator.addListener).to.be.calledWith('scaleAnnotations', sinon.match.func); + }); + }); + + describe('unbindDOMListeners()', () => { + it('should add a listener for scaling the annotator', () => { + sandbox.stub(annotator, 'removeListener'); + annotator.unbindDOMListeners(); + expect(annotator.removeListener).to.be.calledWith('scaleAnnotations', sinon.match.func); + }); + }); + describe('bindCustomListenersOnService()', () => { it('should do nothing if the service does not exist', () => { annotator.annotationService = { @@ -713,6 +729,22 @@ describe('lib/annotations/Annotator', () => { }); }); + describe('scaleAnnotations()', () => { + it('should set scale and rotate annotations based on the annotated element', () => { + sandbox.stub(annotator, 'setScale'); + sandbox.stub(annotator, 'rotateAnnotations'); + + const data = { + scale: 5, + rotationAngle: 90, + pageNum: 2 + }; + annotator.scaleAnnotations(data); + expect(annotator.setScale).to.be.calledWith(data.scale); + expect(annotator.rotateAnnotations).to.be.calledWith(data.rotationAngle, data.pageNum); + }); + }); + describe('destroyPendingThreads()', () => { beforeEach(() => { stubs.thread = { diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index cd1f29f7c..0adc9ac8c 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -36,6 +36,16 @@ const ANNOTATION_TYPE_DRAW = 'draw'; const ANNOTATION_TYPE_POINT = 'point'; const LOAD_TIMEOUT_MS = 180000; // 3m const RESIZE_WAIT_TIME_IN_MILLIS = 300; +const ANNOTATION_BUTTONS = { + point: { + title: __('annotation_point_toggle'), + selector: SELECTOR_BOX_PREVIEW_BTN_ANNOTATE_POINT + }, + draw: { + title: __('annotation_draw_toggle'), + selector: SELECTOR_BOX_PREVIEW_BTN_ANNOTATE_DRAW + } +}; @autobind class BaseViewer extends EventEmitter { @@ -647,21 +657,10 @@ class BaseViewer extends EventEmitter { // Users can currently only view annotations on mobile this.canAnnotate = checkPermission(file, PERMISSION_ANNOTATE); if (this.canAnnotate) { - const annotationButtons = { - point: { - title: __('annotation_point_toggle'), - selector: SELECTOR_BOX_PREVIEW_BTN_ANNOTATE_POINT - }, - draw: { - title: __('annotation_draw_toggle'), - selector: SELECTOR_BOX_PREVIEW_BTN_ANNOTATE_DRAW - } - }; - // Show the annotate button for all enabled types for the // current viewer this.annotatorConf.TYPE.forEach((type) => { - this.showModeAnnotateButton(type, annotationButtons); + this.showModeAnnotateButton(type, ANNOTATION_BUTTONS); }); } this.initAnnotations(); diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 9fdb2537e..3de92d51d 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -111,6 +111,7 @@ describe('lib/viewers/BaseViewer', () => { base.finishLoadingSetup(); expect(container.innerHTML).to.equal('icon'); expect(container.classList.add).to.be.called; + base.options.container = null; }); }); @@ -709,8 +710,7 @@ describe('lib/viewers/BaseViewer', () => { }; stubs.isAnnotatable = sandbox.stub(base, 'isAnnotatable').returns(true); sandbox.stub(base, 'initAnnotations'); - sandbox.stub(base, 'showPointAnnotateButton', () => base.getAnnotationModeClickHandler('point')); - sandbox.stub(base, 'showDrawAnnotateButton', () => base.getAnnotationModeClickHandler('draw')); + sandbox.stub(base, 'showModeAnnotateButton'); stubs.checkPermission = sandbox.stub(file, 'checkPermission').returns(false); }); @@ -725,9 +725,7 @@ describe('lib/viewers/BaseViewer', () => { window.BoxAnnotations = BoxAnnotations; base.loadAnnotator(); expect(base.initAnnotations).to.be.called; - expect(base.showPointAnnotateButton).to.be.called; - // NOTE: Enable once drawing annotations are enabled - // expect(base.showDrawAnnotateButton).to.be.called; + expect(base.showModeAnnotateButton).to.be.called; }); it('should not display the point or draw annotation button if the user does not have the appropriate permissions', () => { @@ -738,9 +736,7 @@ describe('lib/viewers/BaseViewer', () => { base.loadAnnotator(); expect(base.initAnnotations).to.not.be.called; - expect(base.showPointAnnotateButton).to.not.be.called; - // NOTE: Enable once drawing annotations are enabled - // expect(base.showDrawAnnotateButton).to.not.be.called; + expect(base.showModeAnnotateButton).to.not.be.called; }); it('should not load an annotator if no loader was found', () => { @@ -750,8 +746,7 @@ describe('lib/viewers/BaseViewer', () => { window.BoxAnnotations = BoxAnnotations; base.loadAnnotator(); expect(base.initAnnotations).to.not.be.called; - expect(base.showPointAnnotateButton).to.not.be.called; - expect(base.showDrawAnnotateButton).to.not.be.called; + expect(base.showModeAnnotateButton).to.not.be.called; }); it('should not load an annotator if the viewer is not annotatable', () => { @@ -764,35 +759,7 @@ describe('lib/viewers/BaseViewer', () => { stubs.isAnnotatable.returns(false); base.loadAnnotator(); expect(base.initAnnotations).to.not.be.called; - expect(base.showPointAnnotateButton).to.not.be.called; - expect(base.showDrawAnnotateButton).to.not.be.called; - }); - }); - - describe('scaleAnnotations()', () => { - const scaleData = { - scale: 0.4321, - rotationAngle: 90, - pageNum: 2 - }; - beforeEach(() => { - base.annotator = { - setScale: sandbox.stub(), - rotateAnnotations: sandbox.stub() - }; - base.annotator.threads = { - 2: [{}] - }; - - base.scaleAnnotations(scaleData); - }); - - it('should invoke setScale() on annotator to scale annotations', () => { - expect(base.annotator.setScale).to.be.calledWith(scaleData.scale); - }); - - it('should invoke rotateAnnotations() on annotator to orient annotations', () => { - expect(base.annotator.rotateAnnotations).to.be.calledWith(scaleData.rotationAngle, scaleData.pageNum); + expect(base.showModeAnnotateButton).to.not.be.called; }); }); @@ -869,31 +836,42 @@ describe('lib/viewers/BaseViewer', () => { }); }); - describe('showPointAnnotateButton()', () => { - it('should set up and show point annotate button', () => { - const buttonEl = document.createElement('div'); - buttonEl.classList.add('bp-btn-annotate-point'); - buttonEl.classList.add(constants.CLASS_HIDDEN); - base.options = { - container: document, - file: { - id: 123 - } - }; - containerEl.appendChild(buttonEl); + describe('showModeAnnotateButton()', () => { + const modes = { + point: { + title: 'Point Annotation Mode', + selector: constants.SELECTOR_BOX_PREVIEW_BTN_ANNOTATE_POINT + }, + draw: { + title: 'Draw Annotation Mode', + selector: constants.SELECTOR_BOX_PREVIEW_BTN_ANNOTATE_DRAW + } + }; + + it('should do nothing if the mode does not require a button', () => { + sandbox.stub(base, 'getAnnotationModeClickHandler'); sandbox.stub(base, 'isAnnotatable').returns(true); - sandbox.mock(buttonEl).expects('addEventListener').withArgs('click', base.handler); + base.showModeAnnotateButton('highlight', modes); + expect(base.getAnnotationModeClickHandler).to.not.be.called; + }); - base.showPointAnnotateButton(base.handler); - expect(buttonEl.title).to.equal('Point annotation mode'); - expect(buttonEl.classList.contains(constants.CLASS_HIDDEN)).to.be.false; + it('should do nothing if the annotation type is not supported ', () => { + sandbox.stub(base, 'getAnnotationModeClickHandler'); + sandbox.stub(base, 'isAnnotatable').returns(false); + base.showModeAnnotateButton('bleh', modes); + expect(base.getAnnotationModeClickHandler).to.not.be.called; + }); + + it('should do nothing if the button is not in the container', () => { + sandbox.stub(base, 'isAnnotatable').returns(true); + sandbox.stub(base, 'getAnnotationModeClickHandler'); + base.showModeAnnotateButton('point', modes); + expect(base.getAnnotationModeClickHandler).to.not.be.called; }); - }); - describe('showDrawAnnotateButton()', () => { - it('should set up and show point annotate button', () => { + it('should set up and show an annotate button', () => { const buttonEl = document.createElement('div'); - buttonEl.classList.add('bp-btn-annotate-draw'); + buttonEl.classList.add('bp-btn-annotate-point'); buttonEl.classList.add(constants.CLASS_HIDDEN); base.options = { container: document, @@ -901,14 +879,14 @@ describe('lib/viewers/BaseViewer', () => { id: 123 } }; - containerEl.appendChild(buttonEl); sandbox.stub(base, 'isAnnotatable').returns(true); - sandbox.mock(buttonEl).expects('addEventListener').withArgs('click', base.handler); + sandbox.stub(base, 'getAnnotationModeClickHandler'); + sandbox.mock(buttonEl).expects('addEventListener').withArgs('click'); - base.showDrawAnnotateButton(base.handler); - expect(buttonEl.title).to.equal('Drawing annotation mode'); - expect(buttonEl.classList.contains(constants.CLASS_HIDDEN)).to.be.false; + base.showModeAnnotateButton('point', modes); + expect(buttonEl.title).to.equal('Point Annotation Mode'); + expect(base.getAnnotationModeClickHandler).to.be.called; }); }); @@ -986,13 +964,12 @@ describe('lib/viewers/BaseViewer', () => { }); it('should scale annotations on annotationsfetched', () => { - sandbox.stub(base, 'scaleAnnotations'); base.scale = 1; base.rotationAngle = 90; base.handleAnnotatorNotifications({ event: 'annotationsfetched' }); - expect(base.scaleAnnotations).to.be.calledWith({ + expect(base.emit).to.be.calledWith('scale', { scale: base.scale, rotationAngle: base.rotationAngle });