]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
3 | import { RouterTestingModule } from '@angular/router/testing'; | |
4 | ||
494da23a | 5 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
6 | import { of } from 'rxjs'; |
7 | ||
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'; | |
17 | ||
18 | describe('RbdTrashListComponent', () => { | |
19 | let component: RbdTrashListComponent; | |
20 | let fixture: ComponentFixture<RbdTrashListComponent>; | |
21 | let summaryService: SummaryService; | |
22 | let rbdService: RbdService; | |
23 | ||
24 | configureTestBed({ | |
25 | declarations: [RbdTrashListComponent], | |
494da23a | 26 | imports: [SharedModule, HttpClientTestingModule, RouterTestingModule, ToastrModule.forRoot()], |
11fdf7f2 TL |
27 | providers: [TaskListService, i18nProviders] |
28 | }); | |
29 | ||
30 | beforeEach(() => { | |
31 | fixture = TestBed.createComponent(RbdTrashListComponent); | |
32 | component = fixture.componentInstance; | |
33 | summaryService = TestBed.get(SummaryService); | |
34 | rbdService = TestBed.get(RbdService); | |
35 | fixture.detectChanges(); | |
36 | }); | |
37 | ||
38 | it('should create', () => { | |
39 | expect(component).toBeTruthy(); | |
40 | }); | |
41 | ||
42 | it('should load trash images when summary is trigged', () => { | |
43 | spyOn(rbdService, 'listTrash').and.callThrough(); | |
44 | ||
45 | summaryService['summaryDataSource'].next({ executingTasks: null }); | |
46 | expect(rbdService.listTrash).toHaveBeenCalled(); | |
47 | }); | |
48 | ||
49 | it('should call updateSelection', () => { | |
50 | const selection = new CdTableSelection(); | |
51 | selection.selected = ['foo']; | |
52 | selection.update(); | |
53 | ||
54 | expect(component.selection.hasSelection).toBeFalsy(); | |
55 | component.updateSelection(selection); | |
56 | expect(component.selection.hasSelection).toBeTruthy(); | |
57 | }); | |
58 | ||
59 | describe('handling of executing tasks', () => { | |
60 | let images: any[]; | |
61 | ||
62 | const addImage = (id) => { | |
63 | images.push({ | |
64 | id: id | |
65 | }); | |
66 | }; | |
67 | ||
68 | const addTask = (name: string, image_id: string) => { | |
69 | const task = new ExecutingTask(); | |
70 | task.name = name; | |
71 | task.metadata = { | |
72 | image_id: image_id | |
73 | }; | |
74 | summaryService.addRunningTask(task); | |
75 | }; | |
76 | ||
77 | const expectImageTasks = (image: any, executing: string) => { | |
78 | expect(image.cdExecuting).toEqual(executing); | |
79 | }; | |
80 | ||
81 | beforeEach(() => { | |
82 | images = []; | |
83 | addImage('1'); | |
84 | addImage('2'); | |
85 | component.images = images; | |
86 | summaryService['summaryDataSource'].next({ executingTasks: [] }); | |
87 | spyOn(rbdService, 'listTrash').and.callFake(() => | |
88 | of([{ poool_name: 'rbd', status: 1, value: images }]) | |
89 | ); | |
90 | fixture.detectChanges(); | |
91 | }); | |
92 | ||
93 | it('should gets all images without tasks', () => { | |
94 | expect(component.images.length).toBe(2); | |
95 | expect(component.images.every((image) => !image.cdExecuting)).toBeTruthy(); | |
96 | }); | |
97 | ||
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'); | |
104 | }); | |
105 | }); | |
106 | ||
107 | describe('display purge button', () => { | |
108 | beforeEach(() => {}); | |
109 | ||
110 | it('should show button with delete permission', () => { | |
111 | component.permission = { | |
112 | read: true, | |
113 | create: true, | |
114 | delete: true, | |
115 | update: true | |
116 | }; | |
117 | fixture.detectChanges(); | |
118 | ||
119 | const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times')); | |
120 | expect(purge).not.toBeNull(); | |
121 | }); | |
122 | ||
123 | it('should remove button without delete permission', () => { | |
124 | component.permission = { | |
125 | read: true, | |
126 | create: true, | |
127 | delete: false, | |
128 | update: true | |
129 | }; | |
130 | fixture.detectChanges(); | |
131 | ||
132 | const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times')); | |
133 | expect(purge).toBeNull(); | |
134 | }); | |
135 | }); | |
136 | }); |