]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { inject, TestBed } from '@angular/core/testing'; | |
3 | import { RouterTestingModule } from '@angular/router/testing'; | |
4 | ||
494da23a | 5 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
6 | import { Observable } from 'rxjs'; |
7 | ||
8 | import { configureTestBed, i18nProviders } from '../../../testing/unit-test-helper'; | |
9 | import { FinishedTask } from '../models/finished-task'; | |
10 | import { SharedModule } from '../shared.module'; | |
11 | import { NotificationService } from './notification.service'; | |
12 | import { SummaryService } from './summary.service'; | |
13 | import { TaskManagerService } from './task-manager.service'; | |
14 | import { TaskWrapperService } from './task-wrapper.service'; | |
15 | ||
16 | describe('TaskWrapperService', () => { | |
17 | let service: TaskWrapperService; | |
18 | ||
19 | configureTestBed({ | |
494da23a | 20 | imports: [HttpClientTestingModule, ToastrModule.forRoot(), SharedModule, RouterTestingModule], |
11fdf7f2 TL |
21 | providers: [TaskWrapperService, i18nProviders] |
22 | }); | |
23 | ||
24 | beforeEach(inject([TaskWrapperService], (wrapper: TaskWrapperService) => { | |
25 | service = wrapper; | |
26 | })); | |
27 | ||
28 | it('should be created', () => { | |
29 | expect(service).toBeTruthy(); | |
30 | }); | |
31 | ||
32 | describe('wrapTaskAroundCall', () => { | |
33 | let notify: NotificationService; | |
34 | let passed: boolean; | |
35 | let summaryService: SummaryService; | |
36 | ||
9f95a23c | 37 | const fakeCall = (status?: number) => |
11fdf7f2 TL |
38 | new Observable((observer) => { |
39 | if (!status) { | |
40 | observer.error({ error: 'failed' }); | |
41 | } | |
42 | observer.next({ status: status }); | |
43 | observer.complete(); | |
44 | }); | |
45 | ||
9f95a23c | 46 | const callWrapTaskAroundCall = (status: number, name: string) => { |
11fdf7f2 TL |
47 | return service.wrapTaskAroundCall({ |
48 | task: new FinishedTask(name, { sth: 'else' }), | |
49 | call: fakeCall(status) | |
50 | }); | |
51 | }; | |
52 | ||
53 | beforeEach(() => { | |
54 | passed = false; | |
55 | notify = TestBed.get(NotificationService); | |
56 | summaryService = TestBed.get(SummaryService); | |
57 | spyOn(notify, 'show'); | |
58 | spyOn(notify, 'notifyTask').and.stub(); | |
59 | spyOn(service, '_handleExecutingTasks').and.callThrough(); | |
60 | spyOn(summaryService, 'addRunningTask').and.callThrough(); | |
61 | }); | |
62 | ||
63 | it('should simulate a synchronous task', () => { | |
64 | callWrapTaskAroundCall(200, 'sync').subscribe(null, null, () => (passed = true)); | |
65 | expect(service._handleExecutingTasks).not.toHaveBeenCalled(); | |
66 | expect(passed).toBeTruthy(); | |
67 | expect(summaryService.addRunningTask).not.toHaveBeenCalled(); | |
68 | }); | |
69 | ||
70 | it('should simulate a asynchronous task', () => { | |
71 | callWrapTaskAroundCall(202, 'async').subscribe(null, null, () => (passed = true)); | |
72 | expect(service._handleExecutingTasks).toHaveBeenCalled(); | |
73 | expect(passed).toBeTruthy(); | |
74 | expect(summaryService.addRunningTask).toHaveBeenCalledTimes(1); | |
75 | }); | |
76 | ||
77 | it('should call notifyTask if asynchronous task would have been finished', () => { | |
78 | const taskManager = TestBed.get(TaskManagerService); | |
79 | spyOn(taskManager, 'subscribe').and.callFake((_name, _metadata, onTaskFinished) => { | |
80 | onTaskFinished(); | |
81 | }); | |
82 | callWrapTaskAroundCall(202, 'async').subscribe(null, null, () => (passed = true)); | |
83 | expect(notify.notifyTask).toHaveBeenCalled(); | |
84 | }); | |
85 | ||
86 | it('should simulate a task failure', () => { | |
87 | callWrapTaskAroundCall(null, 'async').subscribe(null, () => (passed = true), null); | |
88 | expect(service._handleExecutingTasks).not.toHaveBeenCalled(); | |
89 | expect(passed).toBeTruthy(); | |
90 | expect(summaryService.addRunningTask).not.toHaveBeenCalled(); | |
91 | /** | |
92 | * A notification will be raised by the API interceptor. | |
93 | * This resolves this bug https://tracker.ceph.com/issues/25139 | |
94 | */ | |
95 | expect(notify.notifyTask).not.toHaveBeenCalled(); | |
96 | }); | |
97 | }); | |
98 | }); |