From 24cea641287831a1ea32574e3b62ba858b89f0de Mon Sep 17 00:00:00 2001 From: Jefferson Stachelski Date: Wed, 17 Feb 2016 17:00:52 -0200 Subject: Fixed bug removing duplicated attachment Issue #549 --- web-ui/app/js/mail_view/ui/attachment_list.js | 14 ++++++------ web-ui/app/js/mail_view/ui/draft_box.js | 2 +- .../test/spec/mail_view/ui/attachment_list.spec.js | 26 ++++++++++++---------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/web-ui/app/js/mail_view/ui/attachment_list.js b/web-ui/app/js/mail_view/ui/attachment_list.js index 0483ae6a..ef2960f7 100644 --- a/web-ui/app/js/mail_view/ui/attachment_list.js +++ b/web-ui/app/js/mail_view/ui/attachment_list.js @@ -71,7 +71,7 @@ define( element.find('i.remove-icon').bind('click', function(event) { var element = $(this); var ident = element.closest('li').attr('data-ident'); - self.trigger(document, events.mail.removeAttachment, {ident: ident}); + self.trigger(document, events.mail.removeAttachment, {ident: ident, element: element}); event.preventDefault(); }); return element; @@ -181,22 +181,22 @@ define( this.select('inputFileUpload').click(); }; - this.removeAttachmentFromList = function(attachment) { + this.removeAttachmentFromList = function(ident) { for (var i = 0; i < this.attr.attachments.length; i++) { - if (this.attr.attachments[i].ident === attachment.ident) { + if (this.attr.attachments[i].ident === ident) { this.attr.attachments.remove(i); break; } } }; - this.destroyAttachmentElement = function(attachment) { - this.$node.find('li[data-ident=' + attachment.ident + ']').remove(); + this.destroyAttachmentElement = function(element) { + element.closest('li').remove(); }; this.removeAttachments = function(event, data) { - this.removeAttachmentFromList(data); - this.destroyAttachmentElement(data); + this.removeAttachmentFromList(data.ident); + this.destroyAttachmentElement(data.element); }; this.after('initialize', function () { diff --git a/web-ui/app/js/mail_view/ui/draft_box.js b/web-ui/app/js/mail_view/ui/draft_box.js index 07a7e472..afe31914 100644 --- a/web-ui/app/js/mail_view/ui/draft_box.js +++ b/web-ui/app/js/mail_view/ui/draft_box.js @@ -71,7 +71,7 @@ define( this.$node.find('i.remove-icon').bind('click', function(event) { var element = $(this); var ident = element.closest('li').attr('data-ident'); - self.trigger(document, events.mail.removeAttachment, {ident: ident}); + self.trigger(document, events.mail.removeAttachment, {ident: ident, element: element}); event.preventDefault(); }); diff --git a/web-ui/test/spec/mail_view/ui/attachment_list.spec.js b/web-ui/test/spec/mail_view/ui/attachment_list.spec.js index eab605d6..c0e2a5ef 100644 --- a/web-ui/test/spec/mail_view/ui/attachment_list.spec.js +++ b/web-ui/test/spec/mail_view/ui/attachment_list.spec.js @@ -123,47 +123,49 @@ describeMixin('mail_view/ui/attachment_list', function () { describe('Remove attachment', function () { it('should call the remove attachment method when triggered the removeAttachement event', function () { - var stubAttachment = {ident: 'whatever'}; + var stubAttachment = {ident: 'whatever', element: 'element'}; spyOn(this.component, 'removeAttachmentFromList'); spyOn(this.component, 'destroyAttachmentElement'); $(document).trigger(Pixelated.events.mail.removeAttachment, stubAttachment); - expect(this.component.removeAttachmentFromList).toHaveBeenCalledWith(stubAttachment); - expect(this.component.destroyAttachmentElement).toHaveBeenCalledWith(stubAttachment); + expect(this.component.removeAttachmentFromList).toHaveBeenCalledWith('whatever'); + expect(this.component.destroyAttachmentElement).toHaveBeenCalledWith('element'); }); it('should remove the attachment item from the DOM', function () { - var stubAttachment = {ident: 'whatever'}; + var stubAttachment = {ident: 'whatever', element: 'element'}; this.setupComponent('
' + - '' + + '' + '' + '
'); + var element = this.component.$node.find('i.remove-icon'); + expect(this.component.$node.find('li[data-ident=whatever]').length).toEqual(1); - this.component.destroyAttachmentElement(stubAttachment); + this.component.destroyAttachmentElement(element); expect(this.component.$node.find('li[data-ident=whatever]').length).toEqual(0); }); it('should remove attachment from attachment list', function () { - var stubAttachment = {ident: 'whatever'}; - this.component.attr.attachments = [stubAttachment, {ident: 'another attachment'}]; - this.component.removeAttachmentFromList(stubAttachment); + var stubAttachment = {ident: 'whatever', element: 'element'}; + this.component.attr.attachments = [{ident: 'whatever'}, {ident: 'another attachment'}]; + this.component.removeAttachmentFromList('whatever'); expect(this.component.attr.attachments).toEqual([{ident: 'another attachment'}]); }); it('when remove attachment that is not on the attachment list should not do anything', function () { - var stubAttachment = {ident: 'whatever'}; - this.component.attr.attachments = [stubAttachment]; + var stubAttachment = {ident: 'whatever', element: 'element'}; + this.component.attr.attachments = [{ident: 'whatever'}]; this.component.removeAttachmentFromList({ident: 'different attachment'}); - expect(this.component.attr.attachments).toEqual([stubAttachment]); + expect(this.component.attr.attachments).toEqual([{ident: 'whatever'}]); }); }); }); -- cgit v1.2.3