Skip to content

Commit

Permalink
Chore: Add safety check for annotatorConfig (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum authored Dec 20, 2017
1 parent 9509619 commit c1f0bc0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ class BaseViewer extends EventEmitter {
}

if (this.annotationsLoadPromise) {
this.annotationsLoadPromise.then(this.annotationsLoadHandler).catch(() => {
this.annotationsLoadPromise.then(this.annotationsLoadHandler).catch((err) => {
/* eslint-disable no-console */
console.error('Annotation assets failed to load');
console.error('Annotation assets failed to load', err);
/* eslint-enable no-console */
});
}
Expand Down Expand Up @@ -763,7 +763,7 @@ class BaseViewer extends EventEmitter {
const { boxAnnotations, viewer } = this.options;
const annotatorConfig = boxAnnotations.options[viewer.NAME];
this.viewerConfig = {
enabled: annotatorConfig.enabled || !!annotatorConfig.enabledTypes
enabled: annotatorConfig && (annotatorConfig.enabled || annotatorConfig.enabledTypes.length > 0)
};
} else {
this.viewerConfig = this.getViewerAnnotationsConfig();
Expand Down
53 changes: 41 additions & 12 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,26 +326,36 @@ describe('lib/viewers/BaseViewer', () => {
describe('viewerLoadHandler()', () => {
beforeEach(() => {
base.annotationsLoadPromise = Promise.resolve();
sandbox.stub(base, 'annotationsLoadHandler');
stubs.annotationsLoadHandler = sandbox.stub(base, 'annotationsLoadHandler');
});

it('should set the scale if it exists', () => {
base.viewerLoadHandler({
scale: 1.5
});

base.viewerLoadHandler({ scale: 1.5 });
expect(base.scale).to.equal(1.5);
});

it('should handle the annotations load promise', () => {
base.viewerLoadHandler({
scale: 1.5
});

base.viewerLoadHandler({ scale: 1.5 });
return base.annotationsLoadPromise.then(() => {
expect(base.annotationsLoadHandler).to.be.called;
});
});

it('should handle the annotations load promise', () => {
stubs.annotationsLoadHandler.callsFake(() => {
throw new Error('message');
});

base.viewerLoadHandler({ scale: 1.5 });
return base.annotationsLoadPromise
.then(() => {
sinon.assert.failException;
})
.catch((error) => {
expect(error).equals(sinon.match.error);
expect(error.message).equals('message');
});
});
});

describe('toggleFullscreen()', () => {
Expand Down Expand Up @@ -886,7 +896,7 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.areAnnotationsEnabled()).to.equal(true);
});

it('should use the global show annotationsBoolean if the viewer param is not specified', () => {
it('should use the global show annotations boolean if the viewer param is not specified', () => {
stubs.getViewerOption.withArgs('annotations').returns(null);
base.options.showAnnotations = true;
expect(base.areAnnotationsEnabled()).to.equal(true);
Expand All @@ -895,19 +905,38 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.areAnnotationsEnabled()).to.equal(false);
});

it('should user BoxAnnotations options if an instance of BoxAnnotations is passed into Preview', () => {
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);

base.options.viewer = { NAME: 'viewerName' };
base.options.boxAnnotations = sinon.createStubInstance(window.BoxAnnotations);

// No enabled annotators in options
base.options.boxAnnotations.options = {};
expect(base.areAnnotationsEnabled()).to.equal(false);

// All default types enabled
base.options.boxAnnotations.options = {
'viewerName': { enabled: true }
}
};
expect(base.areAnnotationsEnabled()).to.equal(true);

// No specified enabled types
base.options.boxAnnotations.options = {
'viewerName': { enabledTypes: [] }
};
expect(base.areAnnotationsEnabled()).to.equal(false);

// Specified types enabled
base.options.boxAnnotations.options = {
'viewerName': { enabledTypes: [ 'point' ] }
};
expect(base.areAnnotationsEnabled()).to.equal(true);

// No passed in version of BoxAnnotations
window.BoxAnnotations = undefined;
expect(base.areAnnotationsEnabled()).to.equal(false);
});
Expand Down

0 comments on commit c1f0bc0

Please sign in to comment.