]>
Commit | Line | Data |
---|---|---|
e306af50 TL |
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'; | |
6 | ||
7 | import * as _ from 'lodash'; | |
8 | import { ToastrModule } from 'ngx-toastr'; | |
9 | import { of as observableOf } from 'rxjs'; | |
10 | ||
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'; | |
14 | ||
15 | import { TextToDownloadService } from '../../../shared/services/text-to-download.service'; | |
16 | import { SharedModule } from '../../../shared/shared.module'; | |
17 | import { TelemetryComponent } from './telemetry.component'; | |
18 | ||
19 | describe('TelemetryComponent', () => { | |
20 | let component: TelemetryComponent; | |
21 | let fixture: ComponentFixture<TelemetryComponent>; | |
22 | let mgrModuleService: MgrModuleService; | |
23 | let telemetryService: TelemetryService; | |
24 | let options: any; | |
25 | let configs: any; | |
26 | let httpTesting: HttpTestingController; | |
27 | let router: Router; | |
28 | ||
29 | const optionsNames = [ | |
30 | 'channel_basic', | |
31 | 'channel_crash', | |
32 | 'channel_device', | |
33 | 'channel_ident', | |
34 | 'contact', | |
35 | 'description', | |
36 | 'device_url', | |
37 | 'enabled', | |
38 | 'interval', | |
39 | 'last_opt_revision', | |
40 | 'leaderboard', | |
41 | 'log_level', | |
42 | 'log_to_cluster', | |
43 | 'log_to_cluster_level', | |
44 | 'log_to_file', | |
45 | 'organization', | |
46 | 'proxy', | |
47 | 'url' | |
48 | ]; | |
49 | ||
f6b5b4d7 TL |
50 | configureTestBed({ |
51 | declarations: [TelemetryComponent], | |
52 | imports: [ | |
53 | HttpClientTestingModule, | |
54 | ReactiveFormsModule, | |
55 | RouterTestingModule, | |
56 | SharedModule, | |
57 | ToastrModule.forRoot() | |
58 | ], | |
59 | providers: i18nProviders | |
60 | }); | |
e306af50 TL |
61 | |
62 | describe('configForm', () => { | |
63 | beforeEach(() => { | |
64 | fixture = TestBed.createComponent(TelemetryComponent); | |
65 | component = fixture.componentInstance; | |
66 | mgrModuleService = TestBed.get(MgrModuleService); | |
67 | options = {}; | |
68 | configs = {}; | |
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'); | |
77 | }); | |
78 | ||
79 | it('should create', () => { | |
80 | expect(component).toBeTruthy(); | |
81 | }); | |
82 | ||
83 | it('should set module enability to true correctly', () => { | |
84 | expect(component.moduleEnabled).toBeTruthy(); | |
85 | }); | |
86 | ||
87 | it('should set module enability to false correctly', () => { | |
88 | configs['enabled'] = false; | |
89 | component.ngOnInit(); | |
90 | expect(component.moduleEnabled).toBeFalsy(); | |
91 | }); | |
92 | ||
93 | it('should filter options list correctly', () => { | |
94 | _.forEach(Object.keys(component.options), (option) => { | |
95 | expect(component.requiredFields).toContain(option); | |
96 | }); | |
97 | }); | |
98 | ||
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({ | |
104 | config: {} | |
105 | }); | |
106 | req.flush({}); | |
107 | }); | |
108 | ||
109 | it('should disable the Telemetry module', () => { | |
110 | const message = 'Module disabled message.'; | |
111 | const followUpFunc = function () { | |
112 | return 'followUp'; | |
113 | }; | |
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({ | |
118 | enable: false | |
119 | }); | |
120 | req.flush({}); | |
121 | }); | |
122 | ||
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({ | |
128 | enable: false | |
129 | }); | |
130 | req.flush({}); | |
131 | expect(router.navigate).toHaveBeenCalledWith(['']); | |
132 | }); | |
133 | }); | |
134 | ||
135 | describe('previewForm', () => { | |
136 | const reportText = { | |
137 | testA: 'testA', | |
138 | testB: 'testB' | |
139 | }; | |
140 | ||
141 | beforeEach(() => { | |
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'); | |
149 | }); | |
150 | ||
151 | it('should create', () => { | |
152 | expect(component).toBeTruthy(); | |
153 | }); | |
154 | ||
155 | it('should call TextToDownloadService download function', () => { | |
156 | spyOn(telemetryService, 'getReport').and.returnValue(observableOf(reportText)); | |
157 | component.ngOnInit(); | |
158 | ||
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); | |
163 | }); | |
164 | ||
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({ | |
170 | enable: true, | |
171 | license_name: 'sharing-1-0' | |
172 | }); | |
173 | req.flush({}); | |
174 | expect(router.navigate).toHaveBeenCalledWith(['']); | |
175 | }); | |
176 | }); | |
177 | }); |