]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
925d5fcfa7101b14e1f647708b2dcb929502a7c0
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { ToastrModule } from 'ngx-toastr';
6 import { of } from 'rxjs';
7
8 import { By } from '@angular/platform-browser';
9 import {
10   configureTestBed,
11   expectItemTasks,
12   i18nProviders
13 } from '../../../../testing/unit-test-helper';
14 import { RbdService } from '../../../shared/api/rbd.service';
15 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
16 import { ExecutingTask } from '../../../shared/models/executing-task';
17 import { SummaryService } from '../../../shared/services/summary.service';
18 import { TaskListService } from '../../../shared/services/task-list.service';
19 import { SharedModule } from '../../../shared/shared.module';
20 import { RbdTrashListComponent } from './rbd-trash-list.component';
21
22 describe('RbdTrashListComponent', () => {
23   let component: RbdTrashListComponent;
24   let fixture: ComponentFixture<RbdTrashListComponent>;
25   let summaryService: SummaryService;
26   let rbdService: RbdService;
27
28   configureTestBed({
29     declarations: [RbdTrashListComponent],
30     imports: [SharedModule, HttpClientTestingModule, RouterTestingModule, ToastrModule.forRoot()],
31     providers: [TaskListService, i18nProviders]
32   });
33
34   beforeEach(() => {
35     fixture = TestBed.createComponent(RbdTrashListComponent);
36     component = fixture.componentInstance;
37     summaryService = TestBed.get(SummaryService);
38     rbdService = TestBed.get(RbdService);
39     fixture.detectChanges();
40   });
41
42   it('should create', () => {
43     expect(component).toBeTruthy();
44   });
45
46   it('should load trash images when summary is trigged', () => {
47     spyOn(rbdService, 'listTrash').and.callThrough();
48
49     summaryService['summaryDataSource'].next({ executingTasks: null });
50     expect(rbdService.listTrash).toHaveBeenCalled();
51   });
52
53   it('should call updateSelection', () => {
54     const selection = new CdTableSelection();
55     selection.selected = ['foo'];
56     selection.update();
57
58     expect(component.selection.hasSelection).toBeFalsy();
59     component.updateSelection(selection);
60     expect(component.selection.hasSelection).toBeTruthy();
61   });
62
63   describe('handling of executing tasks', () => {
64     let images: any[];
65
66     const addImage = (id) => {
67       images.push({
68         id: id,
69         pool_name: 'pl'
70       });
71     };
72
73     const addTask = (name: string, image_id: string) => {
74       const task = new ExecutingTask();
75       task.name = name;
76       task.metadata = {
77         image_id_spec: `pl/${image_id}`
78       };
79       summaryService.addRunningTask(task);
80     };
81
82     beforeEach(() => {
83       images = [];
84       addImage('1');
85       addImage('2');
86       component.images = images;
87       summaryService['summaryDataSource'].next({ executingTasks: [] });
88       spyOn(rbdService, 'listTrash').and.callFake(() =>
89         of([{ poool_name: 'rbd', status: 1, value: images }])
90       );
91       fixture.detectChanges();
92     });
93
94     it('should gets all images without tasks', () => {
95       expect(component.images.length).toBe(2);
96       expect(component.images.every((image) => !image.cdExecuting)).toBeTruthy();
97     });
98
99     it('should show when an existing image is being modified', () => {
100       addTask('rbd/trash/remove', '1');
101       addTask('rbd/trash/restore', '2');
102       expect(component.images.length).toBe(2);
103       expectItemTasks(component.images[0], 'Deleting');
104       expectItemTasks(component.images[1], 'Restoring');
105     });
106   });
107
108   describe('display purge button', () => {
109     beforeEach(() => {});
110
111     it('should show button with delete permission', () => {
112       component.permission = {
113         read: true,
114         create: true,
115         delete: true,
116         update: true
117       };
118       fixture.detectChanges();
119
120       const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
121       expect(purge).not.toBeNull();
122     });
123
124     it('should remove button without delete permission', () => {
125       component.permission = {
126         read: true,
127         create: true,
128         delete: false,
129         update: true
130       };
131       fixture.detectChanges();
132
133       const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
134       expect(purge).toBeNull();
135     });
136   });
137 });