1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
10 import { By } from '@angular/platform-browser';
15 } from '../../../../testing/unit-test-helper';
16 import { RbdService } from '../../../shared/api/rbd.service';
17 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
18 import { ExecutingTask } from '../../../shared/models/executing-task';
19 import { SummaryService } from '../../../shared/services/summary.service';
20 import { TaskListService } from '../../../shared/services/task-list.service';
21 import { SharedModule } from '../../../shared/shared.module';
22 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
23 import { RbdTrashListComponent } from './rbd-trash-list.component';
25 describe('RbdTrashListComponent', () => {
26 let component: RbdTrashListComponent;
27 let fixture: ComponentFixture<RbdTrashListComponent>;
28 let summaryService: SummaryService;
29 let rbdService: RbdService;
32 declarations: [RbdTrashListComponent, RbdTabsComponent],
34 BrowserAnimationsModule,
35 HttpClientTestingModule,
39 ToastrModule.forRoot()
41 providers: [TaskListService, i18nProviders]
45 fixture = TestBed.createComponent(RbdTrashListComponent);
46 component = fixture.componentInstance;
47 summaryService = TestBed.get(SummaryService);
48 rbdService = TestBed.get(RbdService);
49 fixture.detectChanges();
52 it('should create', () => {
53 expect(component).toBeTruthy();
56 it('should load trash images when summary is trigged', () => {
57 spyOn(rbdService, 'listTrash').and.callThrough();
59 summaryService['summaryDataSource'].next({ executingTasks: null });
60 expect(rbdService.listTrash).toHaveBeenCalled();
63 it('should call updateSelection', () => {
64 expect(component.selection.hasSelection).toBeFalsy();
65 component.updateSelection(new CdTableSelection(['foo']));
66 expect(component.selection.hasSelection).toBeTruthy();
69 describe('handling of executing tasks', () => {
72 const addImage = (id: string) => {
79 const addTask = (name: string, image_id: string) => {
80 const task = new ExecutingTask();
83 image_id_spec: `pl/${image_id}`
85 summaryService.addRunningTask(task);
92 component.images = images;
93 summaryService['summaryDataSource'].next({ executingTasks: [] });
94 spyOn(rbdService, 'listTrash').and.callFake(() =>
95 of([{ pool_name: 'rbd', status: 1, value: images }])
97 fixture.detectChanges();
100 it('should gets all images without tasks', () => {
101 expect(component.images.length).toBe(2);
103 component.images.every((image: Record<string, any>) => !image.cdExecuting)
107 it('should show when an existing image is being modified', () => {
108 addTask('rbd/trash/remove', '1');
109 addTask('rbd/trash/restore', '2');
110 expect(component.images.length).toBe(2);
111 expectItemTasks(component.images[0], 'Deleting');
112 expectItemTasks(component.images[1], 'Restoring');
116 describe('display purge button', () => {
118 const addImage = (id: string) => {
122 deferment_end_time: 'abc'
127 summaryService['summaryDataSource'].next({ executingTasks: [] });
128 spyOn(rbdService, 'listTrash').and.callFake(() => {
129 of([{ pool_name: 'rbd', status: 1, value: images }]);
131 fixture.detectChanges();
134 it('should show button disabled when no image is in trash', () => {
135 expect(component.disablePurgeBtn).toBeTruthy();
138 it('should show button enabled when an existing image is in trash', () => {
141 const payload = [{ pool_name: 'rbd', status: 1, value: images }];
142 component.prepareResponse(payload);
143 expect(component.disablePurgeBtn).toBeFalsy();
146 it('should show button with delete permission', () => {
147 component.permission = {
153 fixture.detectChanges();
155 const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
156 expect(purge).not.toBeNull();
159 it('should remove button without delete permission', () => {
160 component.permission = {
166 fixture.detectChanges();
168 const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
169 expect(purge).toBeNull();