Skip to content

Commit

Permalink
Chore: Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Aug 3, 2017
1 parent 03b0fc2 commit 2468a37
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 78 deletions.
32 changes: 32 additions & 0 deletions src/lib/annotations/__tests__/Annotator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
23 changes: 11 additions & 12 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
109 changes: 43 additions & 66 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

Expand Down Expand Up @@ -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);
});

Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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;
});
});

Expand Down Expand Up @@ -869,46 +836,57 @@ 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,
file: {
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;
});
});

Expand Down Expand Up @@ -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
});
Expand Down

0 comments on commit 2468a37

Please sign in to comment.