1 import { HttpClient } from '@angular/common/http';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { of as observableOf, Subscriber, Subscription } from 'rxjs';
7 import { configureTestBed } from '../../../testing/unit-test-helper';
8 import { ExecutingTask } from '../models/executing-task';
9 import { Summary } from '../models/summary.model';
10 import { AuthStorageService } from './auth-storage.service';
11 import { SummaryService } from './summary.service';
13 describe('SummaryService', () => {
14 let summaryService: SummaryService;
15 let authStorageService: AuthStorageService;
16 let subs: Subscription;
18 const summary: Summary = {
20 health_status: 'HEALTH_OK',
22 rbd_mirroring: { errors: 0, warnings: 0 },
24 have_mon_connection: true,
26 filesystems: [{ id: 1, name: 'cephfs_a' }]
29 const httpClientSpy = {
30 get: () => observableOf(summary)
33 const nextSummary = (newData: any) => summaryService['summaryDataSource'].next(newData);
36 imports: [RouterTestingModule],
40 { provide: HttpClient, useValue: httpClientSpy }
45 summaryService = TestBed.get(SummaryService);
46 authStorageService = TestBed.get(AuthStorageService);
49 it('should be created', () => {
50 expect(summaryService).toBeTruthy();
53 it('should call refresh', fakeAsync(() => {
54 authStorageService.set('foobar', undefined, undefined);
55 const calledWith: any[] = [];
56 subs = new Subscription();
57 subs.add(summaryService.startPolling());
60 summaryService.subscribe((data) => {
61 calledWith.push(data);
64 expect(calledWith).toEqual([summary]);
65 subs.add(summaryService.refresh());
66 expect(calledWith).toEqual([summary, summary]);
67 tick(summaryService.REFRESH_INTERVAL * 2);
68 expect(calledWith.length).toEqual(4);
72 describe('Should test subscribe without initial value', () => {
76 const callback = (response: Summary) => {
84 nextSummary(undefined);
87 it('should call subscribeOnce', () => {
88 const subscriber = summaryService.subscribeOnce(callback);
90 expect(subscriber).toEqual(jasmine.any(Subscriber));
92 expect(result).toEqual(undefined);
94 nextSummary(undefined);
96 expect(result).toEqual(undefined);
97 expect(subscriber.closed).toBe(false);
100 expect(result).toEqual(summary);
102 expect(subscriber.closed).toBe(true);
104 nextSummary(summary);
105 expect(result).toEqual(summary);
109 it('should call subscribe', () => {
110 const subscriber = summaryService.subscribe(callback);
112 expect(subscriber).toEqual(jasmine.any(Subscriber));
114 expect(result).toEqual(undefined);
116 nextSummary(undefined);
118 expect(result).toEqual(undefined);
119 expect(subscriber.closed).toBe(false);
121 nextSummary(summary);
122 expect(result).toEqual(summary);
124 expect(subscriber.closed).toBe(false);
126 nextSummary(summary);
127 expect(result).toEqual(summary);
129 expect(subscriber.closed).toBe(false);
133 describe('Should test methods after first refresh', () => {
135 authStorageService.set('foobar', undefined, undefined);
136 summaryService.refresh();
139 it('should call addRunningTask', () => {
140 summaryService.addRunningTask(
141 new ExecutingTask('rbd/delete', {
142 pool_name: 'somePool',
143 image_name: 'someImage'
147 summaryService.subscribeOnce((response) => {
151 expect(result.executing_tasks.length).toBe(1);
152 expect(result.executing_tasks[0]).toEqual({
153 metadata: { image_name: 'someImage', pool_name: 'somePool' },
158 it('should call addRunningTask with duplicate task', () => {
160 summaryService.subscribe((response) => {
164 const exec_task = new ExecutingTask('rbd/delete', {
165 pool_name: 'somePool',
166 image_name: 'someImage'
169 result.executing_tasks = [exec_task];
172 expect(result.executing_tasks.length).toBe(1);
174 summaryService.addRunningTask(exec_task);
176 expect(result.executing_tasks.length).toBe(1);