1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import * as _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of as observableOf } from 'rxjs';
11 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { MgrModuleService } from '../../../shared/api/mgr-module.service';
13 import { TelemetryService } from '../../../shared/api/telemetry.service';
15 import { TextToDownloadService } from '../../../shared/services/text-to-download.service';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { TelemetryComponent } from './telemetry.component';
19 describe('TelemetryComponent', () => {
20 let component: TelemetryComponent;
21 let fixture: ComponentFixture<TelemetryComponent>;
22 let mgrModuleService: MgrModuleService;
23 let telemetryService: TelemetryService;
26 let httpTesting: HttpTestingController;
29 const optionsNames = [
43 'log_to_cluster_level',
51 declarations: [TelemetryComponent],
53 HttpClientTestingModule,
57 ToastrModule.forRoot()
59 providers: i18nProviders
62 describe('configForm', () => {
64 fixture = TestBed.createComponent(TelemetryComponent);
65 component = fixture.componentInstance;
66 mgrModuleService = TestBed.get(MgrModuleService);
69 optionsNames.forEach((name) => (options[name] = { name }));
70 optionsNames.forEach((name) => (configs[name] = true));
71 spyOn(mgrModuleService, 'getOptions').and.callFake(() => observableOf(options));
72 spyOn(mgrModuleService, 'getConfig').and.callFake(() => observableOf(configs));
73 fixture.detectChanges();
74 httpTesting = TestBed.get(HttpTestingController);
75 router = TestBed.get(Router);
76 spyOn(router, 'navigate');
79 it('should create', () => {
80 expect(component).toBeTruthy();
83 it('should set module enability to true correctly', () => {
84 expect(component.moduleEnabled).toBeTruthy();
87 it('should set module enability to false correctly', () => {
88 configs['enabled'] = false;
90 expect(component.moduleEnabled).toBeFalsy();
93 it('should filter options list correctly', () => {
94 _.forEach(Object.keys(component.options), (option) => {
95 expect(component.requiredFields).toContain(option);
99 it('should update the Telemetry configuration', () => {
100 component.updateConfig();
101 const req = httpTesting.expectOne('api/mgr/module/telemetry');
102 expect(req.request.method).toBe('PUT');
103 expect(req.request.body).toEqual({
109 it('should disable the Telemetry module', () => {
110 const message = 'Module disabled message.';
111 const followUpFunc = function () {
114 component.disableModule(message, followUpFunc);
115 const req = httpTesting.expectOne('api/telemetry');
116 expect(req.request.method).toBe('PUT');
117 expect(req.request.body).toEqual({
123 it('should disable the Telemetry module with default parameters', () => {
124 component.disableModule();
125 const req = httpTesting.expectOne('api/telemetry');
126 expect(req.request.method).toBe('PUT');
127 expect(req.request.body).toEqual({
131 expect(router.navigate).toHaveBeenCalledWith(['']);
135 describe('previewForm', () => {
142 fixture = TestBed.createComponent(TelemetryComponent);
143 component = fixture.componentInstance;
144 fixture.detectChanges();
145 telemetryService = TestBed.get(TelemetryService);
146 httpTesting = TestBed.get(HttpTestingController);
147 router = TestBed.get(Router);
148 spyOn(router, 'navigate');
151 it('should create', () => {
152 expect(component).toBeTruthy();
155 it('should call TextToDownloadService download function', () => {
156 spyOn(telemetryService, 'getReport').and.returnValue(observableOf(reportText));
157 component.ngOnInit();
159 const downloadSpy = spyOn(TestBed.get(TextToDownloadService), 'download');
160 const filename = 'reportText.json';
161 component.download(reportText, filename);
162 expect(downloadSpy).toHaveBeenCalledWith(JSON.stringify(reportText, null, 2), filename);
165 it('should submit', () => {
166 component.onSubmit();
167 const req = httpTesting.expectOne('api/telemetry');
168 expect(req.request.method).toBe('PUT');
169 expect(req.request.body).toEqual({
171 license_name: 'sharing-1-0'
174 expect(router.navigate).toHaveBeenCalledWith(['']);