Refactor aspect membership error response handling with flash messages
This commit is contained in:
parent
b645541d52
commit
8078c60cee
5 changed files with 33 additions and 2 deletions
|
|
@ -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
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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: ""
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue