Skip to content

Commit

Permalink
feat: increased coverage and fixed code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Dec 13, 2024
1 parent d0006ea commit c71d4a5
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 8 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"jest-preset-angular": "14.2.4",
"jest-sonar-reporter": "^2.0.0",
"jsonc-eslint-parser": "^2.4.0",
"ngx-translate-testing": "^7.0.0",
"nx": "19.8.2",
"prettier": "^3.3.3",
"ts-jest": "^29.2.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { TestBed, ComponentFixture } from '@angular/core/testing'
import { ActivatedRoute } from '@angular/router'
import { of } from 'rxjs'
import { TranslateTestingModule } from 'ngx-translate-testing'
import { InitializationErrorPageComponent } from './initialization-error-page.component'

describe('InitializationErrorPageComponent', () => {
let component: InitializationErrorPageComponent
let fixture: ComponentFixture<InitializationErrorPageComponent>
let route: ActivatedRoute

const fragmentParams = {
message: 'Test Error',
requestedUrl: 'http://example.com',
detail: 'Detail message',
errorCode: '404',
invalidParams: '[param1: Invalid]',
params: '[key1: value1]'
}

beforeEach(() => {
route = {
fragment: of(new URLSearchParams(fragmentParams).toString())
} as any

TestBed.configureTestingModule({
declarations: [InitializationErrorPageComponent],
imports: [
TranslateTestingModule.withTranslations({
en: require('../../../../assets/i18n/en.json')
}).withDefaultLanguage('en')
],
providers: [{ provide: ActivatedRoute, useValue: route }]
}).compileComponents()

fixture = TestBed.createComponent(InitializationErrorPageComponent)
component = fixture.componentInstance
fixture.detectChanges()
})

it('should create', () => {
expect(component).toBeTruthy()
})

it('should display the error details in the template', () => {
fixture.detectChanges()
const compiled = fixture.nativeElement as HTMLElement

expect(compiled.querySelector('#onecxInitializationErrorMessage')?.textContent).toContain('Test Error')
expect(compiled.querySelector('#onecxInitializationErrorRequestedUrl')?.textContent).toContain('http://example.com')
expect(compiled.querySelector('#onecxInitializationErrorDetail')?.textContent).toContain('Detail message')
expect(compiled.querySelector('#onecxInitializationErrorErrorCode')?.textContent).toContain('404')
expect(compiled.querySelector('#onecxInitializationErrorInvalidParams')?.textContent).toContain('[param1: Invalid]')
expect(compiled.querySelector('#onecxInitializationErrorParams')?.textContent).toContain('[key1: value1]')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@ interface InitializationError {
<h1 class="md:text-2xl text-lg mb-1">{{ 'INITIALIZATION_ERROR_PAGE.TITLE' | translate }}</h1>
<p class="md:text-lg text-base">{{ 'INITIALIZATION_ERROR_PAGE.SUBTITLE' | translate }}</p>
<div *ngIf="error$ | async as error" class="mt-3 flex flex-column row-gap-2">
<div *ngIf="error.message">
<div id="onecxInitializationErrorMessage" *ngIf="error.message">
<div class="md:text-base text-sm">
{{ 'INITIALIZATION_ERROR_PAGE.DETAILS.MESSAGE' | translate }}
</div>
<i>{{ error.message }}</i>
</div>
<div *ngIf="error.requestedUrl">
<div id="onecxInitializationErrorRequestedUrl" *ngIf="error.requestedUrl">
<div class="md:text-base text-sm">
{{ 'INITIALIZATION_ERROR_PAGE.DETAILS.REQUESTED_URL' | translate }}
</div>
<i>{{ error.requestedUrl }}</i>
</div>
<div *ngIf="error.detail">
<div id="onecxInitializationErrorDetail" *ngIf="error.detail">
<div class="md:text-base text-sm">
{{ 'INITIALIZATION_ERROR_PAGE.DETAILS.DETAILS' | translate }}
</div>
<i>{{ error.detail }}</i>
</div>
<div *ngIf="error.errorCode">
<div id="onecxInitializationErrorErrorCode" *ngIf="error.errorCode">
<div class="md:text-base text-sm">
{{ 'INITIALIZATION_ERROR_PAGE.DETAILS.ERRORCODE' | translate }}
</div>
<i>{{ error.errorCode }}</i>
</div>
<div *ngIf="error.invalidParams">
<div id="onecxInitializationErrorInvalidParams" *ngIf="error.invalidParams">
<div class="md:text-base text-sm">
{{ 'INITIALIZATION_ERROR_PAGE.DETAILS.INVALID_PARAMS' | translate }}
</div>
<i>{{ error.invalidParams }}</i>
</div>
<div *ngIf="error.params">
<div id="onecxInitializationErrorParams" *ngIf="error.params">
<div class="md:text-base text-sm">
{{ 'INITIALIZATION_ERROR_PAGE.DETAILS.PARAMS' | translate }}
</div>
Expand Down
81 changes: 81 additions & 0 deletions src/app/shell/utils/initialization-error-handler.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { TestBed } from '@angular/core/testing'
import { Router } from '@angular/router'
import { initializationErrorHandler } from './initialization-error-handler.utils'
import { HttpErrorResponse } from '@angular/common/http'

describe('initializationErrorHandler', () => {
let router: Router

beforeEach(() => {
TestBed.configureTestingModule({
providers: [{ provide: Router, useValue: { navigate: jest.fn() } }]
})

router = TestBed.inject(Router)
})

it('should log the error and navigate to the error page with fragment parameters for ErrorEvent', () => {
const errorEvent = new ErrorEvent('Network error', { error: { message: 'Network error occurred' } })
const consoleSpy = jest.spyOn(console, 'error')

initializationErrorHandler(errorEvent, router)

expect(consoleSpy).toHaveBeenCalledWith(errorEvent)
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('message=Network+error+occurred')
})

consoleSpy.mockRestore()
})

it('should log the error and navigate to the error page with fragment parameters for HttpErrorResponse', () => {
const httpErrorResponse = new HttpErrorResponse({
error: {
detail: 'Detail message',
errorCode: '404',
invalidParams: [{ name: 'param1', message: 'Invalid' }],
params: [{ key: 'key1', value: 'value1' }]
},
status: 404,
statusText: 'Not Found',
url: 'http://example.com'
})
;(httpErrorResponse as any)['message'] = 'HTTP error occurred'
const consoleSpy = jest.spyOn(console, 'error')

initializationErrorHandler(httpErrorResponse, router)

expect(consoleSpy).toHaveBeenCalledWith(httpErrorResponse)
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('message=HTTP+error+occurred')
})
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('detail=Detail+message')
})
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('errorCode=404')
})
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('invalidParams=%5Bparam1%3A+Invalid%5D')
})
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('params=%5Bkey1%3A+value1%5D')
})

consoleSpy.mockRestore()
})

it('should handle unknown error types gracefully', () => {
const unknownError = { message: 'Unknown error' }
const consoleSpy = jest.spyOn(console, 'error')

initializationErrorHandler(unknownError, router)

expect(consoleSpy).toHaveBeenCalledWith(unknownError)
expect(router.navigate).toHaveBeenCalledWith(['portal-initialization-error-page'], {
fragment: expect.stringContaining('message=')
})

consoleSpy.mockRestore()
})
})
6 changes: 4 additions & 2 deletions src/app/shell/utils/initialization-error-handler.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ export function initializationErrorHandler(error: any, router: Router): Observab
params.set(
'invalidParams',
initError.details?.invalidParams
? `[${initError.details.invalidParams.map((invalidParam) => `${invalidParam.name}: ${invalidParam.message}`)}]`
? '['.concat(initError.details.invalidParams.map((p) => `${p.name}: ${p.message}`).join(',')).concat(']')
: ''
)
params.set(
'params',
initError.details?.params ? `[${initError.details.params.map((param) => `${param.key}: ${param.value}`)}]` : ''
initError.details?.params
? '['.concat(initError.details.params.map((p) => `${p.key}: ${p.value}`).join(',')).concat(']')
: ''
)

router.navigate(['portal-initialization-error-page'], { fragment: params.toString() })
Expand Down

0 comments on commit c71d4a5

Please sign in to comment.