1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
4 import { of } from 'rxjs';
6 import { RgwDaemon } from '~/app/ceph/rgw/models/rgw-daemon';
7 import { configureTestBed, RgwHelper } from '~/testing/unit-test-helper';
8 import { RgwDaemonService } from './rgw-daemon.service';
10 describe('RgwDaemonService', () => {
11 let service: RgwDaemonService;
12 let httpTesting: HttpTestingController;
13 let selectDaemonSpy: jasmine.Spy;
15 const daemonList = RgwHelper.getDaemonList();
16 const retrieveDaemonList = (reqDaemonList: RgwDaemon[], daemon: RgwDaemon) => {
18 .request((params) => of(params))
19 .subscribe((params) => expect(params.get('daemon_name')).toBe(daemon.id));
20 const listReq = httpTesting.expectOne('api/rgw/daemon');
21 listReq.flush(reqDaemonList);
23 expect(service['selectedDaemon'].getValue()).toEqual(daemon);
27 providers: [RgwDaemonService],
28 imports: [HttpClientTestingModule]
32 service = TestBed.inject(RgwDaemonService);
33 selectDaemonSpy = spyOn(service, 'selectDaemon').and.callThrough();
34 httpTesting = TestBed.inject(HttpTestingController);
41 it('should be created', () => {
42 expect(service).toBeTruthy();
45 it('should get daemon list', () => {
46 service.list().subscribe();
47 const req = httpTesting.expectOne('api/rgw/daemon');
48 req.flush(daemonList);
49 expect(req.request.method).toBe('GET');
50 expect(service['daemons'].getValue()).toEqual(daemonList);
53 it('should get daemon ', () => {
54 service.get('foo').subscribe();
55 const req = httpTesting.expectOne('api/rgw/daemon/foo');
56 expect(req.request.method).toBe('GET');
59 it('should call request and not select any daemon from empty daemon list', fakeAsync(() => {
60 expect(() => retrieveDaemonList([], null)).toThrowError('No RGW daemons found!');
61 expect(selectDaemonSpy).toHaveBeenCalledTimes(0);
64 it('should call request and select default daemon from daemon list', fakeAsync(() => {
65 retrieveDaemonList(daemonList, daemonList[1]);
66 expect(selectDaemonSpy).toHaveBeenCalledTimes(1);
67 expect(selectDaemonSpy).toHaveBeenCalledWith(daemonList[1]);
70 it('should call request and select first daemon from daemon list that has no default', fakeAsync(() => {
71 const noDefaultDaemonList = daemonList.map((daemon) => {
72 daemon.default = false;
75 retrieveDaemonList(noDefaultDaemonList, noDefaultDaemonList[0]);
76 expect(selectDaemonSpy).toHaveBeenCalledTimes(1);
77 expect(selectDaemonSpy).toHaveBeenCalledWith(noDefaultDaemonList[0]);