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 moment from 'moment';
9 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
10 import { ToastrModule } from 'ngx-toastr';
11 import { of } from 'rxjs';
13 import { RbdService } from '~/app/shared/api/rbd.service';
14 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
15 import { ExecutingTask } from '~/app/shared/models/executing-task';
16 import { Summary } from '~/app/shared/models/summary.model';
17 import { SummaryService } from '~/app/shared/services/summary.service';
18 import { TaskListService } from '~/app/shared/services/task-list.service';
19 import { SharedModule } from '~/app/shared/shared.module';
20 import { configureTestBed, expectItemTasks } from '~/testing/unit-test-helper';
21 import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component';
22 import { RbdTrashListComponent } from './rbd-trash-list.component';
24 describe('RbdTrashListComponent', () => {
25 let component: RbdTrashListComponent;
26 let fixture: ComponentFixture<RbdTrashListComponent>;
27 let summaryService: SummaryService;
28 let rbdService: RbdService;
31 declarations: [RbdTrashListComponent, RbdTabsComponent],
33 BrowserAnimationsModule,
34 HttpClientTestingModule,
38 NgxPipeFunctionModule,
39 ToastrModule.forRoot()
41 providers: [TaskListService]
45 fixture = TestBed.createComponent(RbdTrashListComponent);
46 component = fixture.componentInstance;
47 summaryService = TestBed.inject(SummaryService);
48 rbdService = TestBed.inject(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(new Summary());
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(new Summary());
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: moment()
127 summaryService['summaryDataSource'].next(new Summary());
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();