]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
e40d38a11057084cff17ec7f9f37f273a8c7298f
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3
4 import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';
5 import { ToastrModule } from 'ngx-toastr';
6 import { of } from 'rxjs';
7
8 import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
9 import { UserService } from '~/app/shared/api/user.service';
10 import { Permissions } from '~/app/shared/models/permissions';
11 import { PipesModule } from '~/app/shared/pipes/pipes.module';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { NotificationService } from '~/app/shared/services/notification.service';
14 import { TelemetryNotificationService } from '~/app/shared/services/telemetry-notification.service';
15 import { configureTestBed } from '~/testing/unit-test-helper';
16 import { TelemetryNotificationComponent } from './telemetry-notification.component';
17
18 describe('TelemetryActivationNotificationComponent', () => {
19   let component: TelemetryNotificationComponent;
20   let fixture: ComponentFixture<TelemetryNotificationComponent>;
21
22   let authStorageService: AuthStorageService;
23   let mgrModuleService: MgrModuleService;
24   let notificationService: NotificationService;
25
26   let isNotificationHiddenSpy: jasmine.Spy;
27   let getPermissionsSpy: jasmine.Spy;
28   let getConfigSpy: jasmine.Spy;
29
30   const configOptPermissions: Permissions = new Permissions({
31     'config-opt': ['read', 'create', 'update', 'delete']
32   });
33   const noConfigOptPermissions: Permissions = new Permissions({});
34   const telemetryEnabledConfig = {
35     enabled: true
36   };
37   const telemetryDisabledConfig = {
38     enabled: false
39   };
40
41   configureTestBed({
42     declarations: [TelemetryNotificationComponent],
43     imports: [NgbAlertModule, HttpClientTestingModule, ToastrModule.forRoot(), PipesModule],
44     providers: [MgrModuleService, UserService]
45   });
46
47   beforeEach(() => {
48     fixture = TestBed.createComponent(TelemetryNotificationComponent);
49     component = fixture.componentInstance;
50     authStorageService = TestBed.inject(AuthStorageService);
51     mgrModuleService = TestBed.inject(MgrModuleService);
52     notificationService = TestBed.inject(NotificationService);
53
54     isNotificationHiddenSpy = spyOn(component, 'isNotificationHidden').and.returnValue(false);
55     getPermissionsSpy = spyOn(authStorageService, 'getPermissions').and.returnValue(
56       configOptPermissions
57     );
58     getConfigSpy = spyOn(mgrModuleService, 'getConfig').and.returnValue(
59       of(telemetryDisabledConfig)
60     );
61   });
62
63   it('should create', () => {
64     fixture.detectChanges();
65     expect(component).toBeTruthy();
66   });
67
68   it('should not show notification again if the user closed it before', () => {
69     isNotificationHiddenSpy.and.returnValue(true);
70     fixture.detectChanges();
71     expect(component.displayNotification).toBe(false);
72   });
73
74   it('should not show notification for a user without configOpt permissions', () => {
75     getPermissionsSpy.and.returnValue(noConfigOptPermissions);
76     fixture.detectChanges();
77     expect(component.displayNotification).toBe(false);
78   });
79
80   it('should not show notification if the module is enabled already', () => {
81     getConfigSpy.and.returnValue(of(telemetryEnabledConfig));
82     fixture.detectChanges();
83     expect(component.displayNotification).toBe(false);
84   });
85
86   it('should show the notification if all pre-conditions set accordingly', () => {
87     fixture.detectChanges();
88     expect(component.displayNotification).toBe(true);
89   });
90
91   it('should hide the notification if the user closes it', () => {
92     spyOn(notificationService, 'show');
93     fixture.detectChanges();
94     component.close();
95     expect(notificationService.show).toHaveBeenCalled();
96     expect(localStorage.getItem('telemetry_notification_hidden')).toBe('true');
97   });
98
99   it('should hide the notification if the user logs out', () => {
100     const telemetryNotificationService = TestBed.inject(TelemetryNotificationService);
101     spyOn(telemetryNotificationService, 'setVisibility');
102     fixture.detectChanges();
103     component.ngOnDestroy();
104     expect(telemetryNotificationService.setVisibility).toHaveBeenCalledWith(false);
105   });
106 });