]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/notifications/notifications.component.spec.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / navigation / notifications / notifications.component.spec.ts
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3
4 import { PopoverModule } from 'ngx-bootstrap/popover';
5 import { ToastrModule } from 'ngx-toastr';
6
7 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
8 import { PrometheusService } from '../../../shared/api/prometheus.service';
9 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
10 import { PrometheusAlertService } from '../../../shared/services/prometheus-alert.service';
11 import { PrometheusNotificationService } from '../../../shared/services/prometheus-notification.service';
12 import { SharedModule } from '../../../shared/shared.module';
13 import { NotificationsComponent } from './notifications.component';
14
15 describe('NotificationsComponent', () => {
16 let component: NotificationsComponent;
17 let fixture: ComponentFixture<NotificationsComponent>;
18
19 configureTestBed({
20 imports: [
21 HttpClientTestingModule,
22 PopoverModule.forRoot(),
23 SharedModule,
24 ToastrModule.forRoot()
25 ],
26 declarations: [NotificationsComponent],
27 providers: i18nProviders
28 });
29
30 beforeEach(() => {
31 fixture = TestBed.createComponent(NotificationsComponent);
32 component = fixture.componentInstance;
33 });
34
35 it('should create', () => {
36 fixture.detectChanges();
37 expect(component).toBeTruthy();
38 });
39
40 describe('prometheus alert handling', () => {
41 let prometheusAlertService: PrometheusAlertService;
42 let prometheusNotificationService: PrometheusNotificationService;
43 let prometheusAccessAllowed: boolean;
44
45 const expectPrometheusServicesToBeCalledTimes = (n: number) => {
46 expect(prometheusNotificationService.refresh).toHaveBeenCalledTimes(n);
47 expect(prometheusAlertService.refresh).toHaveBeenCalledTimes(n);
48 };
49
50 beforeEach(() => {
51 prometheusAccessAllowed = true;
52 spyOn(TestBed.get(AuthStorageService), 'getPermissions').and.callFake(() => ({
53 prometheus: { read: prometheusAccessAllowed }
54 }));
55
56 spyOn(TestBed.get(PrometheusService), 'ifAlertmanagerConfigured').and.callFake((fn) => fn());
57
58 prometheusAlertService = TestBed.get(PrometheusAlertService);
59 spyOn(prometheusAlertService, 'refresh').and.stub();
60
61 prometheusNotificationService = TestBed.get(PrometheusNotificationService);
62 spyOn(prometheusNotificationService, 'refresh').and.stub();
63 });
64
65 it('should not refresh prometheus services if not allowed', () => {
66 prometheusAccessAllowed = false;
67 fixture.detectChanges();
68
69 expectPrometheusServicesToBeCalledTimes(0);
70 });
71 it('should first refresh prometheus notifications and alerts during init', () => {
72 fixture.detectChanges();
73
74 expect(prometheusAlertService.refresh).toHaveBeenCalledTimes(1);
75 expectPrometheusServicesToBeCalledTimes(1);
76 });
77
78 it('should refresh prometheus services every 5s', fakeAsync(() => {
79 fixture.detectChanges();
80
81 expectPrometheusServicesToBeCalledTimes(1);
82 tick(5000);
83 expectPrometheusServicesToBeCalledTimes(2);
84 tick(15000);
85 expectPrometheusServicesToBeCalledTimes(5);
86 component.ngOnDestroy();
87 }));
88 });
89 });