diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index e26e62a47..9be243d39 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -873,8 +873,11 @@ class BaseViewer extends EventEmitter { return false; } - const canViewAnnotations = !!(permissions.can_view_annotations_all || permissions.can_view_annotations_self); - return !permissions.can_annotate && !canViewAnnotations; + return !!( + permissions.can_annotate || + permissions.can_view_annotations_all || + permissions.can_view_annotations_self + ); } /** @@ -884,7 +887,8 @@ class BaseViewer extends EventEmitter { */ areAnnotationsEnabled() { // Do not attempt to fetch annotations if the user cannot create or view annotations - if (!this.hasAnnotationPermissions(this.options.file)) { + const { permissions } = this.options.file; + if (!this.hasAnnotationPermissions(permissions)) { return false; } diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 648bb53a8..775c1293e 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -87,6 +87,7 @@ describe('lib/viewers/BaseViewer', () => { base.isMobile = true; sandbox.stub(base, 'loadAnnotator'); sandbox.stub(base, 'finishLoadingSetup'); + sandbox.stub(base, 'areAnnotationsEnabled').returns(true); base.setup(); @@ -499,8 +500,8 @@ describe('lib/viewers/BaseViewer', () => { sinon.assert.failException; }) .catch((error) => { - expect(error).equals(sinon.match.error); - expect(error.message).equals('message'); + expect(error).to.be.an('error'); + expect(error.message).to.equal('message'); }); }); }); @@ -691,7 +692,7 @@ describe('lib/viewers/BaseViewer', () => { stubs.isIOS.returns(true); base.mobileZoomStartHandler(event); - expect(base._scaling).to.equal(true); + expect(base._scaling).to.be.true; expect(event.stopPropagation).to.be.called; expect(event.preventDefault).to.be.called; }); @@ -700,7 +701,7 @@ describe('lib/viewers/BaseViewer', () => { stubs.isIOS.returns(false); base.mobileZoomStartHandler(event); - expect(base._scaling).to.equal(true); + expect(base._scaling).to.be.true; expect(base._pinchScale).to.not.equal(undefined); expect(event.stopPropagation).to.be.called; expect(event.preventDefault).to.be.called; @@ -711,7 +712,7 @@ describe('lib/viewers/BaseViewer', () => { event.touches = [0]; base.mobileZoomStartHandler(event); - expect(base._scaling).to.equal(false); + expect(base._scaling).to.be.false; expect(base._pinchScale).to.equal(undefined); expect(event.stopPropagation).to.not.be.called; expect(event.preventDefault).to.not.be.called; @@ -783,7 +784,7 @@ describe('lib/viewers/BaseViewer', () => { base.mobileZoomEndHandler(event); expect(base.zoomIn).to.be.called; - expect(base._scaling).to.equal(false); + expect(base._scaling).to.be.false; expect(base._pincScale).to.equal(undefined); }); @@ -807,7 +808,7 @@ describe('lib/viewers/BaseViewer', () => { base.mobileZoomEndHandler(event); expect(base.zoomOut).to.be.called; expect(base.zoomIn).to.not.be.called; - expect(base._scaling).to.equal(false); + expect(base._scaling).to.be.false; expect(base._pincScale).to.equal(undefined); }); }); @@ -1071,28 +1072,29 @@ describe('lib/viewers/BaseViewer', () => { }; it('does nothing if file permissions are undefined', () => { - expect(base.hasAnnotationPermissions()).to.be.falsy; + expect(base.hasAnnotationPermissions()).to.be.false; }); it('should return false if the user can neither annotate nor view all or their own annotations', () => { - expect(base.hasAnnotationPermissions(permissions)).to.be.falsy; + expect(base.hasAnnotationPermissions(permissions)).to.be.false; }); it('should return true if the user can at least view all annotations', () => { permissions.can_view_annotations_all = true; - expect(base.hasAnnotationPermissions(permissions)).to.be.truthy; + expect(base.hasAnnotationPermissions(permissions)).to.be.true; }); it('should return true if the user can at least view their own annotations', () => { permissions.can_view_annotations_all = false; permissions.can_view_annotations_self = true; - expect(base.hasAnnotationPermissions(permissions)).to.be.truthy; + expect(base.hasAnnotationPermissions(permissions)).to.be.true; }); }); describe('areAnnotationsEnabled()', () => { beforeEach(() => { stubs.getViewerOption = sandbox.stub(base, 'getViewerOption').withArgs('annotations').returns(false); + stubs.hasPermissions = sandbox.stub(base, 'hasAnnotationPermissions').returns(true); base.options.file = { permissions: { can_annotate: true @@ -1100,26 +1102,32 @@ describe('lib/viewers/BaseViewer', () => { }; }); + it('should return false if the user cannot create/view annotations', () => { + stubs.hasPermissions.returns(false); + expect(base.areAnnotationsEnabled()).to.be.false; + }); + it('should return true if viewer option is set to true', () => { - expect(base.areAnnotationsEnabled()).to.equal(false); + expect(base.areAnnotationsEnabled()).to.be.false; + stubs.getViewerOption.returns(true); - expect(base.areAnnotationsEnabled()).to.equal(true); + expect(base.areAnnotationsEnabled()).to.be.true; }); it('should use the global showAnnotations boolean if the viewer param is not specified', () => { stubs.getViewerOption.withArgs('annotations').returns(null); base.options.showAnnotations = true; - expect(base.areAnnotationsEnabled()).to.equal(true); + expect(base.areAnnotationsEnabled()).to.be.true; base.options.showAnnotations = false; - expect(base.areAnnotationsEnabled()).to.equal(false); + expect(base.areAnnotationsEnabled()).to.be.false; }); it('should use BoxAnnotations options if an instance of BoxAnnotations is passed into Preview', () => { stubs.getViewerOption.withArgs('annotations').returns(null); base.options.showAnnotations = false; base.options.boxAnnotations = undefined; - expect(base.areAnnotationsEnabled()).to.equal(false); + expect(base.areAnnotationsEnabled()).to.be.false; base.options.viewer = { NAME: 'viewerName' }; base.options.boxAnnotations = sinon.createStubInstance(window.BoxAnnotations); @@ -1128,29 +1136,29 @@ describe('lib/viewers/BaseViewer', () => { // No enabled annotators in options boxAnnotations.options = { 'nope': 'wrong options type' }; boxAnnotations.viewerOptions = undefined; - expect(base.areAnnotationsEnabled()).to.equal(false); + expect(base.areAnnotationsEnabled()).to.be.false; // All default types enabled boxAnnotations.viewerOptions = { 'viewerName': { enabled: true } }; - expect(base.areAnnotationsEnabled()).to.equal(true); + expect(base.areAnnotationsEnabled()).to.be.true; // No specified enabled types boxAnnotations.viewerOptions = { 'viewerName': { enabledTypes: [] } }; - expect(base.areAnnotationsEnabled()).to.equal(false); + expect(base.areAnnotationsEnabled()).to.be.false; // Specified types enabled boxAnnotations.viewerOptions = { 'viewerName': { enabledTypes: [ 'point' ] } }; - expect(base.areAnnotationsEnabled()).to.equal(true); + expect(base.areAnnotationsEnabled()).to.be.true; // No passed in version of BoxAnnotations window.BoxAnnotations = undefined; - expect(base.areAnnotationsEnabled()).to.equal(false); + expect(base.areAnnotationsEnabled()).to.be.false; }); });