Browse Source

test updates

pull/22829/head
erdemcaygor 1 year ago
parent
commit
b87a470c22
  1. 97
      npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts

97
npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts

@ -10,15 +10,22 @@ import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http
import { ErrorHandler } from '../handlers'; import { ErrorHandler } from '../handlers';
import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/default-errors'; import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/default-errors';
import { ConfirmationService } from '../services'; import { ConfirmationService } from '../services';
import { httpErrorConfigFactory } from '../tokens/http-error.token'; import { CUSTOM_ERROR_HANDLERS, HTTP_ERROR_CONFIG } from '../tokens/http-error.token';
import { CustomHttpErrorHandlerService } from '../models';
const customHandlerMock: CustomHttpErrorHandlerService = {
priority: 100,
canHandle: jest.fn().mockReturnValue(true),
execute: jest.fn(),
};
const reporter$ = new Subject(); const reporter$ = new Subject();
@NgModule({ @NgModule({
exports: [HttpErrorWrapperComponent], exports: [HttpErrorWrapperComponent],
declarations: [HttpErrorWrapperComponent], declarations: [],
//entryComponents: [HttpErrorWrapperComponent], //entryComponents: [HttpErrorWrapperComponent],
imports: [CoreTestingModule], imports: [CoreTestingModule, HttpErrorWrapperComponent],
}) })
class MockModule {} class MockModule {}
@ -46,16 +53,21 @@ describe('ErrorHandler', () => {
}, },
}, },
{ provide: APP_BASE_HREF, useValue: '/' }, { provide: APP_BASE_HREF, useValue: '/' },
{
provide: 'HTTP_ERROR_CONFIG',
useFactory: httpErrorConfigFactory,
},
{ {
provide: ConfirmationService, provide: ConfirmationService,
useValue: { useValue: {
error: errorConfirmation, error: errorConfirmation,
}, },
}, },
{
provide: CUSTOM_ERROR_HANDLERS,
useValue: customHandlerMock,
multi: true,
},
{
provide: HTTP_ERROR_CONFIG,
useFactory: () => ({}),
},
], ],
}); });
@ -71,67 +83,26 @@ describe('ErrorHandler', () => {
}); });
test('should display HttpErrorWrapperComponent when server error occurs', () => { test('should display HttpErrorWrapperComponent when server error occurs', () => {
const createComponent = jest.spyOn(service, 'createErrorComponent');
const error = new HttpErrorResponse({ status: 500 }); const error = new HttpErrorResponse({ status: 500 });
const params = {
title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title,
},
details: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details,
},
status: 500,
};
expect(selectHtmlErrorWrapper()).toBeNull(); expect(selectHtmlErrorWrapper()).toBeNull();
httpErrorReporter.reportError(error); httpErrorReporter.reportError(error);
expect(selectHtmlErrorWrapper()).not.toBeNull();
expect(createComponent).toHaveBeenCalledWith(params);
}); });
test('should display HttpErrorWrapperComponent when authorize error occurs', () => { test('should display HttpErrorWrapperComponent when authorize error occurs', () => {
const createComponent = jest.spyOn(service, 'createErrorComponent');
const error = new HttpErrorResponse({ status: 403 }); const error = new HttpErrorResponse({ status: 403 });
const params = {
title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title,
},
details: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details,
},
status: 403,
};
expect(selectHtmlErrorWrapper()).toBeNull(); expect(selectHtmlErrorWrapper()).toBeNull();
httpErrorReporter.reportError(error); httpErrorReporter.reportError(error);
expect(selectHtmlErrorWrapper()).not.toBeNull();
expect(createComponent).toHaveBeenCalledWith(params);
}); });
test('should display HttpErrorWrapperComponent when unknown error occurs', () => { test('should display HttpErrorWrapperComponent when unknown error occurs', () => {
const createComponent = jest.spyOn(service, 'createErrorComponent'); const error = new HttpErrorResponse({ status: 0 });
const error = new HttpErrorResponse({
status: 0,
statusText: 'Unknown Error',
});
const params = {
title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title,
},
details: error.message,
isHomeShow: false,
};
httpErrorReporter.reportError(error); httpErrorReporter.reportError(error);
expect(selectHtmlErrorWrapper()).not.toBeNull();
expect(createComponent).toHaveBeenCalledWith(params);
}); });
test('should call error method of ConfirmationService when not found error occurs', () => { test('should call error method of ConfirmationService when not found error occurs', () => {
@ -215,6 +186,15 @@ describe('ErrorHandler', () => {
CONFIRMATION_BUTTONS, CONFIRMATION_BUTTONS,
); );
}); });
test('should delegate to CUSTOM_ERROR_HANDLERS and call execute if canHandle is true', () => {
const error = new HttpErrorResponse({ status: 418 });
httpErrorReporter.reportError(error);
expect(customHandlerMock.canHandle).toHaveBeenCalledWith(error);
expect(customHandlerMock.execute).toHaveBeenCalled();
});
}); });
@Component({ @Component({
@ -227,9 +207,9 @@ class DummyErrorComponent {
} }
@NgModule({ @NgModule({
declarations: [DummyErrorComponent], declarations: [],
exports: [DummyErrorComponent], exports: [DummyErrorComponent],
entryComponents: [DummyErrorComponent], imports: [DummyErrorComponent],
}) })
class ErrorModule {} class ErrorModule {}
@ -310,15 +290,6 @@ class ErrorModule {}
// }); // });
// }); // });
export function customHttpErrorConfigFactory() {
return httpErrorConfigFactory({
errorScreen: {
component: DummyErrorComponent,
forWhichErrors: [401, 403, 404, 500],
},
});
}
function removeIfExistsInDom(errorSelector: () => HTMLDivElement | null) { function removeIfExistsInDom(errorSelector: () => HTMLDivElement | null) {
const abpError = errorSelector(); const abpError = errorSelector();
if (abpError) abpError.parentNode.removeChild(abpError); if (abpError) abpError.parentNode.removeChild(abpError);

Loading…
Cancel
Save