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