Improved error notifications

This commit is contained in:
shamoon
2023-08-23 23:50:54 -07:00
parent 0ef3a141a8
commit 423e0768f9
14 changed files with 296 additions and 260 deletions

View File

@@ -11,6 +11,32 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'
import { of } from 'rxjs'
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
const toasts = [
{
title: 'Title',
content: 'content',
delay: 5000,
},
{
title: 'Error 1',
content: 'Error 1 content',
delay: 5000,
error: 'Error 1 string',
},
{
title: 'Error 2',
content: 'Error 2 content',
delay: 5000,
error: {
url: 'https://example.com',
status: 500,
statusText: 'Internal Server Error',
message: 'Internal server error 500 message',
error: { detail: 'Error 2 message details' },
},
},
]
describe('ToastsComponent', () => {
let component: ToastsComponent
let fixture: ComponentFixture<ToastsComponent>
@@ -24,20 +50,7 @@ describe('ToastsComponent', () => {
{
provide: ToastService,
useValue: {
getToasts: () =>
of([
{
title: 'Title',
content: 'content',
delay: 5000,
},
{
title: 'Error',
content: 'Error content',
delay: 5000,
error: new Error('Error message'),
},
]),
getToasts: () => of(toasts),
},
},
],
@@ -85,10 +98,41 @@ describe('ToastsComponent', () => {
fixture.detectChanges()
expect(fixture.nativeElement.querySelector('details')).not.toBeNull()
expect(fixture.nativeElement.textContent).toContain('Error message')
expect(fixture.nativeElement.textContent).toContain('Error 1 content')
component.ngOnDestroy()
flush()
discardPeriodicTasks()
}))
it('should show error details, support copy', fakeAsync(() => {
component.ngOnInit()
fixture.detectChanges()
expect(fixture.nativeElement.querySelector('details')).not.toBeNull()
expect(fixture.nativeElement.textContent).toContain(
'Error 2 message details'
)
const copySpy = jest.spyOn(navigator.clipboard, 'writeText')
component.copyError(toasts[2].error)
expect(copySpy).toHaveBeenCalled()
component.ngOnDestroy()
flush()
discardPeriodicTasks()
}))
it('should parse error text, add ellipsis', () => {
expect(component.getErrorText(toasts[2].error)).toEqual(
'Error 2 message details'
)
expect(component.getErrorText({ error: 'Error string no detail' })).toEqual(
'Error string no detail'
)
expect(component.getErrorText('Error string')).toEqual('')
expect(
component.getErrorText({ error: new Array(205).join('a') })
).toContain('...')
})
})