/
error-handler.service.spec.ts
90 lines (73 loc) · 3.01 KB
/
error-handler.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { OverlayContainer } from '@angular/cdk/overlay';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { MatDialog, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { HealthEndpointSystem, MockHealth } from '@dasch-swiss/dsp-js';
import { of } from 'rxjs';
import { DspApiConnectionToken } from '../declarations/dsp-api-tokens';
import { DialogComponent } from '../dialog/dialog.component';
import { ErrorHandlerService } from './error-handler.service';
describe('ErrorHandlerService', () => {
let httpTestingController: HttpTestingController;
let service: ErrorHandlerService;
let overlayContainer: OverlayContainer;
let dialog: MatDialog;
beforeEach(() => {
const apiEndpointSpyObj = {
v2: {
auth: jasmine.createSpyObj('auth', ['logout'])
},
system: {
healthEndpoint: jasmine.createSpyObj('healthEndpoint', ['getHealthStatus'])
}
};
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
HttpClientTestingModule,
MatDialogModule,
MatSnackBarModule,
NoopAnimationsModule
],
providers: [
{
provide: MatDialog,
useValue: { open: () => of({ id: 1 }) }
},
{
provide: DspApiConnectionToken,
useValue: apiEndpointSpyObj
}
]
});
service = TestBed.inject(ErrorHandlerService);
httpTestingController = TestBed.inject(HttpTestingController);
const dspConnSpy = TestBed.inject(DspApiConnectionToken);
(dspConnSpy.system.healthEndpoint as jasmine.SpyObj<HealthEndpointSystem>).getHealthStatus.and.callFake(
() => {
const health = MockHealth.mockRunning();
return of(health);
}
);
overlayContainer = TestBed.inject(OverlayContainer);
dialog = TestBed.inject(MatDialog);
});
afterEach(() => {
httpTestingController.verify();
});
afterEach(async () => {
// angular won't call this for us so we need to do it ourselves to avoid leaks.
overlayContainer.ngOnDestroy();
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('api is not healthy: should return 503 server error', () => {
const error = require('../../../assets/test-data/api-error-0.json');
service.showMessage(error);
spyOn(dialog, 'open').and.returnValue({ afterClosed: () => of({ id: 1 }) } as MatDialogRef<typeof DialogComponent>);
expect(dialog).toBeDefined();
});
});