Skip to content

Commit

Permalink
Fix: Replace {1} with user name on drawing save (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum authored Apr 23, 2018
1 parent 29cc0fd commit 408b495
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/doc/DocDrawingDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class DocDrawingDialog extends AnnotationDialog {
if (!this.element) {
this.setup([annotation]);
}
this.assignDrawingLabel(annotation);
}

/**
Expand Down Expand Up @@ -145,7 +146,7 @@ class DocDrawingDialog extends AnnotationDialog {

const firstAnnotation = util.getFirstAnnotation(annotations);
if (firstAnnotation) {
this.assignDrawingLabel(firstAnnotation);
this.addAnnotation(firstAnnotation);
}

this.element.appendChild(this.drawingDialogEl);
Expand Down Expand Up @@ -249,17 +250,16 @@ class DocDrawingDialog extends AnnotationDialog {
* the username does not exist.
*
* @private
* @param {Annotation} savedAnnotation The annotation data to populate the label with.
* @param {Annotation} annotation The annotation data to populate the label with.
* @return {void}
*/
assignDrawingLabel(savedAnnotation) {
if (!savedAnnotation || !this.drawingDialogEl) {
assignDrawingLabel(annotation) {
if (!annotation || !this.drawingDialogEl || annotation.user.id === '0') {
return;
}

const drawingLabelEl = this.drawingDialogEl.querySelector(`.${constants.CLASS_ANNOTATION_DRAWING_LABEL}`);
const username = savedAnnotation.user ? savedAnnotation.user.name : constants.USER_ANONYMOUS;
drawingLabelEl.textContent = util.replacePlaceholders(this.localized.whoDrew, [username]);
drawingLabelEl.textContent = util.replacePlaceholders(this.localized.whoDrew, [annotation.user.name]);

util.showElement(drawingLabelEl);
}
Expand Down
38 changes: 30 additions & 8 deletions src/doc/__tests__/DocDrawingDialog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ describe('doc/DocDrawingDialog', () => {
});
});

describe('addAnnotation()', () => {
it('should setup the element if not already done', () => {
dialog.element = undefined;
sandbox.stub(dialog, 'setup');
sandbox.stub(dialog, 'assignDrawingLabel');

dialog.addAnnotation({});
expect(dialog.setup).to.be.calledWith([{}]);
expect(dialog.assignDrawingLabel).to.be.calledWith({});

dialog.element = document.createElement('div');
dialog.addAnnotation({});
expect(dialog.setup).to.be.calledWith([{}]);
expect(dialog.assignDrawingLabel).to.be.calledWith({});
});
});

describe('postDrawing()', () => {
it('should emit annotation create to indicate that the save button was pressed', () => {
const event = {
Expand Down Expand Up @@ -134,7 +151,7 @@ describe('doc/DocDrawingDialog', () => {

sandbox.stub(dialog, 'generateDialogEl').returns(drawingDialogEl);
sandbox.stub(dialog, 'bindDOMListeners');
sandbox.stub(dialog, 'assignDrawingLabel');
sandbox.stub(dialog, 'addAnnotation');
});

it('should setup the dialog element without a commit button when given an annotation', () => {
Expand All @@ -148,7 +165,7 @@ describe('doc/DocDrawingDialog', () => {
dialog.setup([annotation]);
expect(dialog.generateDialogEl).to.be.called;
expect(dialog.bindDOMListeners).to.be.called;
expect(dialog.assignDrawingLabel).to.be.called;
expect(dialog.addAnnotation).to.be.called;
expect(dialog.element.contains(dialog.drawingDialogEl));
expect(dialog.commitButtonEl).to.be.null;
});
Expand All @@ -157,7 +174,7 @@ describe('doc/DocDrawingDialog', () => {
dialog.setup([]);
expect(dialog.generateDialogEl).to.be.called;
expect(dialog.bindDOMListeners).to.be.called;
expect(dialog.assignDrawingLabel).to.not.be.called;
expect(dialog.addAnnotation).to.not.be.called;
expect(dialog.element.contains(dialog.drawingDialogEl));
expect(dialog.commitButtonEl).to.not.be.undefined;
expect(dialog.element.contains(dialog.commitButtonEl));
Expand Down Expand Up @@ -276,19 +293,24 @@ describe('doc/DocDrawingDialog', () => {
sandbox.stub(util, 'replacePlaceholders').returns(notYaoMing);
sandbox.stub(util, 'showElement');

dialog.assignDrawingLabel('non empty');
dialog.assignDrawingLabel({ user: { id: '1' } });
expect(drawingLabelEl.textContent).to.equal(notYaoMing);
expect(dialog.drawingDialogEl.querySelector).to.be.called;
expect(util.replacePlaceholders).to.be.called;
expect(util.showElement).to.be.called;
});

it('should do nothing when given an invalid annotation or does not have a dialog', () => {
expect(dialog.assignDrawingLabel, 'not empty').to.not.throw();
sandbox.stub(util, 'showElement');

dialog.drawingDialogEl = document.createElement('div');
dialog.assignDrawingLabel(null);
dialog.assignDrawingLabel({ user: { id: '0' } });

dialog.drawingDialogEl = undefined;
dialog.assignDrawingLabel({ user: { id: '1' } });

dialog.drawingDialogEl = 'not empty';
expect(dialog.assignDrawingLabel, undefined).to.not.throw();
expect(dialog.assignDrawingLabel, null).to.not.throw();
expect(util.showElement).to.not.be.called;
});
});
});

0 comments on commit 408b495

Please sign in to comment.