]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-manager.service.spec.ts
235d001f12e4a5fc170bbfe05575c329f2f58e12
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / task-manager.service.spec.ts
1 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
2
3 import * as _ from 'lodash';
4 import { BehaviorSubject } from 'rxjs';
5
6 import { configureTestBed } from '../../../testing/unit-test-helper';
7 import { SummaryService } from './summary.service';
8 import { TaskManagerService } from './task-manager.service';
9
10 const summary: Record<string, any> = {
11 executing_tasks: [],
12 health_status: 'HEALTH_OK',
13 mgr_id: 'x',
14 rbd_mirroring: { errors: 0, warnings: 0 },
15 rbd_pools: [],
16 have_mon_connection: true,
17 finished_tasks: [{ name: 'foo', metadata: {} }],
18 filesystems: [{ id: 1, name: 'cephfs_a' }]
19 };
20
21 export class SummaryServiceMock {
22 summaryDataSource = new BehaviorSubject(summary);
23 summaryData$ = this.summaryDataSource.asObservable();
24
25 refresh() {
26 this.summaryDataSource.next(summary);
27 }
28 subscribe(call: any) {
29 return this.summaryData$.subscribe(call);
30 }
31 }
32
33 describe('TaskManagerService', () => {
34 let taskManagerService: TaskManagerService;
35 let summaryService: any;
36 let called: boolean;
37
38 configureTestBed(
39 {
40 providers: [TaskManagerService, { provide: SummaryService, useClass: SummaryServiceMock }]
41 },
42 true
43 );
44
45 beforeEach(() => {
46 taskManagerService = TestBed.get(TaskManagerService);
47 summaryService = TestBed.get(SummaryService);
48 called = false;
49 taskManagerService.subscribe('foo', {}, () => (called = true));
50 });
51
52 it('should be created', () => {
53 expect(taskManagerService).toBeTruthy();
54 });
55
56 it('should subscribe and be notified when task is finished', fakeAsync(() => {
57 expect(taskManagerService.subscriptions.length).toBe(1);
58 summaryService.refresh();
59 tick();
60 expect(called).toEqual(true);
61 expect(taskManagerService.subscriptions).toEqual([]);
62 }));
63
64 it('should subscribe and process executing taks', fakeAsync(() => {
65 const original_subscriptions = _.cloneDeep(taskManagerService.subscriptions);
66 _.assign(summary, {
67 executing_tasks: [{ name: 'foo', metadata: {} }],
68 finished_tasks: []
69 });
70 summaryService.refresh();
71 tick();
72 expect(taskManagerService.subscriptions).toEqual(original_subscriptions);
73 }));
74 });