Skip to content

Commit

Permalink
Add HttpErrorService unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpetitcolas committed Nov 23, 2016
1 parent 59cc856 commit 87eabc3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
export default function httpErrorService($state, $translate, notification) {

function httpErrorService($state, $translate, notification) {
return {
handleError: function(event, toState, toParams, fromState, fromParams, error){
handleError: function(event, toState, toParams, fromState, fromParams, error) {
switch (error.status) {
case 404:
this.handle404Error(event, error);
break;
case 403:
this.handle403Error(error);
break;
default:
this.handleDefaultError(error);
break;
case 404:
this.handle404Error(event, error);
break;
case 403:
this.handle403Error(error);
break;
default:
this.handleDefaultError(error);
break;
}
},
handle404Error: function(event,error){
$state.go('ma-404');
event.preventDefault();
},
handle403Error: function(error){
$translate('STATE_FORBIDDEN_ERROR', { message: error.data.message }).then(this.displayError);
throw error;
},
handleDefaultError: function(error){
$translate('STATE_CHANGE_ERROR', { message: error.message }).then(this.displayError);
},

handle404Error: function(event,error) {
event.preventDefault();
$state.go('ma-404');
},

handle403Error: function(error) {
$translate('STATE_FORBIDDEN_ERROR', { message: error.data.message }).then(this.displayError);
throw error;
},

handleDefaultError: function(error) {
$translate('STATE_CHANGE_ERROR', { message: error.message }).then(this.displayError);
throw error;
},
displayError:text => notification.log(text, { addnCls: 'humane-flatty-error' })
},

displayError: text => notification.log(text, { addnCls: 'humane-flatty-error' }),
}
}

httpErrorService.$inject =['$state', '$translate', 'notification'];
httpErrorService.$inject = ['$state', '$translate', 'notification'];

export default httpErrorService;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const HttpErrorService = require('../../../../../ng-admin/Main/component/provider/HttpErrorService');

describe('Http Error Service', () => {
let $state;
let $translate;
let httpErrorService;
let notification;

angular.module('test_httpErrorService', [])
.service('$state', () => ({
go: jasmine.createSpy('$state.go'),
}))
.service('$translate', () => (
jasmine.createSpy('$translate').and.returnValue({ then: cb => cb('translated') })
))
.service('notification', () => ({
log: jasmine.createSpy('notification.log'),
}))
.service('HttpErrorService', HttpErrorService);

beforeEach(angular.mock.module('test_httpErrorService'));

beforeEach(inject(function (_$state_, _$translate_, _HttpErrorService_, _notification_) {
$state = _$state_;
$translate = _$translate_;
httpErrorService = _HttpErrorService_;
notification = _notification_;
}));

it('should redirect to `ma-404` state when receiving a 404 error', () => {
const event = {
preventDefault: jasmine.createSpy(),
};

const error = { status: 404 };
httpErrorService.handleError(event, '', '', '', '', error);

expect(event.preventDefault).toHaveBeenCalled();
expect($state.go).toHaveBeenCalledWith('ma-404');
});

it('should display a translated forbidden error notification in case of 403', () => {
const error = {
status: 403,
data: {
message: 'Forbidden access',
},
};

try {
httpErrorService.handleError('', '', '', '', '', error);
expect(true).toBe(false);
} catch (e) {
// should throw an exception in case of 403
}

expect($translate).toHaveBeenCalledWith('STATE_FORBIDDEN_ERROR', { message: 'Forbidden access' });
expect(notification.log).toHaveBeenCalledWith('translated', { addnCls: 'humane-flatty-error' });
});

it('should display generic translated error notification if neither 404 nor 403', () => {
const error = {
status: 500,
message: 'Unknown error',
};

try {
httpErrorService.handleError('', '', '', '', '', error);
expect(true).toBe(false);
} catch (e) {
// should throw an exception in case of 403
}

expect($translate).toHaveBeenCalledWith('STATE_CHANGE_ERROR', { message: 'Unknown error' });
expect(notification.log).toHaveBeenCalledWith('translated', { addnCls: 'humane-flatty-error' });
});
});

0 comments on commit 87eabc3

Please sign in to comment.