]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / settings.service.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3
4 import { configureTestBed } from '../../../testing/unit-test-helper';
5 import { SettingsService } from './settings.service';
6
7 describe('SettingsService', () => {
8 let service: SettingsService;
9 let httpTesting: HttpTestingController;
10
11 const exampleUrl = 'api/settings/something';
12 const exampleValue = 'http://localhost:3000';
13
14 configureTestBed(
15 {
16 providers: [SettingsService],
17 imports: [HttpClientTestingModule]
18 },
19 true
20 );
21
22 beforeEach(() => {
23 service = TestBed.get(SettingsService);
24 httpTesting = TestBed.get(HttpTestingController);
25 });
26
27 afterEach(() => {
28 httpTesting.verify();
29 });
30
31 it('should be created', () => {
32 expect(service).toBeTruthy();
33 });
34
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');
39 });
40
41 describe('getSettingsValue', () => {
42 const testMethod = (data, expected: string) => {
43 expect(service['getSettingsValue'](data)).toBe(expected);
44 };
45
46 it('should explain the logic of the method', () => {
47 expect('' || undefined).toBe(undefined);
48 expect(undefined || '').toBe('');
49 expect('test' || undefined || '').toBe('test');
50 });
51
52 it('should test the method for empty string values', () => {
53 testMethod({}, '');
54 testMethod({ wrongAttribute: 'test' }, '');
55 testMethod({ value: '' }, '');
56 testMethod({ instance: '' }, '');
57 });
58
59 it('should test the method for non empty string values', () => {
60 testMethod({ value: 'test' }, 'test');
61 testMethod({ instance: 'test' }, 'test');
62 });
63 });
64
65 describe('isSettingConfigured', () => {
66 let increment: number;
67
68 const testConfig = (url, value) => {
69 service.ifSettingConfigured(
70 url,
71 (setValue) => {
72 expect(setValue).toBe(value);
73 increment++;
74 },
75 () => {
76 increment--;
77 }
78 );
79 };
80
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');
85 req.flush(value);
86 tick();
87 expect(increment).toBe(isSet !== '' ? 1 : -1);
88 expect(service['settings'][url]).toBe(isSet);
89 };
90
91 beforeEach(() => {
92 increment = 0;
93 });
94
95 it(`should return true if 'value' does not contain an empty string`, fakeAsync(() => {
96 expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
97 }));
98
99 it(`should return false if 'value' does contain an empty string`, fakeAsync(() => {
100 expectSettingsApiCall(exampleUrl, { value: '' }, '');
101 }));
102
103 it(`should return true if 'instance' does not contain an empty string`, fakeAsync(() => {
104 expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
105 }));
106
107 it(`should return false if 'instance' does contain an empty string`, fakeAsync(() => {
108 expectSettingsApiCall(exampleUrl, { instance: '' }, '');
109 }));
110
111 it(`should return false if the api object is empty`, fakeAsync(() => {
112 expectSettingsApiCall(exampleUrl, {}, '');
113 }));
114
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);
120 }));
121 });
122
123 it('should disable a set setting', () => {
124 service['settings'] = { [exampleUrl]: exampleValue };
125 service.disableSetting(exampleUrl);
126 expect(service['settings']).toEqual({ [exampleUrl]: '' });
127 });
128 });