Refactor aspect membership error response handling with flash messages

This commit is contained in:
Steffen van Bergerem 2016-11-18 22:47:58 +01:00 committed by Benjamin Neff
parent b645541d52
commit 8078c60cee
5 changed files with 33 additions and 2 deletions

View file

@ -125,7 +125,7 @@ app.views.AspectMembership = app.views.Base.extend({
_displayError: function(model, resp) { _displayError: function(model, resp) {
this._done(); this._done();
this.dropdown.closest(".aspect_membership_dropdown").removeClass("open"); // close the dropdown 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 // remove the membership with the given id
@ -134,7 +134,7 @@ app.views.AspectMembership = app.views.Base.extend({
this.listenToOnce(membership, "sync", this._successDestroyCb); this.listenToOnce(membership, "sync", this._successDestroyCb);
this.listenToOnce(membership, "error", this._displayError); this.listenToOnce(membership, "error", this._displayError);
return membership.destroy(); return membership.destroy({wait: true});
}, },
_successDestroyCb: function(aspectMembership) { _successDestroyCb: function(aspectMembership) {

View file

@ -16,5 +16,13 @@ app.views.FlashMessages = app.views.Base.extend({
error: function(message){ error: function(message){
this._flash(message, true); this._flash(message, true);
},
handleAjaxError: function(response) {
if (response.status === 0) {
this.error(Diaspora.I18n.t("errors.connection"));
} else {
this.error(response.responseText);
}
} }
}); });

View file

@ -87,6 +87,9 @@ en:
success: "Your new aspect <%= name %> was created" success: "Your new aspect <%= name %> was created"
failure: "Aspect creation failed." failure: "Aspect creation failed."
errors:
connection: "Unable to connect to the server."
timeago: timeago:
prefixAgo: "" prefixAgo: ""
prefixFromNow: "" prefixFromNow: ""

View file

@ -55,9 +55,12 @@ describe("app.views.AspectMembership", function(){
}); });
it('displays an error when it fails', function() { it('displays an error when it fails', function() {
spyOn(app.flashMessages, "handleAjaxError").and.callThrough();
this.newAspect.trigger('click'); this.newAspect.trigger('click');
jasmine.Ajax.requests.mostRecent().respondWith(resp_fail); 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"); expect(spec.content().find(".flash-message")).toBeErrorFlashMessage("error message");
}); });
}); });
@ -95,9 +98,12 @@ describe("app.views.AspectMembership", function(){
}); });
it('displays an error when it fails', function() { it('displays an error when it fails', function() {
spyOn(app.flashMessages, "handleAjaxError").and.callThrough();
this.oldAspect.trigger('click'); this.oldAspect.trigger('click');
jasmine.Ajax.requests.mostRecent().respondWith(resp_fail); 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"); expect(spec.content().find(".flash-message")).toBeErrorFlashMessage("error message");
}); });
}); });

View file

@ -30,4 +30,18 @@ describe("app.views.FlashMessages", function(){
expect($(".flash-message").text().trim()).toBe("error!"); 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");
});
});
}); });