1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { inject, TestBed } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { ToastrModule } from 'ngx-toastr';
6 import { Observable } from 'rxjs';
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';
16 describe('TaskWrapperService', () => {
17 let service: TaskWrapperService;
20 imports: [HttpClientTestingModule, ToastrModule.forRoot(), SharedModule, RouterTestingModule],
21 providers: [TaskWrapperService, i18nProviders]
24 beforeEach(inject([TaskWrapperService], (wrapper: TaskWrapperService) => {
28 it('should be created', () => {
29 expect(service).toBeTruthy();
32 describe('wrapTaskAroundCall', () => {
33 let notify: NotificationService;
35 let summaryService: SummaryService;
37 const fakeCall = (status?) =>
38 new Observable((observer) => {
40 observer.error({ error: 'failed' });
42 observer.next({ status: status });
46 const callWrapTaskAroundCall = (status, name) => {
47 return service.wrapTaskAroundCall({
48 task: new FinishedTask(name, { sth: 'else' }),
49 call: fakeCall(status)
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();
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();
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);
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) => {
82 callWrapTaskAroundCall(202, 'async').subscribe(null, null, () => (passed = true));
83 expect(notify.notifyTask).toHaveBeenCalled();
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();
92 * A notification will be raised by the API interceptor.
93 * This resolves this bug https://tracker.ceph.com/issues/25139
95 expect(notify.notifyTask).not.toHaveBeenCalled();