Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Emit scale does not always pass events #261

Merged
merged 5 commits into from
Jul 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is actually checking any expectations. I'd just do something like

this.annotationPromise = {};
base.emit('load', ....);
expect(base.loadAnnotor).to.be.called;

etc. for w/e is called and check that scale is set if it exists in event.scale

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expectation is that the annotations promise is called. done() is only resolved when the annotations promise is called. Unfortunately we cannot expect base.loadAnnotator to be called since events are async.

I've added a check to ensure that base.scale is set and that base.loadAnnotator is passed into the promise.then function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops you're right. Totally missed that! This works 👍

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()', () => {
Expand Down