summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/components/telemetry-notification/telemetry-notification.component.spec.ts
blob: e946e79d87e6bde8789dfcba1059b864382a199d (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';
import { of } from 'rxjs';

import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
import { UserService } from '~/app/shared/api/user.service';
import { AlertPanelComponent } from '~/app/shared/components/alert-panel/alert-panel.component';
import { Permissions } from '~/app/shared/models/permissions';
import { PipesModule } from '~/app/shared/pipes/pipes.module';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
import { NotificationService } from '~/app/shared/services/notification.service';
import { TelemetryNotificationService } from '~/app/shared/services/telemetry-notification.service';
import { configureTestBed } from '~/testing/unit-test-helper';
import { TelemetryNotificationComponent } from './telemetry-notification.component';

describe('TelemetryActivationNotificationComponent', () => {
  let component: TelemetryNotificationComponent;
  let fixture: ComponentFixture<TelemetryNotificationComponent>;

  let authStorageService: AuthStorageService;
  let mgrModuleService: MgrModuleService;
  let notificationService: NotificationService;

  let isNotificationHiddenSpy: jasmine.Spy;
  let getPermissionsSpy: jasmine.Spy;
  let getConfigSpy: jasmine.Spy;

  const configOptPermissions: Permissions = new Permissions({
    'config-opt': ['read', 'create', 'update', 'delete']
  });
  const noConfigOptPermissions: Permissions = new Permissions({});
  const telemetryEnabledConfig = {
    enabled: true
  };
  const telemetryDisabledConfig = {
    enabled: false
  };

  configureTestBed({
    declarations: [TelemetryNotificationComponent, AlertPanelComponent],
    imports: [NgbAlertModule, HttpClientTestingModule, ToastrModule.forRoot(), PipesModule],
    providers: [MgrModuleService, UserService]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(TelemetryNotificationComponent);
    component = fixture.componentInstance;
    authStorageService = TestBed.inject(AuthStorageService);
    mgrModuleService = TestBed.inject(MgrModuleService);
    notificationService = TestBed.inject(NotificationService);

    isNotificationHiddenSpy = spyOn(component, 'isNotificationHidden').and.returnValue(false);
    getPermissionsSpy = spyOn(authStorageService, 'getPermissions').and.returnValue(
      configOptPermissions
    );
    getConfigSpy = spyOn(mgrModuleService, 'getConfig').and.returnValue(
      of(telemetryDisabledConfig)
    );
  });

  it('should create', () => {
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });

  it('should not show notification again if the user closed it before', () => {
    isNotificationHiddenSpy.and.returnValue(true);
    fixture.detectChanges();
    expect(component.displayNotification).toBe(false);
  });

  it('should not show notification for a user without configOpt permissions', () => {
    getPermissionsSpy.and.returnValue(noConfigOptPermissions);
    fixture.detectChanges();
    expect(component.displayNotification).toBe(false);
  });

  it('should not show notification if the module is enabled already', () => {
    getConfigSpy.and.returnValue(of(telemetryEnabledConfig));
    fixture.detectChanges();
    expect(component.displayNotification).toBe(false);
  });

  it('should show the notification if all pre-conditions set accordingly', () => {
    fixture.detectChanges();
    expect(component.displayNotification).toBe(true);
  });

  it('should hide the notification if the user closes it', () => {
    spyOn(notificationService, 'show');
    fixture.detectChanges();
    component.onDismissed();
    expect(notificationService.show).toHaveBeenCalled();
    expect(localStorage.getItem('telemetry_notification_hidden')).toBe('true');
  });

  it('should hide the notification if the user logs out', () => {
    const telemetryNotificationService = TestBed.inject(TelemetryNotificationService);
    spyOn(telemetryNotificationService, 'setVisibility');
    fixture.detectChanges();
    component.ngOnDestroy();
    expect(telemetryNotificationService.setVisibility).toHaveBeenCalledWith(false);
  });
});