1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
8 import * as moment from 'moment';
9 import { ToastrModule } from 'ngx-toastr';
10 import { of } from 'rxjs';
16 } from '../../../../testing/unit-test-helper';
17 import { RbdService } from '../../../shared/api/rbd.service';
18 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
19 import { ExecutingTask } from '../../../shared/models/executing-task';
20 import { Summary } from '../../../shared/models/summary.model';
21 import { SummaryService } from '../../../shared/services/summary.service';
22 import { TaskListService } from '../../../shared/services/task-list.service';
23 import { SharedModule } from '../../../shared/shared.module';
24 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
25 import { RbdTrashListComponent } from './rbd-trash-list.component';
27 describe('RbdTrashListComponent', () => {
28 let component: RbdTrashListComponent;
29 let fixture: ComponentFixture<RbdTrashListComponent>;
30 let summaryService: SummaryService;
31 let rbdService: RbdService;
34 declarations: [RbdTrashListComponent, RbdTabsComponent],
36 BrowserAnimationsModule,
37 HttpClientTestingModule,
41 ToastrModule.forRoot()
43 providers: [TaskListService, i18nProviders]
47 fixture = TestBed.createComponent(RbdTrashListComponent);
48 component = fixture.componentInstance;
49 summaryService = TestBed.inject(SummaryService);
50 rbdService = TestBed.inject(RbdService);
51 fixture.detectChanges();
54 it('should create', () => {
55 expect(component).toBeTruthy();
58 it('should load trash images when summary is trigged', () => {
59 spyOn(rbdService, 'listTrash').and.callThrough();
61 summaryService['summaryDataSource'].next(new Summary());
62 expect(rbdService.listTrash).toHaveBeenCalled();
65 it('should call updateSelection', () => {
66 expect(component.selection.hasSelection).toBeFalsy();
67 component.updateSelection(new CdTableSelection(['foo']));
68 expect(component.selection.hasSelection).toBeTruthy();
71 describe('handling of executing tasks', () => {
74 const addImage = (id: string) => {
81 const addTask = (name: string, image_id: string) => {
82 const task = new ExecutingTask();
85 image_id_spec: `pl/${image_id}`
87 summaryService.addRunningTask(task);
94 component.images = images;
95 summaryService['summaryDataSource'].next(new Summary());
96 spyOn(rbdService, 'listTrash').and.callFake(() =>
97 of([{ pool_name: 'rbd', status: 1, value: images }])
99 fixture.detectChanges();
102 it('should gets all images without tasks', () => {
103 expect(component.images.length).toBe(2);
105 component.images.every((image: Record<string, any>) => !image.cdExecuting)
109 it('should show when an existing image is being modified', () => {
110 addTask('rbd/trash/remove', '1');
111 addTask('rbd/trash/restore', '2');
112 expect(component.images.length).toBe(2);
113 expectItemTasks(component.images[0], 'Deleting');
114 expectItemTasks(component.images[1], 'Restoring');
118 describe('display purge button', () => {
120 const addImage = (id: string) => {
124 deferment_end_time: moment()
129 summaryService['summaryDataSource'].next(new Summary());
130 spyOn(rbdService, 'listTrash').and.callFake(() => {
131 of([{ pool_name: 'rbd', status: 1, value: images }]);
133 fixture.detectChanges();
136 it('should show button disabled when no image is in trash', () => {
137 expect(component.disablePurgeBtn).toBeTruthy();
140 it('should show button enabled when an existing image is in trash', () => {
143 const payload = [{ pool_name: 'rbd', status: 1, value: images }];
144 component.prepareResponse(payload);
145 expect(component.disablePurgeBtn).toBeFalsy();
148 it('should show button with delete permission', () => {
149 component.permission = {
155 fixture.detectChanges();
157 const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
158 expect(purge).not.toBeNull();
161 it('should remove button without delete permission', () => {
162 component.permission = {
168 fixture.detectChanges();
170 const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
171 expect(purge).toBeNull();