Skip to content

Commit

Permalink
Refactor aspect membership error response handling with flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
svbergerem authored and SuperTux88 committed Nov 22, 2016
1 parent b645541 commit 8078c60
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/app/views/aspect_membership_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ app.views.AspectMembership = app.views.Base.extend({
_displayError: function(model, resp) {
this._done();
this.dropdown.closest(".aspect_membership_dropdown").removeClass("open"); // close the dropdown
app.flashMessages.error(resp.responseText);
app.flashMessages.handleAjaxError(resp);
},

// remove the membership with the given id
Expand All @@ -134,7 +134,7 @@ app.views.AspectMembership = app.views.Base.extend({
this.listenToOnce(membership, "sync", this._successDestroyCb);
this.listenToOnce(membership, "error", this._displayError);

return membership.destroy();
return membership.destroy({wait: true});
},

_successDestroyCb: function(aspectMembership) {
Expand Down
8 changes: 8 additions & 0 deletions app/assets/javascripts/app/views/flash_messages_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ app.views.FlashMessages = app.views.Base.extend({

error: function(message){
this._flash(message, true);
},

handleAjaxError: function(response) {
if (response.status === 0) {
this.error(Diaspora.I18n.t("errors.connection"));
} else {
this.error(response.responseText);
}
}
});
3 changes: 3 additions & 0 deletions config/locales/javascript/javascript.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ en:
success: "Your new aspect <%= name %> was created"
failure: "Aspect creation failed."

errors:
connection: "Unable to connect to the server."

timeago:
prefixAgo: ""
prefixFromNow: ""
Expand Down
6 changes: 6 additions & 0 deletions spec/javascripts/app/views/aspect_membership_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ describe("app.views.AspectMembership", function(){
});

it('displays an error when it fails', function() {
spyOn(app.flashMessages, "handleAjaxError").and.callThrough();
this.newAspect.trigger('click');
jasmine.Ajax.requests.mostRecent().respondWith(resp_fail);

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");
});
});
Expand Down Expand Up @@ -95,9 +98,12 @@ describe("app.views.AspectMembership", function(){
});

it('displays an error when it fails', function() {
spyOn(app.flashMessages, "handleAjaxError").and.callThrough();
this.oldAspect.trigger('click');
jasmine.Ajax.requests.mostRecent().respondWith(resp_fail);

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");
});
});
Expand Down
14 changes: 14 additions & 0 deletions spec/javascripts/app/views/flash_messages_view-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ describe("app.views.FlashMessages", function(){
expect($(".flash-message").text().trim()).toBe("error!");
});
});

describe("handleAjaxError", function() {
it("shows a generic error if the connection failed", function() {
spyOn(flashMessages, "error");
flashMessages.handleAjaxError({status: 0});
expect(flashMessages.error).toHaveBeenCalledWith(Diaspora.I18n.t("errors.connection"));
});

it("shows the error given in the responseText otherwise", function() {
spyOn(flashMessages, "error");
flashMessages.handleAjaxError({status: 400, responseText: "some specific ajax error"});
expect(flashMessages.error).toHaveBeenCalledWith("some specific ajax error");
});
});
});

0 comments on commit 8078c60

Please sign in to comment.