Skip to content

Commit

Permalink
chore: address PR comments, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan committed Apr 17, 2020
1 parent b382ed5 commit 7f35c61
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
4 changes: 0 additions & 4 deletions src/lib/AnnotationControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ export default class AnnotationControls {
*/
private handleFullscreenExit = (): void => this.handleFullscreenChange(false);

/**
* Returns the current active annotation mode
* @return {string} Current active annotation mode
*/
public getActiveMode = (): AnnotationMode => {
return this.currentActiveControl;
};
Expand Down
7 changes: 7 additions & 0 deletions src/lib/__tests__/AnnotationControls-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,11 @@ describe('lib/AnnotationControls', () => {
expect(stubs.updateRegionButton).to.be.called;
});
});

describe('getActiveMode()', () => {
it('should return the current active mode', () => {
annotationControls.currentActiveControl = 'region';
expect(annotationControls.getActiveMode()).to.equal('region');
});
});
});
11 changes: 5 additions & 6 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,15 +1101,14 @@ class BaseViewer extends EventEmitter {
this.emit('annotatorevent', data);
}

handleAnnotationCreateEvent({ annotation, meta: { status } = {} }) {
if (status !== 'pending') {
handleAnnotationCreateEvent({ annotation: { id } = {}, meta: { status } = {} }) {
// Only on success do we exit create annotation mode. If error occurs,
// we remain in create mode
if (status === 'success') {
const activeMode = this.annotationControls.getActiveMode();
this.annotator.toggleAnnotationMode(activeMode);
this.annotationControls.resetControls();
}

if (status === 'success') {
this.annotator.emit('annotations_active_set', annotation.id);
this.annotator.emit('annotations_active_set', id);
}
}

Expand Down
50 changes: 46 additions & 4 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,10 +1139,7 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.addListener).to.be.calledWith('toggleannotationmode', sinon.match.func);
expect(base.addListener).to.be.calledWith('scale', sinon.match.func);
expect(base.addListener).to.be.calledWith('scrolltoannotation', sinon.match.func);
expect(base.annotator.addListener).to.be.calledWith(
'annotationcreate',
base.annotationControls.resetControls,
);
expect(base.annotator.addListener).to.be.calledWith('annotations_create', base.handleAnnotationCreateEvent);
expect(base.annotator.addListener).to.be.calledWith('annotatorevent', sinon.match.func);
expect(base.emit).to.be.calledWith('annotator', base.annotator);
});
Expand Down Expand Up @@ -1495,4 +1492,49 @@ describe('lib/viewers/BaseViewer', () => {
expect(stubs.emit).not.to.be.called;
});
});

describe('handleAnnotationCreateEvent()', () => {
beforeEach(() => {
base.annotationControls = {
getActiveMode: sandbox.stub(),
resetControls: sandbox.stub(),
};

base.annotator = {
toggleAnnotationMode: sandbox.stub(),
emit: sandbox.stub(),
};
});

const createEvent = status => ({
annotation: { id: '123' },
meta: {
status,
},
});

['error', 'pending'].forEach(status => {
test(`should not do anything if status is ${status}`, () => {
const event = createEvent(status);
base.handleAnnotationCreateEvent(event);

expect(base.annotationControls.getActiveMode).not.to.be.called;
expect(base.annotator.toggleAnnotationMode).not.to.be.called;
expect(base.annotationControls.resetControls).not.to.be.called;
expect(base.annotator.emit).not.to.be.called;
});
});

test('should reset controls if status is success', () => {
base.annotationControls.getActiveMode.returns('region');

const event = createEvent('success');
base.handleAnnotationCreateEvent(event);

expect(base.annotationControls.getActiveMode).to.be.called;
expect(base.annotator.toggleAnnotationMode).to.be.calledWith('region');
expect(base.annotationControls.resetControls).to.be.called;
expect(base.annotator.emit).to.be.calledWith('annotations_active_set', '123');
});
});
});

0 comments on commit 7f35c61

Please sign in to comment.