From 41b1af0aeeebfd56fac8af8e074aa3284d8b8b89 Mon Sep 17 00:00:00 2001 From: MinhHNguyen Date: Fri, 28 Jul 2017 22:43:50 -0700 Subject: [PATCH] Fix: Emit scale does not always pass events (#261) * Fix: fancy destructure no bueno * Update: async unit tests * Update: pr feedback --- src/lib/viewers/BaseViewer.js | 4 ++- src/lib/viewers/__tests__/BaseViewer-test.js | 34 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 7b4955688..bb64723f5 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -345,7 +345,9 @@ class BaseViewer extends EventEmitter { document.defaultView.addEventListener('resize', this.debouncedResizeHandler); this.addListener('load', (event) => { - ({ scale: this.scale = 1 } = event); + if (event && event.scale) { + this.scale = event.scale; + } if (this.annotationsPromise) { this.annotationsPromise.then(this.loadAnnotator); diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 1b131bb02..39ce8cc41 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -278,6 +278,40 @@ describe('lib/viewers/BaseViewer', () => { expect(document.defaultView.addEventListener).to.be.calledWith('resize', base.debouncedResizeHandler); expect(base.addListener).to.be.calledWith('load', sinon.match.func); }); + + it('should load the annotator when load is emitted with scale', (done) => { + sandbox.stub(fullscreen, 'addListener'); + sandbox.stub(document.defaultView, 'addEventListener'); + sandbox.stub(base, 'loadAnnotator'); + base.annotationsPromise = { + then: (arg) => { + expect(base.scale).to.equal(1.5); + expect(arg).to.equal(base.loadAnnotator); + done(); + } + }; + + base.addCommonListeners(); + expect(base.scale).to.equal(1); + base.emit('load', { + scale: 1.5 + }); + }); + + it('should load the annotator when load is emitted without an event', (done) => { + sandbox.stub(fullscreen, 'addListener'); + sandbox.stub(document.defaultView, 'addEventListener'); + sandbox.stub(base, 'loadAnnotator'); + base.annotationsPromise = { + then: (arg) => { + expect(arg).to.equal(base.loadAnnotator); + done(); + } + }; + + base.addCommonListeners(); + base.emit('load'); + }); }); describe('toggleFullscreen()', () => {