-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor API failure logic into DialogUserSubmitErrorHandlerService
- Loading branch information
Showing
5 changed files
with
73 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/assets/javascripts/services/dialog_user_submit_error_handler_service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
ManageIQ.angular.app.service('dialogUserSubmitErrorHandlerService', ['miqService', function(miqService) { | ||
this.handleError = function(err) { | ||
miqService.sparkleOff(); | ||
var fullErrorMessage = err.data.error.message; | ||
var allErrorMessages = fullErrorMessage.split('-')[1].split(','); | ||
clearFlash(); | ||
_.forEach(allErrorMessages, function(errorMessage) { | ||
add_flash(errorMessage, 'error'); | ||
}); | ||
console.error(err); | ||
return err; | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spec/javascripts/services/dialog_user_submit_error_handling_service_spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
describe('dialogUserSubmitErrorHandlerService', function() { | ||
var testService, miqService; | ||
|
||
beforeEach(module('ManageIQ')); | ||
|
||
beforeEach(inject(function(dialogUserSubmitErrorHandlerService, _miqService_) { | ||
testService = dialogUserSubmitErrorHandlerService; | ||
miqService = _miqService_; | ||
})); | ||
|
||
describe('#handleError', function() { | ||
var errorData; | ||
|
||
beforeEach(function() { | ||
errorData = {data: {error: {message: "Failed! -One,Two"}}}; | ||
spyOn(miqService, 'sparkleOff'); | ||
spyOn(window, 'clearFlash'); | ||
spyOn(window, 'add_flash'); | ||
}); | ||
|
||
it('turns off the sparkle', function() { | ||
testService.handleError(errorData); | ||
expect(miqService.sparkleOff).toHaveBeenCalled(); | ||
}); | ||
|
||
it('clears flash messages', function() { | ||
testService.handleError(errorData); | ||
expect(window.clearFlash).toHaveBeenCalled(); | ||
}); | ||
|
||
it('adds flash messages for each message after the -', function() { | ||
testService.handleError(errorData); | ||
expect(window.add_flash).toHaveBeenCalledWith('One', 'error'); | ||
expect(window.add_flash).toHaveBeenCalledWith('Two', 'error'); | ||
}); | ||
|
||
it('returns the error data to bubble up to the parent', function() { | ||
expect(testService.handleError(errorData)).toEqual(errorData); | ||
}); | ||
}); | ||
}); |