diff --git a/app/assets/javascripts/admin/utils/services/errors_parser.js.coffee b/app/assets/javascripts/admin/utils/services/errors_parser.js.coffee index 12c1682d77..87a83360a1 100644 --- a/app/assets/javascripts/admin/utils/services/errors_parser.js.coffee +++ b/app/assets/javascripts/admin/utils/services/errors_parser.js.coffee @@ -2,16 +2,25 @@ angular.module("admin.utils").factory "ErrorsParser", -> new class ErrorsParser toString: (errors, defaultContent = "") => + return defaultContent unless errors? + + errorsString = "" if errors.length > 0 # it is an array of errors - errorsString = error + "\n" for error in errors + errorsString = this.arrayToString(errors) else # it is a hash of errors keys = Object.keys(errors) - errorsString = "" for key in keys - errorsString += error for error in errors[key] + errorsString += this.arrayToString(errors[key]) - errorsString = defaultContent if errorsString == "" + this.defaultIfEmpty(errorsString, defaultContent) - errorsString + arrayToString: (array) => + string = "" + string += entry + "\n" for entry in array + string + + defaultIfEmpty: (content, defaultContent) => + return defaultContent if content == "" + content diff --git a/spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee b/spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee index 600e0c16e4..4feff89868 100644 --- a/spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee +++ b/spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee @@ -725,7 +725,7 @@ describe "AdminProductEditCtrl", -> $httpBackend.expectPOST("/admin/products/bulk_update").respond 400, { "errors": { "base": ["a basic error"] } } $scope.updateProducts "updated list of products" $httpBackend.flush() - expect($scope.displayFailure).toHaveBeenCalledWith("Saving failed with the following error(s):\na basic error") + expect($scope.displayFailure).toHaveBeenCalledWith("Saving failed with the following error(s):\na basic error\n") describe "adding variants", -> diff --git a/spec/javascripts/unit/admin/utils/services/errors_parser_spec.js.coffee b/spec/javascripts/unit/admin/utils/services/errors_parser_spec.js.coffee new file mode 100644 index 0000000000..9ae47b201a --- /dev/null +++ b/spec/javascripts/unit/admin/utils/services/errors_parser_spec.js.coffee @@ -0,0 +1,20 @@ +describe "ErrorsParser service", -> + errorsParser = null + + beforeEach -> + module('admin.utils') + inject (ErrorsParser) -> + errorsParser = ErrorsParser + + describe "toString", -> + it "returns empty string for nil errors", -> + expect(errorsParser.toString(null)).toEqual "" + + it "returns the elements in the array if an array is provided", -> + expect(errorsParser.toString(["1", "2"])).toEqual "1\n2\n" + + it "returns the elements in the hash if a hash is provided", -> + expect(errorsParser.toString({ "keyname": ["1", "2"] })).toEqual "1\n2\n" + + it "returns all elements in all hash keys provided", -> + expect(errorsParser.toString({ "keyname1": ["1", "2"], "keyname2": ["3", "4"] })).toEqual "1\n2\n3\n4\n"