From 5660d2f4a8e4593f76395bea71141b9e0d17434f Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Sun, 13 Nov 2016 21:15:00 +0100 Subject: [PATCH] Refactor comment creation in post interactions backbone model closes #7186 --- Changelog.md | 1 + .../app/models/post/interactions.js | 3 +-- .../app/models/post/interacations_spec.js | 21 +++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index 910d23cf7..3079dcf7c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -22,6 +22,7 @@ * Show more information of recipients on conversation creation [#7129](https://github.com/diaspora/diaspora/pull/7129) * Update notifications every 5 minutes and when opening the notification dropdown [#6952](https://github.com/diaspora/diaspora/pull/6952) * Show browser notifications when receiving new unread notifications [#6952](https://github.com/diaspora/diaspora/pull/6952) +* Only clear comment textarea when comment submission was successful [#7186](https://github.com/diaspora/diaspora/pull/7186) # 0.6.1.0 diff --git a/app/assets/javascripts/app/models/post/interactions.js b/app/assets/javascripts/app/models/post/interactions.js index a94ca6775..12d0a82d6 100644 --- a/app/assets/javascripts/app/models/post/interactions.js +++ b/app/assets/javascripts/app/models/post/interactions.js @@ -100,12 +100,11 @@ app.models.Post.Interactions = Backbone.Model.extend({ if (options.error) { options.error(); } }).done(function() { self.post.set({participation: true}); + self.set({"comments_count": self.get("comments_count") + 1}); self.trigger('change'); //updates after sync if (options.success) { options.success(); } }); - this.trigger("change"); //updates count in an eager manner - app.instrument("track", "Comment"); }, diff --git a/spec/javascripts/app/models/post/interacations_spec.js b/spec/javascripts/app/models/post/interacations_spec.js index f26ffe4f8..835504576 100644 --- a/spec/javascripts/app/models/post/interacations_spec.js +++ b/spec/javascripts/app/models/post/interacations_spec.js @@ -214,6 +214,13 @@ describe("app.models.Post.Interactions", function(){ expect(this.post.get("participation")).toBeTruthy(); }); + it("increases the comments count", function() { + var commentsCount = this.interactions.get("comments_count"); + this.interactions.comment("text"); + jasmine.Ajax.requests.mostRecent().respondWith(ajaxSuccess); + expect(this.interactions.get("comments_count")).toBe(commentsCount + 1); + }); + it("triggers a change on the model", function() { spyOn(this.interactions, "trigger"); this.interactions.comment("text"); @@ -237,6 +244,20 @@ describe("app.models.Post.Interactions", function(){ expect(this.post.get("participation")).toBeFalsy(); }); + it("doesn't increase the comments count", function() { + var commentsCount = this.interactions.get("comments_count"); + this.interactions.comment("text"); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400}); + expect(this.interactions.get("comments_count")).toBe(commentsCount); + }); + + it("doesn't trigger a change on the model", function() { + spyOn(this.interactions, "trigger"); + this.interactions.comment("text"); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400}); + expect(this.interactions.trigger).not.toHaveBeenCalledWith("change"); + }); + it("calls the error function if one is given", function() { var error = jasmine.createSpy(); this.interactions.comment("text", {error: error});