1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';
5 import { ToastrModule } from 'ngx-toastr';
6 import { of } from 'rxjs';
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';
18 describe('TelemetryActivationNotificationComponent', () => {
19 let component: TelemetryNotificationComponent;
20 let fixture: ComponentFixture<TelemetryNotificationComponent>;
22 let authStorageService: AuthStorageService;
23 let mgrModuleService: MgrModuleService;
24 let notificationService: NotificationService;
26 let isNotificationHiddenSpy: jasmine.Spy;
27 let getPermissionsSpy: jasmine.Spy;
28 let getConfigSpy: jasmine.Spy;
30 const configOptPermissions: Permissions = new Permissions({
31 'config-opt': ['read', 'create', 'update', 'delete']
33 const noConfigOptPermissions: Permissions = new Permissions({});
34 const telemetryEnabledConfig = {
37 const telemetryDisabledConfig = {
42 declarations: [TelemetryNotificationComponent],
43 imports: [NgbAlertModule, HttpClientTestingModule, ToastrModule.forRoot(), PipesModule],
44 providers: [MgrModuleService, UserService]
48 fixture = TestBed.createComponent(TelemetryNotificationComponent);
49 component = fixture.componentInstance;
50 authStorageService = TestBed.inject(AuthStorageService);
51 mgrModuleService = TestBed.inject(MgrModuleService);
52 notificationService = TestBed.inject(NotificationService);
54 isNotificationHiddenSpy = spyOn(component, 'isNotificationHidden').and.returnValue(false);
55 getPermissionsSpy = spyOn(authStorageService, 'getPermissions').and.returnValue(
58 getConfigSpy = spyOn(mgrModuleService, 'getConfig').and.returnValue(
59 of(telemetryDisabledConfig)
63 it('should create', () => {
64 fixture.detectChanges();
65 expect(component).toBeTruthy();
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);
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);
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);
86 it('should show the notification if all pre-conditions set accordingly', () => {
87 fixture.detectChanges();
88 expect(component.displayNotification).toBe(true);
91 it('should hide the notification if the user closes it', () => {
92 spyOn(notificationService, 'show');
93 fixture.detectChanges();
95 expect(notificationService.show).toHaveBeenCalled();
96 expect(localStorage.getItem('telemetry_notification_hidden')).toBe('true');
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);