]>
Commit | Line | Data |
---|---|---|
1 | import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
2 | ||
3 | import _ 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 | providers: [TaskManagerService, { provide: SummaryService, useClass: SummaryServiceMock }] | |
40 | }); | |
41 | ||
42 | beforeEach(() => { | |
43 | taskManagerService = TestBed.inject(TaskManagerService); | |
44 | summaryService = TestBed.inject(SummaryService); | |
45 | called = false; | |
46 | taskManagerService.subscribe('foo', {}, () => (called = true)); | |
47 | }); | |
48 | ||
49 | it('should be created', () => { | |
50 | expect(taskManagerService).toBeTruthy(); | |
51 | }); | |
52 | ||
53 | it('should subscribe and be notified when task is finished', fakeAsync(() => { | |
54 | expect(taskManagerService.subscriptions.length).toBe(1); | |
55 | summaryService.refresh(); | |
56 | tick(); | |
57 | taskManagerService.init(summaryService); | |
58 | expect(called).toEqual(true); | |
59 | expect(taskManagerService.subscriptions).toEqual([]); | |
60 | })); | |
61 | ||
62 | it('should subscribe and process executing taks', fakeAsync(() => { | |
63 | const original_subscriptions = _.cloneDeep(taskManagerService.subscriptions); | |
64 | _.assign(summary, { | |
65 | executing_tasks: [{ name: 'foo', metadata: {} }], | |
66 | finished_tasks: [] | |
67 | }); | |
68 | summaryService.refresh(); | |
69 | tick(); | |
70 | expect(taskManagerService.subscriptions).toEqual(original_subscriptions); | |
71 | })); | |
72 | }); |