]>
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 | ||
9f95a23c | 5 | import { TabsModule } from 'ngx-bootstrap/tabs'; |
494da23a | 6 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
7 | import { of } from 'rxjs'; |
8 | ||
9 | import { By } from '@angular/platform-browser'; | |
eafe8130 TL |
10 | import { |
11 | configureTestBed, | |
12 | expectItemTasks, | |
13 | i18nProviders | |
14 | } from '../../../../testing/unit-test-helper'; | |
11fdf7f2 TL |
15 | import { RbdService } from '../../../shared/api/rbd.service'; |
16 | import { CdTableSelection } from '../../../shared/models/cd-table-selection'; | |
17 | import { ExecutingTask } from '../../../shared/models/executing-task'; | |
18 | import { SummaryService } from '../../../shared/services/summary.service'; | |
19 | import { TaskListService } from '../../../shared/services/task-list.service'; | |
20 | import { SharedModule } from '../../../shared/shared.module'; | |
9f95a23c | 21 | import { RbdTabsComponent } from '../rbd-tabs/rbd-tabs.component'; |
11fdf7f2 TL |
22 | import { RbdTrashListComponent } from './rbd-trash-list.component'; |
23 | ||
24 | describe('RbdTrashListComponent', () => { | |
25 | let component: RbdTrashListComponent; | |
26 | let fixture: ComponentFixture<RbdTrashListComponent>; | |
27 | let summaryService: SummaryService; | |
28 | let rbdService: RbdService; | |
29 | ||
30 | configureTestBed({ | |
9f95a23c TL |
31 | declarations: [RbdTrashListComponent, RbdTabsComponent], |
32 | imports: [ | |
33 | HttpClientTestingModule, | |
34 | RouterTestingModule, | |
35 | SharedModule, | |
36 | TabsModule.forRoot(), | |
37 | ToastrModule.forRoot() | |
38 | ], | |
11fdf7f2 TL |
39 | providers: [TaskListService, i18nProviders] |
40 | }); | |
41 | ||
42 | beforeEach(() => { | |
43 | fixture = TestBed.createComponent(RbdTrashListComponent); | |
44 | component = fixture.componentInstance; | |
45 | summaryService = TestBed.get(SummaryService); | |
46 | rbdService = TestBed.get(RbdService); | |
47 | fixture.detectChanges(); | |
48 | }); | |
49 | ||
50 | it('should create', () => { | |
51 | expect(component).toBeTruthy(); | |
52 | }); | |
53 | ||
54 | it('should load trash images when summary is trigged', () => { | |
55 | spyOn(rbdService, 'listTrash').and.callThrough(); | |
56 | ||
57 | summaryService['summaryDataSource'].next({ executingTasks: null }); | |
58 | expect(rbdService.listTrash).toHaveBeenCalled(); | |
59 | }); | |
60 | ||
61 | it('should call updateSelection', () => { | |
11fdf7f2 | 62 | expect(component.selection.hasSelection).toBeFalsy(); |
9f95a23c | 63 | component.updateSelection(new CdTableSelection(['foo'])); |
11fdf7f2 TL |
64 | expect(component.selection.hasSelection).toBeTruthy(); |
65 | }); | |
66 | ||
67 | describe('handling of executing tasks', () => { | |
68 | let images: any[]; | |
69 | ||
9f95a23c | 70 | const addImage = (id: string) => { |
11fdf7f2 | 71 | images.push({ |
9f95a23c TL |
72 | id: id, |
73 | pool_name: 'pl' | |
11fdf7f2 TL |
74 | }); |
75 | }; | |
76 | ||
77 | const addTask = (name: string, image_id: string) => { | |
78 | const task = new ExecutingTask(); | |
79 | task.name = name; | |
80 | task.metadata = { | |
9f95a23c | 81 | image_id_spec: `pl/${image_id}` |
11fdf7f2 TL |
82 | }; |
83 | summaryService.addRunningTask(task); | |
84 | }; | |
85 | ||
11fdf7f2 TL |
86 | beforeEach(() => { |
87 | images = []; | |
88 | addImage('1'); | |
89 | addImage('2'); | |
90 | component.images = images; | |
91 | summaryService['summaryDataSource'].next({ executingTasks: [] }); | |
92 | spyOn(rbdService, 'listTrash').and.callFake(() => | |
9f95a23c | 93 | of([{ pool_name: 'rbd', status: 1, value: images }]) |
11fdf7f2 TL |
94 | ); |
95 | fixture.detectChanges(); | |
96 | }); | |
97 | ||
98 | it('should gets all images without tasks', () => { | |
99 | expect(component.images.length).toBe(2); | |
9f95a23c TL |
100 | expect( |
101 | component.images.every((image: Record<string, any>) => !image.cdExecuting) | |
102 | ).toBeTruthy(); | |
11fdf7f2 TL |
103 | }); |
104 | ||
105 | it('should show when an existing image is being modified', () => { | |
106 | addTask('rbd/trash/remove', '1'); | |
107 | addTask('rbd/trash/restore', '2'); | |
108 | expect(component.images.length).toBe(2); | |
eafe8130 TL |
109 | expectItemTasks(component.images[0], 'Deleting'); |
110 | expectItemTasks(component.images[1], 'Restoring'); | |
11fdf7f2 TL |
111 | }); |
112 | }); | |
113 | ||
114 | describe('display purge button', () => { | |
9f95a23c TL |
115 | let images: any[]; |
116 | const addImage = (id: string) => { | |
117 | images.push({ | |
118 | id: id, | |
119 | pool_name: 'pl', | |
120 | deferment_end_time: 'abc' | |
121 | }); | |
122 | }; | |
123 | ||
124 | beforeEach(() => { | |
125 | summaryService['summaryDataSource'].next({ executingTasks: [] }); | |
126 | spyOn(rbdService, 'listTrash').and.callFake(() => { | |
127 | of([{ pool_name: 'rbd', status: 1, value: images }]); | |
128 | }); | |
129 | fixture.detectChanges(); | |
130 | }); | |
131 | ||
132 | it('should show button disabled when no image is in trash', () => { | |
133 | expect(component.disablePurgeBtn).toBeTruthy(); | |
134 | }); | |
135 | ||
136 | it('should show button enabled when an existing image is in trash', () => { | |
137 | images = []; | |
138 | addImage('1'); | |
139 | const payload = [{ pool_name: 'rbd', status: 1, value: images }]; | |
140 | component.prepareResponse(payload); | |
141 | expect(component.disablePurgeBtn).toBeFalsy(); | |
142 | }); | |
11fdf7f2 TL |
143 | |
144 | it('should show button with delete permission', () => { | |
145 | component.permission = { | |
146 | read: true, | |
147 | create: true, | |
148 | delete: true, | |
149 | update: true | |
150 | }; | |
151 | fixture.detectChanges(); | |
152 | ||
153 | const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times')); | |
154 | expect(purge).not.toBeNull(); | |
155 | }); | |
156 | ||
157 | it('should remove button without delete permission', () => { | |
158 | component.permission = { | |
159 | read: true, | |
160 | create: true, | |
161 | delete: false, | |
162 | update: true | |
163 | }; | |
164 | fixture.detectChanges(); | |
165 | ||
166 | const purge = fixture.debugElement.query(By.css('.table-actions button .fa-times')); | |
167 | expect(purge).toBeNull(); | |
168 | }); | |
169 | }); | |
170 | }); |