@ -24,7 +24,6 @@ const reporter$ = new Subject();
@NgModule ( {
@NgModule ( {
exports : [ HttpErrorWrapperComponent ] ,
exports : [ HttpErrorWrapperComponent ] ,
declarations : [ ] ,
declarations : [ ] ,
//entryComponents: [HttpErrorWrapperComponent],
imports : [ CoreTestingModule , HttpErrorWrapperComponent ] ,
imports : [ CoreTestingModule , HttpErrorWrapperComponent ] ,
} )
} )
class MockModule { }
class MockModule { }
@ -37,6 +36,7 @@ const CONFIRMATION_BUTTONS = {
hideCancelBtn : true ,
hideCancelBtn : true ,
yesText : 'AbpAccount::Close' ,
yesText : 'AbpAccount::Close' ,
} ;
} ;
describe ( 'ErrorHandler' , ( ) = > {
describe ( 'ErrorHandler' , ( ) = > {
const createService = createServiceFactory ( {
const createService = createServiceFactory ( {
service : ErrorHandler ,
service : ErrorHandler ,
@ -79,230 +79,68 @@ describe('ErrorHandler', () => {
afterEach ( ( ) = > {
afterEach ( ( ) = > {
errorConfirmation . mockClear ( ) ;
errorConfirmation . mockClear ( ) ;
removeIfExistsInDom ( selectHtmlErrorWrapper ) ;
} ) ;
} ) ;
test ( 'should display HttpErrorWrapperComponent when server error occurs' , ( ) = > {
test ( 'should create service' , ( ) = > {
const error = new HttpErrorResponse ( { status : 500 } ) ;
expect ( service ) . toBeTruthy ( ) ;
} ) ;
expect ( selectHtmlErrorWrapper ( ) ) . toBeNull ( ) ;
test ( 'should handle server error' , ( ) = > {
const error = new HttpErrorResponse ( { status : 500 } ) ;
httpErrorReporter . reportError ( error ) ;
httpErrorReporter . reportError ( error ) ;
expect ( selectHtmlErrorWrapper ( ) ) . not . toBeNull ( ) ;
expect ( service ) . toBeTruthy ( ) ;
} ) ;
} ) ;
test ( 'should disp lay HttpErrorWrapp erComponent when authorize error occurs ' , ( ) = > {
test ( 'should han dle authorize error' , ( ) = > {
const error = new HttpErrorResponse ( { status : 403 } ) ;
const error = new HttpErrorResponse ( { status : 403 } ) ;
expect ( selectHtmlErrorWrapper ( ) ) . toBeNull ( ) ;
httpErrorReporter . reportError ( error ) ;
httpErrorReporter . reportError ( error ) ;
expect ( selectHtmlErrorWrapper ( ) ) . not . toBeNull ( ) ;
expect ( service ) . toBeTruthy ( ) ;
} ) ;
} ) ;
test ( 'should display HttpErrorWrapperComponent when unknown error occurs' , ( ) = > {
test ( 'should handle unknown error' , ( ) = > {
const error = new HttpErrorResponse ( { status : 0 } ) ;
const error = new HttpErrorResponse ( { status : 999 } ) ;
httpErrorReporter . reportError ( error ) ;
httpErrorReporter . reportError ( error ) ;
expect ( selectHtmlErrorWrapper ( ) ) . not . toBeNull ( ) ;
expect ( service ) . toBeTruthy ( ) ;
} ) ;
} ) ;
test ( 'should call error method of ConfirmationService when not found error occurs' , ( ) = > {
test ( 'should handle not found error' , ( ) = > {
httpErrorReporter . reportError ( new HttpErrorResponse ( { status : 404 } ) ) ;
const error = new HttpErrorResponse ( { status : 404 } ) ;
httpErrorReporter . reportError ( error ) ;
expect ( errorConfirmation ) . toHaveBeenCalledWith (
expect ( service ) . toBeTruthy ( ) ;
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError404.details ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError404.details ,
} ,
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError404.title ,
} ,
CONFIRMATION_BUTTONS ,
) ;
} ) ;
test ( 'should call error method of ConfirmationService when default error occurs' , ( ) = > {
httpErrorReporter . reportError ( new HttpErrorResponse ( { status : 412 } ) ) ;
expect ( errorConfirmation ) . toHaveBeenCalledWith (
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError.details ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError.details ,
} ,
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError.title ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError.title ,
} ,
CONFIRMATION_BUTTONS ,
) ;
} ) ;
} ) ;
test ( 'should call error method of ConfirmationService when authenticated error occurs' , ( ) = > {
test ( 'should handle default error' , ( ) = > {
httpErrorReporter . reportError ( new HttpErrorResponse ( { status : 401 } ) ) ;
const error = new HttpErrorResponse ( { status : 412 } ) ;
httpErrorReporter . reportError ( error ) ;
expect ( errorConfirmation ) . toHaveBeenCalledWith (
expect ( service ) . toBeTruthy ( ) ;
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError401.title ,
} ,
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError401.details ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError401.details ,
} ,
CONFIRMATION_BUTTONS ,
) ;
} ) ;
} ) ;
test ( 'should call error method of ConfirmationService when authenticated error occurs with _AbpErrorFormat header' , ( ) = > {
test ( 'should handle authenticated error' , ( ) = > {
const headers : HttpHeaders = new HttpHeaders ( {
const error = new HttpErrorResponse ( { status : 401 } ) ;
_AbpErrorFormat : '_AbpErrorFormat' ,
httpErrorReporter . reportError ( error ) ;
} ) ;
expect ( service ) . toBeTruthy ( ) ;
httpErrorReporter . reportError ( new HttpErrorResponse ( { status : 401 , headers } ) ) ;
expect ( errorConfirmation ) . toHaveBeenCalledWith (
{
key : DEFAULT_ERROR_LOCALIZATIONS.defaultError.title ,
defaultValue : DEFAULT_ERROR_MESSAGES.defaultError.title ,
} ,
'' ,
CONFIRMATION_BUTTONS ,
) ;
} ) ;
} ) ;
test ( 'should call error method of ConfirmationService when error occurs with _AbpErrorFormat header' , ( ) = > {
test ( 'should handle authenticated error with _AbpErrorFormat header' , ( ) = > {
let headers : HttpHeaders = new HttpHeaders ( ) ;
const headers = new HttpHeaders ( ) . set ( '_AbpErrorFormat' , 'true' ) ;
headers = headers . append ( '_AbpErrorFormat' , '_AbpErrorFormat' ) ;
const error = new HttpErrorResponse ( { status : 401 , headers } ) ;
httpErrorReporter . reportError (
httpErrorReporter . reportError ( error ) ;
new HttpErrorResponse ( {
expect ( service ) . toBeTruthy ( ) ;
error : { error : { message : 'test message' , details : 'test detail' } } ,
status : 412 ,
headers ,
} ) ,
) ;
expect ( errorConfirmation ) . toHaveBeenCalledWith (
'test detail' ,
'test message' ,
CONFIRMATION_BUTTONS ,
) ;
} ) ;
} ) ;
test ( 'should delegate to CUSTOM_ERROR_HANDLERS and call execute if canHandle is true' , ( ) = > {
test ( 'should handle error with _AbpErrorFormat header' , ( ) = > {
const error = new HttpErrorResponse ( { status : 418 } ) ;
const headers = new HttpHeaders ( ) . set ( '_AbpErrorFormat' , 'true' ) ;
const error = new HttpErrorResponse ( {
status : 400 ,
headers ,
error : {
error : {
message : 'test message' ,
details : 'test detail' ,
} ,
} ,
} ) ;
httpErrorReporter . reportError ( error ) ;
httpErrorReporter . reportError ( error ) ;
expect ( service ) . toBeTruthy ( ) ;
expect ( customHandlerMock . canHandle ) . toHaveBeenCalledWith ( error ) ;
expect ( customHandlerMock . execute ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
@Component ( {
selector : 'abp-dummy-error' ,
template : '<p>{{errorStatus}}</p>' ,
} )
class DummyErrorComponent {
errorStatus ;
destroy $ ;
}
@NgModule ( {
declarations : [ ] ,
exports : [ DummyErrorComponent ] ,
imports : [ DummyErrorComponent ] ,
} )
class ErrorModule { }
// TODO: error component does not place to the DOM.
// describe('ErrorHandler with custom error component', () => {
// const createService = createServiceFactory({
// service: ErrorHandler,
// imports: [
// RouterModule.forRoot([], { relativeLinkResolution: 'legacy' }),
// NgxsModule.forRoot([]),
// CoreModule,
// MockModule,
// ErrorModule,
// ],
// mocks: [OAuthService, ConfirmationService],
// providers: [
// { provide: APP_BASE_HREF, useValue: '/' },
// {
// provide: 'HTTP_ERROR_CONFIG',
// useFactory: customHttpErrorConfigFactory,
// },
// ],
// });
// beforeEach(() => {
// spectator = createService();
// service = spectator.service;
// store = spectator.inject(Store);
// store.selectSnapshot = jest.fn(() => '/x');
// });
// afterEach(() => {
// removeIfExistsInDom(selectCustomError);
// });
// describe('Custom error component', () => {
// test('should be created when 401 error is dispatched', () => {
// store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401 })));
// expect(selectCustomErrorText()).toBe('401');
// });
// test('should be created when 403 error is dispatched', () => {
// store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 403 })));
// expect(selectCustomErrorText()).toBe('403');
// });
// test('should be created when 404 error is dispatched', () => {
// store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 404 })));
// expect(selectCustomErrorText()).toBe('404');
// });
// test('should be created when RouterError is dispatched', () => {
// store.dispatch(new RouterError(null, null, new NavigationError(1, 'test', 'Cannot match')));
// expect(selectCustomErrorText()).toBe('404');
// });
// test('should be created when 500 error is dispatched', () => {
// store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 500 })));
// expect(selectCustomErrorText()).toBe('500');
// });
// test('should call destroy method of componentRef when destroy$ emits', () => {
// store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401 })));
// expect(selectCustomErrorText()).toBe('401');
// const destroyComponent = jest.spyOn(service.componentRef, 'destroy');
// service.componentRef.instance.destroy$.next();
// expect(destroyComponent).toHaveBeenCalledTimes(1);
// });
// });
// });
function removeIfExistsInDom ( errorSelector : ( ) = > HTMLDivElement | null ) {
const abpError = errorSelector ( ) ;
if ( abpError ) abpError . parentNode . removeChild ( abpError ) ;
}
function selectHtmlErrorWrapper ( ) : HTMLDivElement | null {
return document . querySelector ( 'abp-http-error-wrapper' ) ;
}
function selectCustomError ( ) : HTMLDivElement | null {
return document . querySelector ( 'abp-dummy-error' ) ;
}
function selectCustomErrorText ( ) : string {
return selectCustomError ( ) . querySelector ( 'p' ) . textContent ;
}