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 } from 'rxjs';
7 import { configureTestBed } from '../../../testing/unit-test-helper';
8 import { ExecutingTask } from '../models/executing-task';
9 import { AuthStorageService } from './auth-storage.service';
10 import { SummaryService } from './summary.service';
12 describe('SummaryService', () => {
13 let summaryService: SummaryService;
14 let authStorageService: AuthStorageService;
16 const summary: Record<string, any> = {
18 health_status: 'HEALTH_OK',
20 rbd_mirroring: { errors: 0, warnings: 0 },
22 have_mon_connection: true,
24 filesystems: [{ id: 1, name: 'cephfs_a' }]
27 const httpClientSpy = {
28 get: () => observableOf(summary)
32 imports: [RouterTestingModule],
36 { provide: HttpClient, useValue: httpClientSpy }
41 summaryService = TestBed.get(SummaryService);
42 authStorageService = TestBed.get(AuthStorageService);
45 it('should be created', () => {
46 expect(summaryService).toBeTruthy();
49 it('should call refresh', fakeAsync(() => {
50 summaryService.enablePolling();
51 authStorageService.set('foobar', undefined, undefined);
52 const calledWith: any[] = [];
53 summaryService.subscribe((data) => {
54 calledWith.push(data);
56 expect(calledWith).toEqual([summary]);
57 summaryService.refresh();
58 expect(calledWith).toEqual([summary, summary]);
60 expect(calledWith.length).toEqual(4);
61 // In order to not trigger setInterval again,
62 // which would raise 'Error: 1 timer(s) still in the queue.'
63 window.clearInterval(summaryService.polling);
66 describe('Should test methods after first refresh', () => {
68 authStorageService.set('foobar', undefined, undefined);
69 summaryService.refresh();
72 it('should call getCurrentSummary', () => {
73 expect(summaryService.getCurrentSummary()).toEqual(summary);
76 it('should call subscribe', () => {
78 const subscriber = summaryService.subscribe((data) => {
81 expect(subscriber).toEqual(jasmine.any(Subscriber));
82 expect(result).toEqual(summary);
85 it('should call addRunningTask', () => {
86 summaryService.addRunningTask(
87 new ExecutingTask('rbd/delete', {
88 pool_name: 'somePool',
89 image_name: 'someImage'
92 const result = summaryService.getCurrentSummary();
93 expect(result.executing_tasks.length).toBe(1);
94 expect(result.executing_tasks[0]).toEqual({
95 metadata: { image_name: 'someImage', pool_name: 'somePool' },
100 it('should call addRunningTask with duplicate task', () => {
101 let result = summaryService.getCurrentSummary();
102 const exec_task = new ExecutingTask('rbd/delete', {
103 pool_name: 'somePool',
104 image_name: 'someImage'
107 result.executing_tasks = [exec_task];
108 summaryService['summaryDataSource'].next(result);
109 result = summaryService.getCurrentSummary();
110 expect(result.executing_tasks.length).toBe(1);
112 summaryService.addRunningTask(exec_task);
113 result = summaryService.getCurrentSummary();
114 expect(result.executing_tasks.length).toBe(1);