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',
52 declarations: [TelemetryComponent],
54 HttpClientTestingModule,
58 ToastrModule.forRoot()
60 providers: i18nProviders
65 describe('configForm', () => {
67 fixture = TestBed.createComponent(TelemetryComponent);
68 component = fixture.componentInstance;
69 mgrModuleService = TestBed.get(MgrModuleService);
72 optionsNames.forEach((name) => (options[name] = { name }));
73 optionsNames.forEach((name) => (configs[name] = true));
74 spyOn(mgrModuleService, 'getOptions').and.callFake(() => observableOf(options));
75 spyOn(mgrModuleService, 'getConfig').and.callFake(() => observableOf(configs));
76 fixture.detectChanges();
77 httpTesting = TestBed.get(HttpTestingController);
78 router = TestBed.get(Router);
79 spyOn(router, 'navigate');
82 it('should create', () => {
83 expect(component).toBeTruthy();
86 it('should set module enability to true correctly', () => {
87 expect(component.moduleEnabled).toBeTruthy();
90 it('should set module enability to false correctly', () => {
91 configs['enabled'] = false;
93 expect(component.moduleEnabled).toBeFalsy();
96 it('should filter options list correctly', () => {
97 _.forEach(Object.keys(component.options), (option) => {
98 expect(component.requiredFields).toContain(option);
102 it('should update the Telemetry configuration', () => {
103 component.updateConfig();
104 const req = httpTesting.expectOne('api/mgr/module/telemetry');
105 expect(req.request.method).toBe('PUT');
106 expect(req.request.body).toEqual({
112 it('should disable the Telemetry module', () => {
113 const message = 'Module disabled message.';
114 const followUpFunc = function () {
117 component.disableModule(message, followUpFunc);
118 const req = httpTesting.expectOne('api/telemetry');
119 expect(req.request.method).toBe('PUT');
120 expect(req.request.body).toEqual({
126 it('should disable the Telemetry module with default parameters', () => {
127 component.disableModule();
128 const req = httpTesting.expectOne('api/telemetry');
129 expect(req.request.method).toBe('PUT');
130 expect(req.request.body).toEqual({
134 expect(router.navigate).toHaveBeenCalledWith(['']);
138 describe('previewForm', () => {
145 fixture = TestBed.createComponent(TelemetryComponent);
146 component = fixture.componentInstance;
147 fixture.detectChanges();
148 telemetryService = TestBed.get(TelemetryService);
149 httpTesting = TestBed.get(HttpTestingController);
150 router = TestBed.get(Router);
151 spyOn(router, 'navigate');
154 it('should create', () => {
155 expect(component).toBeTruthy();
158 it('should call TextToDownloadService download function', () => {
159 spyOn(telemetryService, 'getReport').and.returnValue(observableOf(reportText));
160 component.ngOnInit();
162 const downloadSpy = spyOn(TestBed.get(TextToDownloadService), 'download');
163 const filename = 'reportText.json';
164 component.download(reportText, filename);
165 expect(downloadSpy).toHaveBeenCalledWith(JSON.stringify(reportText, null, 2), filename);
168 it('should submit', () => {
169 component.onSubmit();
170 const req = httpTesting.expectOne('api/telemetry');
171 expect(req.request.method).toBe('PUT');
172 expect(req.request.body).toEqual({
174 license_name: 'sharing-1-0'
177 expect(router.navigate).toHaveBeenCalledWith(['']);