1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { ToastrModule } from 'ngx-toastr';
6 import { of } from 'rxjs';
8 import { By } from '@angular/platform-browser';
9 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
10 import { RbdService } from '../../../shared/api/rbd.service';
11 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
12 import { ExecutingTask } from '../../../shared/models/executing-task';
13 import { SummaryService } from '../../../shared/services/summary.service';
14 import { TaskListService } from '../../../shared/services/task-list.service';
15 import { SharedModule } from '../../../shared/shared.module';
16 import { RbdTrashListComponent } from './rbd-trash-list.component';
18 describe('RbdTrashListComponent', () => {
19 let component: RbdTrashListComponent;
20 let fixture: ComponentFixture<RbdTrashListComponent>;
21 let summaryService: SummaryService;
22 let rbdService: RbdService;
25 declarations: [RbdTrashListComponent],
26 imports: [SharedModule, HttpClientTestingModule, RouterTestingModule, ToastrModule.forRoot()],
27 providers: [TaskListService, i18nProviders]
31 fixture = TestBed.createComponent(RbdTrashListComponent);
32 component = fixture.componentInstance;
33 summaryService = TestBed.get(SummaryService);
34 rbdService = TestBed.get(RbdService);
35 fixture.detectChanges();
38 it('should create', () => {
39 expect(component).toBeTruthy();
42 it('should load trash images when summary is trigged', () => {
43 spyOn(rbdService, 'listTrash').and.callThrough();
45 summaryService['summaryDataSource'].next({ executingTasks: null });
46 expect(rbdService.listTrash).toHaveBeenCalled();
49 it('should call updateSelection', () => {
50 const selection = new CdTableSelection();
51 selection.selected = ['foo'];
54 expect(component.selection.hasSelection).toBeFalsy();
55 component.updateSelection(selection);
56 expect(component.selection.hasSelection).toBeTruthy();
59 describe('handling of executing tasks', () => {
62 const addImage = (id) => {
68 const addTask = (name: string, image_id: string) => {
69 const task = new ExecutingTask();
74 summaryService.addRunningTask(task);
77 const expectImageTasks = (image: any, executing: string) => {
78 expect(image.cdExecuting).toEqual(executing);
85 component.images = images;
86 summaryService['summaryDataSource'].next({ executingTasks: [] });
87 spyOn(rbdService, 'listTrash').and.callFake(() =>
88 of([{ poool_name: 'rbd', status: 1, value: images }])
90 fixture.detectChanges();
93 it('should gets all images without tasks', () => {
94 expect(component.images.length).toBe(2);
95 expect(component.images.every((image) => !image.cdExecuting)).toBeTruthy();
98 it('should show when an existing image is being modified', () => {
99 addTask('rbd/trash/remove', '1');
100 addTask('rbd/trash/restore', '2');
101 expect(component.images.length).toBe(2);
102 expectImageTasks(component.images[0], 'Deleting');
103 expectImageTasks(component.images[1], 'Restoring');
107 describe('display purge button', () => {
108 beforeEach(() => {});
110 it('should show button with delete permission', () => {
111 component.permission = {
117 fixture.detectChanges();
119 const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
120 expect(purge).not.toBeNull();
123 it('should remove button without delete permission', () => {
124 component.permission = {
130 fixture.detectChanges();
132 const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times'));
133 expect(purge).toBeNull();