1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
4 import { configureTestBed } from '../../../testing/unit-test-helper';
5 import { SettingsService } from './settings.service';
7 describe('SettingsService', () => {
8 let service: SettingsService;
9 let httpTesting: HttpTestingController;
11 const exampleUrl = 'api/settings/something';
12 const exampleValue = 'http://localhost:3000';
16 providers: [SettingsService],
17 imports: [HttpClientTestingModule]
23 service = TestBed.get(SettingsService);
24 httpTesting = TestBed.get(HttpTestingController);
31 it('should be created', () => {
32 expect(service).toBeTruthy();
35 it('should call validateGrafanaDashboardUrl', () => {
36 service.validateGrafanaDashboardUrl('s').subscribe();
37 const req = httpTesting.expectOne('api/grafana/validation/s');
38 expect(req.request.method).toBe('GET');
41 describe('getSettingsValue', () => {
42 const testMethod = (data: object, expected: string) => {
43 expect(service['getSettingsValue'](data)).toBe(expected);
46 it('should explain the logic of the method', () => {
47 expect('' || undefined).toBe(undefined);
48 expect(undefined || '').toBe('');
49 expect('test' || undefined || '').toBe('test');
52 it('should test the method for empty string values', () => {
54 testMethod({ wrongAttribute: 'test' }, '');
55 testMethod({ value: '' }, '');
56 testMethod({ instance: '' }, '');
59 it('should test the method for non empty string values', () => {
60 testMethod({ value: 'test' }, 'test');
61 testMethod({ instance: 'test' }, 'test');
65 describe('isSettingConfigured', () => {
66 let increment: number;
68 const testConfig = (url: string, value: string) => {
69 service.ifSettingConfigured(
72 expect(setValue).toBe(value);
81 const expectSettingsApiCall = (url: string, value: object, isSet: string) => {
82 testConfig(url, isSet);
83 const req = httpTesting.expectOne(url);
84 expect(req.request.method).toBe('GET');
87 expect(increment).toBe(isSet !== '' ? 1 : -1);
88 expect(service['settings'][url]).toBe(isSet);
95 it(`should return true if 'value' does not contain an empty string`, fakeAsync(() => {
96 expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
99 it(`should return false if 'value' does contain an empty string`, fakeAsync(() => {
100 expectSettingsApiCall(exampleUrl, { value: '' }, '');
103 it(`should return true if 'instance' does not contain an empty string`, fakeAsync(() => {
104 expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
107 it(`should return false if 'instance' does contain an empty string`, fakeAsync(() => {
108 expectSettingsApiCall(exampleUrl, { instance: '' }, '');
111 it(`should return false if the api object is empty`, fakeAsync(() => {
112 expectSettingsApiCall(exampleUrl, {}, '');
115 it(`should call the API once even if it is called multiple times`, fakeAsync(() => {
116 expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
117 testConfig(exampleUrl, exampleValue);
118 httpTesting.expectNone(exampleUrl);
119 expect(increment).toBe(2);
123 it('should disable a set setting', () => {
124 service['settings'] = { [exampleUrl]: exampleValue };
125 service.disableSetting(exampleUrl);
126 expect(service['settings']).toEqual({ [exampleUrl]: '' });
129 it('should return the specified settings (1)', () => {
131 service.getValues('foo,bar').subscribe((resp) => {
134 const req = httpTesting.expectOne('api/settings?names=foo,bar');
135 expect(req.request.method).toBe('GET');
137 { name: 'foo', default: '', type: 'str', value: 'test' },
138 { name: 'bar', default: 0, type: 'int', value: 2 }
140 expect(result).toEqual({
146 it('should return the specified settings (2)', () => {
147 service.getValues(['abc', 'xyz']).subscribe();
148 const req = httpTesting.expectOne('api/settings?names=abc,xyz');
149 expect(req.request.method).toBe('GET');
152 it('should return standard settings', () => {
153 service.getStandardSettings().subscribe();
154 const req = httpTesting.expectOne('ui-api/standard_settings');
155 expect(req.request.method).toBe('GET');