From d0e70ccd6a94a7f4402d4b8d54041008ad00919b Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Sun, 27 Nov 2016 17:14:17 +0100 Subject: [PATCH] Use new ajax error handling for like, unlike and comment failures closes #7227 --- Changelog.md | 1 + .../javascripts/mobile/mobile_comments.js | 5 +- .../javascripts/mobile/mobile_post_actions.js | 6 ++ .../mobile/mobile_comments_spec.js | 70 ++++++++++++++----- .../mobile/mobile_post_actions_spec.js | 16 +++++ 5 files changed, 78 insertions(+), 20 deletions(-) diff --git a/Changelog.md b/Changelog.md index 2c4896ef7..47c7e1646 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ * Increase the spacing above and below post contents [#7267](https://github.com/diaspora/diaspora/pull/7267) * Replace fileuploader-custom with FineUploader [#7083](https://github.com/diaspora/diaspora/pull/7083) * Always show mobile reaction counts [#7207](https://github.com/diaspora/diaspora/pull/7207) +* Refactor mobile alerts for error responses [#7227](https://github.com/diaspora/diaspora/pull/7227) ## Bug fixes * Fix background color of year on notifications page with dark theme [#7263](https://github.com/diaspora/diaspora/pull/7263) diff --git a/app/assets/javascripts/mobile/mobile_comments.js b/app/assets/javascripts/mobile/mobile_comments.js index 55fdfb44d..0861ec0f2 100644 --- a/app/assets/javascripts/mobile/mobile_comments.js +++ b/app/assets/javascripts/mobile/mobile_comments.js @@ -50,8 +50,9 @@ $.post(form.attr("action") + "?format=mobile", form.serialize(), function(data){ Diaspora.Mobile.Comments.updateStream(form, data); - }, "html").fail(function(){ - Diaspora.Mobile.Comments.resetCommentBox(this); + }, "html").fail(function(response) { + Diaspora.Mobile.Alert.handleAjaxError(response); + Diaspora.Mobile.Comments.resetCommentBox(form); }); autosize($(".add-comment-switcher:not(.hidden) textarea")); diff --git a/app/assets/javascripts/mobile/mobile_post_actions.js b/app/assets/javascripts/mobile/mobile_post_actions.js index f7064baef..7df54d6a9 100644 --- a/app/assets/javascripts/mobile/mobile_post_actions.js +++ b/app/assets/javascripts/mobile/mobile_post_actions.js @@ -35,6 +35,9 @@ Diaspora.Mobile.PostActions.showLoader(link); }, success: onSuccess, + error: function(response) { + Diaspora.Mobile.Alert.handleAjaxError(response); + }, complete: function() { Diaspora.Mobile.PostActions.hideLoader(link); } @@ -61,6 +64,9 @@ Diaspora.Mobile.PostActions.showLoader(link); }, success: onSuccess, + error: function(response) { + Diaspora.Mobile.Alert.handleAjaxError(response); + }, complete: function() { Diaspora.Mobile.PostActions.hideLoader(link); } diff --git a/spec/javascripts/mobile/mobile_comments_spec.js b/spec/javascripts/mobile/mobile_comments_spec.js index d2405a4e6..602db13f6 100644 --- a/spec/javascripts/mobile/mobile_comments_spec.js +++ b/spec/javascripts/mobile/mobile_comments_spec.js @@ -1,8 +1,22 @@ describe("Diaspora.Mobile.Comments", function(){ + beforeEach(function() { + spec.loadFixture("aspects_index_mobile_post_with_comments"); + this.bottomBar = $(".bottom-bar").first(); + this.link = $(".stream .show-comments").first(); + }); + + describe("initialize", function() { + it("calls submitComment when the comment form has been submitted", function() { + spyOn(Diaspora.Mobile.Comments, "submitComment"); + Diaspora.Mobile.Comments.initialize(); + Diaspora.Mobile.Comments.showCommentBox($(".stream .comment-action").first()); + $(".stream .new_comment").first().submit(); + expect(Diaspora.Mobile.Comments.submitComment).toHaveBeenCalled(); + }); + }); + describe("toggleComments", function() { beforeEach(function() { - spec.loadFixture("aspects_index_mobile_post_with_comments"); - this.link = $(".stream .show-comments").first(); spyOn(Diaspora.Mobile.Comments, "showComments"); spyOn(Diaspora.Mobile.Comments, "hideComments"); }); @@ -30,9 +44,6 @@ describe("Diaspora.Mobile.Comments", function(){ describe("showUnloadedComments", function() { beforeEach(function() { - spec.loadFixture("aspects_index_mobile_post_with_comments"); - this.link = $(".stream .show-comments").first(); - this.bottomBar = this.link.closest(".bottom-bar").first(); this.commentActionLink = this.bottomBar.find("a.comment-action"); }); @@ -79,26 +90,51 @@ describe("Diaspora.Mobile.Comments", function(){ }); }); - describe("createComment", function () { + describe("submitComment", function() { beforeEach(function() { - spec.loadFixture("aspects_index_mobile_post_with_comments"); - var link = $(".stream .comment-action").first(); - Diaspora.Mobile.Comments.showCommentBox(link); - $(".stream .new_comment").submit(Diaspora.Mobile.Comments.submitComment); + Diaspora.Mobile.Comments.initialize(); + Diaspora.Mobile.Comments.showCommentBox($(".stream .comment-action").first()); }); it("doesn't submit an empty comment", function() { - var form = $(".stream .new_comment").first(); - spyOn(jQuery, "ajax"); - form.submit(); - expect(jQuery.ajax).not.toHaveBeenCalled(); + $(".stream .new_comment").first().submit(); + expect(jasmine.Ajax.requests.count()).toBe(0); + }); + + it("submits comments with text", function() { + $(".stream .new_comment textarea").val("comment text"); + $(".stream .new_comment").first().submit(); + expect(jasmine.Ajax.requests.mostRecent().data().text).toEqual(["comment text"]); + }); + + it("calls updateStream on success", function() { + spyOn(Diaspora.Mobile.Comments, "updateStream"); + $(".stream .new_comment textarea").val("comment text"); + $(".stream .new_comment").first().submit(); + jasmine.Ajax.requests.mostRecent().respondWith({status: 200, responseText: "foo"}); + expect(Diaspora.Mobile.Comments.updateStream).toHaveBeenCalledWith($(".stream .new_comment").first(), "foo"); + }); + + it("lets Diaspora.Mobile.Alert handle AJAX errors", function() { + spyOn(Diaspora.Mobile.Alert, "handleAjaxError"); + $(".stream .new_comment textarea").val("comment text"); + $(".stream .new_comment").first().submit(); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "oh noez! comment failed!"}); + expect(Diaspora.Mobile.Alert.handleAjaxError).toHaveBeenCalled(); + expect(Diaspora.Mobile.Alert.handleAjaxError.calls.argsFor(0)[0].responseText).toBe("oh noez! comment failed!"); + }); + + it("calls resetCommentBox on errors", function() { + spyOn(Diaspora.Mobile.Comments, "resetCommentBox"); + $(".stream .new_comment textarea").val("comment text"); + $(".stream .new_comment").first().submit(); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "oh noez! comment failed!"}); + expect(Diaspora.Mobile.Comments.resetCommentBox).toHaveBeenCalledWith($(".stream .new_comment").first()); }); }); describe("increaseReactionCount", function(){ beforeEach(function() { - spec.loadFixture("aspects_index_mobile_post_with_comments"); - this.bottomBar = $(".bottom-bar").first(); this.toggleReactionsLink = this.bottomBar.find(".show-comments").first(); }); @@ -123,8 +159,6 @@ describe("Diaspora.Mobile.Comments", function(){ describe("bottomBarLazy", function(){ beforeEach(function() { - spec.loadFixture("aspects_index_mobile_post_with_comments"); - this.bottomBar = $(".bottom-bar").first(); this.bottomBarLazy = Diaspora.Mobile.Comments.bottomBarLazy(this.bottomBar); }); diff --git a/spec/javascripts/mobile/mobile_post_actions_spec.js b/spec/javascripts/mobile/mobile_post_actions_spec.js index c2e158202..a33a29994 100644 --- a/spec/javascripts/mobile/mobile_post_actions_spec.js +++ b/spec/javascripts/mobile/mobile_post_actions_spec.js @@ -118,6 +118,14 @@ describe("Diaspora.Mobile.PostActions", function(){ expect(this.likeCounter.text()).toBe("0"); }); + it("lets Diaspora.Mobile.Alert handle AJAX errors", function() { + spyOn(Diaspora.Mobile.Alert, "handleAjaxError"); + Diaspora.Mobile.PostActions.like(this.likeCounter, this.link); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "oh noez! like failed!"}); + expect(Diaspora.Mobile.Alert.handleAjaxError).toHaveBeenCalled(); + expect(Diaspora.Mobile.Alert.handleAjaxError.calls.argsFor(0)[0].responseText).toBe("oh noez! like failed!"); + }); + it("activates link on success", function(){ spyOn(Diaspora.Mobile.PostActions, "toggleActive"); var data = this.link.data("url"); @@ -166,6 +174,14 @@ describe("Diaspora.Mobile.PostActions", function(){ expect(this.likeCounter.text()).toBe("1"); }); + it("lets Diaspora.Mobile.Alert handle AJAX errors", function() { + spyOn(Diaspora.Mobile.Alert, "handleAjaxError"); + Diaspora.Mobile.PostActions.unlike(this.likeCounter, this.link); + jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "oh noez! unlike failed!"}); + expect(Diaspora.Mobile.Alert.handleAjaxError).toHaveBeenCalled(); + expect(Diaspora.Mobile.Alert.handleAjaxError.calls.argsFor(0)[0].responseText).toBe("oh noez! unlike failed!"); + }); + it("deactivates link on success", function(){ spyOn(Diaspora.Mobile.PostActions, "toggleActive"); var data = this.link.data("url");