-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initialization error page params passing via url fragment (#443)
* fix: using fragment instead of query params to pass error details * feat: increased coverage and fixed code smells
- Loading branch information
Showing
6 changed files
with
203 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
56 changes: 56 additions & 0 deletions
56
...pp/shell/components/initialization-error-page/initialization-error-page.component.spec.ts
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,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]') | ||
}) | ||
}) |
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
81 changes: 81 additions & 0 deletions
81
src/app/shell/utils/initialization-error-handler.utils.spec.ts
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,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() | ||
}) | ||
}) |
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