summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-wrapper.service.spec.ts
blob: e819622118ec0a40f80fbd419e9bc8fe27201373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { inject, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { ToastrModule } from 'ngx-toastr';
import { Observable } from 'rxjs';

import { configureTestBed } from '~/testing/unit-test-helper';
import { FinishedTask } from '../models/finished-task';
import { SharedModule } from '../shared.module';
import { NotificationService } from './notification.service';
import { SummaryService } from './summary.service';
import { TaskManagerService } from './task-manager.service';
import { TaskWrapperService } from './task-wrapper.service';

describe('TaskWrapperService', () => {
  let service: TaskWrapperService;

  configureTestBed({
    imports: [HttpClientTestingModule, ToastrModule.forRoot(), SharedModule, RouterTestingModule],
    providers: [TaskWrapperService]
  });

  beforeEach(inject([TaskWrapperService], (wrapper: TaskWrapperService) => {
    service = wrapper;
  }));

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  describe('wrapTaskAroundCall', () => {
    let notify: NotificationService;
    let passed: boolean;
    let summaryService: SummaryService;

    const fakeCall = (status?: number) =>
      new Observable((observer) => {
        if (!status) {
          observer.error({ error: 'failed' });
        }
        observer.next({ status: status });
        observer.complete();
      });

    const callWrapTaskAroundCall = (status: number, name: string) => {
      return service.wrapTaskAroundCall({
        task: new FinishedTask(name, { sth: 'else' }),
        call: fakeCall(status)
      });
    };

    beforeEach(() => {
      passed = false;
      notify = TestBed.inject(NotificationService);
      summaryService = TestBed.inject(SummaryService);
      spyOn(notify, 'show');
      spyOn(notify, 'notifyTask').and.stub();
      spyOn(service, '_handleExecutingTasks').and.callThrough();
      spyOn(summaryService, 'addRunningTask').and.callThrough();
    });

    it('should simulate a synchronous task', () => {
      callWrapTaskAroundCall(200, 'sync').subscribe({ complete: () => (passed = true) });
      expect(service._handleExecutingTasks).not.toHaveBeenCalled();
      expect(passed).toBeTruthy();
      expect(summaryService.addRunningTask).not.toHaveBeenCalled();
    });

    it('should simulate a asynchronous task', () => {
      callWrapTaskAroundCall(202, 'async').subscribe({ complete: () => (passed = true) });
      expect(service._handleExecutingTasks).toHaveBeenCalled();
      expect(passed).toBeTruthy();
      expect(summaryService.addRunningTask).toHaveBeenCalledTimes(1);
    });

    it('should call notifyTask if asynchronous task would have been finished', () => {
      const taskManager = TestBed.inject(TaskManagerService);
      spyOn(taskManager, 'subscribe').and.callFake((_name, _metadata, onTaskFinished) => {
        onTaskFinished();
      });
      callWrapTaskAroundCall(202, 'async').subscribe({ complete: () => (passed = true) });
      expect(notify.notifyTask).toHaveBeenCalled();
    });

    it('should simulate a task failure', () => {
      callWrapTaskAroundCall(null, 'async').subscribe({ error: () => (passed = true) });
      expect(service._handleExecutingTasks).not.toHaveBeenCalled();
      expect(passed).toBeTruthy();
      expect(summaryService.addRunningTask).not.toHaveBeenCalled();
      /**
       * A notification will be raised by the API interceptor.
       * This resolves this bug https://tracker.ceph.com/issues/25139
       */
      expect(notify.notifyTask).not.toHaveBeenCalled();
    });
  });
});