summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts
blob: 06bd198232dc6b7d0a1e94d22876756bddd0e56d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { configureTestBed } from '~/testing/unit-test-helper';
import { SettingsService } from './settings.service';

describe('SettingsService', () => {
  let service: SettingsService;
  let httpTesting: HttpTestingController;

  const exampleUrl = 'api/settings/something';
  const exampleValue = 'http://localhost:3000';

  configureTestBed({
    providers: [SettingsService],
    imports: [HttpClientTestingModule]
  });

  beforeEach(() => {
    service = TestBed.inject(SettingsService);
    httpTesting = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    httpTesting.verify();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should call validateGrafanaDashboardUrl', () => {
    service.validateGrafanaDashboardUrl('s').subscribe();
    const req = httpTesting.expectOne('api/grafana/validation/s');
    expect(req.request.method).toBe('GET');
  });

  describe('getSettingsValue', () => {
    const testMethod = (data: object, expected: string) => {
      expect(service['getSettingsValue'](data)).toBe(expected);
    };

    it('should explain the logic of the method', () => {
      expect('' || undefined).toBe(undefined);
      expect(undefined || '').toBe('');
      expect('test' || undefined || '').toBe('test');
    });

    it('should test the method for empty string values', () => {
      testMethod({}, '');
      testMethod({ wrongAttribute: 'test' }, '');
      testMethod({ value: '' }, '');
      testMethod({ instance: '' }, '');
    });

    it('should test the method for non empty string values', () => {
      testMethod({ value: 'test' }, 'test');
      testMethod({ instance: 'test' }, 'test');
    });
  });

  describe('isSettingConfigured', () => {
    let increment: number;

    const testConfig = (url: string, value: string) => {
      service.ifSettingConfigured(
        url,
        (setValue) => {
          expect(setValue).toBe(value);
          increment++;
        },
        () => {
          increment--;
        }
      );
    };

    const expectSettingsApiCall = (url: string, value: object, isSet: string) => {
      testConfig(url, isSet);
      const req = httpTesting.expectOne(url);
      expect(req.request.method).toBe('GET');
      req.flush(value);
      tick();
      expect(increment).toBe(isSet !== '' ? 1 : -1);
      expect(service['settings'][url]).toBe(isSet);
    };

    beforeEach(() => {
      increment = 0;
    });

    it(`should return true if 'value' does not contain an empty string`, fakeAsync(() => {
      expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
    }));

    it(`should return false if 'value' does contain an empty string`, fakeAsync(() => {
      expectSettingsApiCall(exampleUrl, { value: '' }, '');
    }));

    it(`should return true if 'instance' does not contain an empty string`, fakeAsync(() => {
      expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
    }));

    it(`should return false if 'instance' does contain an empty string`, fakeAsync(() => {
      expectSettingsApiCall(exampleUrl, { instance: '' }, '');
    }));

    it(`should return false if the api object is empty`, fakeAsync(() => {
      expectSettingsApiCall(exampleUrl, {}, '');
    }));

    it(`should call the API once even if it is called multiple times`, fakeAsync(() => {
      expectSettingsApiCall(exampleUrl, { value: exampleValue }, exampleValue);
      testConfig(exampleUrl, exampleValue);
      httpTesting.expectNone(exampleUrl);
      expect(increment).toBe(2);
    }));
  });

  it('should disable a set setting', () => {
    service['settings'] = { [exampleUrl]: exampleValue };
    service.disableSetting(exampleUrl);
    expect(service['settings']).toEqual({ [exampleUrl]: '' });
  });

  it('should return the specified settings (1)', () => {
    let result;
    service.getValues('foo,bar').subscribe((resp) => {
      result = resp;
    });
    const req = httpTesting.expectOne('api/settings?names=foo,bar');
    expect(req.request.method).toBe('GET');
    req.flush([
      { name: 'foo', default: '', type: 'str', value: 'test' },
      { name: 'bar', default: 0, type: 'int', value: 2 }
    ]);
    expect(result).toEqual({
      foo: 'test',
      bar: 2
    });
  });

  it('should return the specified settings (2)', () => {
    service.getValues(['abc', 'xyz']).subscribe();
    const req = httpTesting.expectOne('api/settings?names=abc,xyz');
    expect(req.request.method).toBe('GET');
  });

  it('should return standard settings', () => {
    service.getStandardSettings().subscribe();
    const req = httpTesting.expectOne('ui-api/standard_settings');
    expect(req.request.method).toBe('GET');
  });
});