From cab0e0100b69c3e5ce15cd6d9a925fdb4c6aacc5 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Sun, 7 Oct 2018 15:44:30 -0400 Subject: [PATCH] 7841 Post Unlike sets status icon correctly closes #7882 fixes #7841 --- Changelog.md | 1 + .../app/models/post/interactions.js | 4 +++ .../app/models/post/interacations_spec.js | 36 +++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 655040f8a..0024688c6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -8,6 +8,7 @@ ## Bug fixes * Ignore invalid URLs for camo [#7922](https://github.com/diaspora/diaspora/pull/7922) +* Unlinking a post did not update the participation icon without a reload [#7882](https://github.com/diaspora/diaspora/pull/7882) ## Features * Add the ability to assign roles in the admin panel [#7868](https://github.com/diaspora/diaspora/pull/7868) diff --git a/app/assets/javascripts/app/models/post/interactions.js b/app/assets/javascripts/app/models/post/interactions.js index 5770b3291..2108393de 100644 --- a/app/assets/javascripts/app/models/post/interactions.js +++ b/app/assets/javascripts/app/models/post/interactions.js @@ -60,9 +60,13 @@ app.models.Post.Interactions = Backbone.Model.extend({ unlike : function() { var self = this; this.userLike().destroy({success : function() { + self.post.set({participation: false}); self.trigger('change'); self.set({"likes_count" : self.get("likes_count") - 1}); self.likes.trigger("change"); + }, + error: function(model, response) { + app.flashMessages.handleAjaxError(response); }}); }, diff --git a/spec/javascripts/app/models/post/interacations_spec.js b/spec/javascripts/app/models/post/interacations_spec.js index 019ca307a..599929eca 100644 --- a/spec/javascripts/app/models/post/interacations_spec.js +++ b/spec/javascripts/app/models/post/interacations_spec.js @@ -1,5 +1,6 @@ describe("app.models.Post.Interactions", function(){ var ajaxSuccess = {status: 200, responseText: "{\"id\": 1}"}; + var ajaxNoContent = {status: 204}; beforeEach(function(){ this.post = factory.post(); @@ -9,7 +10,7 @@ describe("app.models.Post.Interactions", function(){ spec.content().append($("
")); app.flashMessages = new app.views.FlashMessages({el: spec.content().find("#flash-container")}); - this.userLike = new app.models.Like({author : this.author}); + this.userLike = new app.models.Like({author: this.author, id: "id01"}); }); describe("toggleLike", function(){ @@ -62,12 +63,41 @@ describe("app.models.Post.Interactions", function(){ }); describe("unlike", function(){ - it("calls destroy on the likes collection", function(){ + beforeEach(function() { this.interactions.likes.add(this.userLike); - this.interactions.unlike(); + this.post.set({participation: true}); + spyOn(this.interactions, "userLike").and.returnValue(this.userLike); + }); + it("calls delete on the likes collection for the post", function() { + expect(this.interactions.likes.length).toEqual(1); + this.interactions.unlike(); expect(this.interactions.likes.length).toEqual(0); }); + + it("sets the participation flag for the post", function() { + expect(this.post.get("participation")).toBeTruthy(); + this.interactions.unlike(); + jasmine.Ajax.requests.mostRecent().respondWith(ajaxNoContent); + expect(this.post.get("participation")).toBeFalsy(); + }); + + it("triggers a change on the likes collection", function() { + spyOn(this.interactions.likes, "trigger"); + this.interactions.unlike(); + jasmine.Ajax.requests.mostRecent().respondWith(ajaxNoContent); + expect(this.interactions.likes.trigger).toHaveBeenCalledWith("change"); + }); + + it("displays a flash message on errors", function() { + spyOn(app.flashMessages, "handleAjaxError").and.callThrough(); + this.interactions.unlike(); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "error message"}); + + expect(app.flashMessages.handleAjaxError).toHaveBeenCalled(); + expect(app.flashMessages.handleAjaxError.calls.argsFor(0)[0].responseText).toBe("error message"); + expect(spec.content().find(".flash-message")).toBeErrorFlashMessage("error message"); + }); }); describe("reshare", function() {