1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
8 import { BehaviorSubject, of } from 'rxjs';
14 } from '../../../../testing/unit-test-helper';
15 import { NfsService } from '../../../shared/api/nfs.service';
16 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
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';
21 import { NfsDetailsComponent } from '../nfs-details/nfs-details.component';
22 import { NfsListComponent } from './nfs-list.component';
24 describe('NfsListComponent', () => {
25 let component: NfsListComponent;
26 let fixture: ComponentFixture<NfsListComponent>;
27 let summaryService: SummaryService;
28 let nfsService: NfsService;
29 let httpTesting: HttpTestingController;
31 const refresh = (data) => {
32 summaryService['summaryDataSource'].next(data);
37 declarations: [NfsListComponent, NfsDetailsComponent],
39 HttpClientTestingModule,
42 ToastrModule.forRoot(),
45 providers: [TaskListService, i18nProviders]
51 fixture = TestBed.createComponent(NfsListComponent);
52 component = fixture.componentInstance;
53 summaryService = TestBed.get(SummaryService);
54 nfsService = TestBed.get(NfsService);
55 httpTesting = TestBed.get(HttpTestingController);
57 // this is needed because summaryService isn't being reset after each test.
58 summaryService['summaryDataSource'] = new BehaviorSubject(null);
59 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
62 it('should create', () => {
63 expect(component).toBeTruthy();
66 describe('after ngOnInit', () => {
68 fixture.detectChanges();
69 spyOn(nfsService, 'list').and.callThrough();
70 httpTesting.expectOne('api/nfs-ganesha/daemon').flush([]);
71 httpTesting.expectOne('api/summary');
78 it('should load exports on init', () => {
80 httpTesting.expectOne('api/nfs-ganesha/export');
81 expect(nfsService.list).toHaveBeenCalled();
84 it('should not load images on init because no data', () => {
86 expect(nfsService.list).not.toHaveBeenCalled();
89 it('should call error function on init when summary service fails', () => {
90 spyOn(component.table, 'reset');
91 summaryService['summaryDataSource'].error(undefined);
92 expect(component.table.reset).toHaveBeenCalled();
96 describe('handling of executing tasks', () => {
99 const addExport = (export_id) => {
101 export_id: export_id,
102 path: 'path_' + export_id,
103 fsal: 'fsal_' + export_id,
104 cluster_id: 'cluster_' + export_id
109 const addTask = (name: string, export_id: string) => {
110 const task = new ExecutingTask();
115 path: 'path_' + export_id,
116 fsal: 'fsal_' + export_id,
117 cluster_id: 'cluster_' + export_id
122 cluster_id: 'cluster_' + export_id,
127 summaryService.addRunningTask(task);
130 const expectExportTasks = (expo: any, executing: string) => {
131 expect(expo.cdExecuting).toEqual(executing);
139 component.exports = exports;
140 refresh({ executing_tasks: [], finished_tasks: [] });
141 spyOn(nfsService, 'list').and.callFake(() => of(exports));
142 fixture.detectChanges();
144 const req = httpTesting.expectOne('api/nfs-ganesha/daemon');
148 it('should gets all exports without tasks', () => {
149 expect(component.exports.length).toBe(3);
150 expect(component.exports.every((expo) => !expo.cdExecuting)).toBeTruthy();
153 it('should add a new export from a task', fakeAsync(() => {
154 addTask('nfs/create', 'd');
156 expect(component.exports.length).toBe(4);
157 expectExportTasks(component.exports[0], undefined);
158 expectExportTasks(component.exports[1], undefined);
159 expectExportTasks(component.exports[2], undefined);
160 expectExportTasks(component.exports[3], 'Creating');
163 it('should show when an existing export is being modified', () => {
164 addTask('nfs/edit', 'a');
165 addTask('nfs/delete', 'b');
166 expect(component.exports.length).toBe(3);
167 expectExportTasks(component.exports[0], 'Updating');
168 expectExportTasks(component.exports[1], 'Deleting');
172 describe('show action buttons and drop down actions depending on permissions', () => {
173 let tableActions: TableActionsComponent;
174 let scenario: { fn; empty; single };
175 let permissionHelper: PermissionHelper;
177 const getTableActionComponent = (): TableActionsComponent => {
178 fixture.detectChanges();
179 return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
183 permissionHelper = new PermissionHelper(component.permission, () =>
184 getTableActionComponent()
187 fn: () => tableActions.getCurrentButton().name,
193 describe('with all', () => {
195 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1);
198 it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
199 permissionHelper.testScenarios(scenario));
201 it('shows all actions', () => {
202 expect(tableActions.tableActions.length).toBe(3);
203 expect(tableActions.tableActions).toEqual(component.tableActions);
207 describe('with read, create and update', () => {
209 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0);
212 it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
213 permissionHelper.testScenarios(scenario));
215 it(`shows all actions except for 'Delete'`, () => {
216 expect(tableActions.tableActions.length).toBe(2);
217 component.tableActions.pop();
218 expect(tableActions.tableActions).toEqual(component.tableActions);
222 describe('with read, create and delete', () => {
224 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1);
227 it(`shows 'Delete' for single selection else 'Add' as main action`, () => {
228 scenario.single = 'Delete';
229 permissionHelper.testScenarios(scenario);
232 it(`shows 'Add', and 'Delete' action`, () => {
233 expect(tableActions.tableActions.length).toBe(2);
234 expect(tableActions.tableActions).toEqual([
235 component.tableActions[0],
236 component.tableActions[2]
241 describe('with read, edit and delete', () => {
243 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1);
246 it(`shows always 'Edit' as main action`, () => {
247 scenario.empty = 'Edit';
248 permissionHelper.testScenarios(scenario);
251 it(`shows 'Edit' and 'Delete' actions`, () => {
252 expect(tableActions.tableActions.length).toBe(2);
253 expect(tableActions.tableActions).toEqual([
254 component.tableActions[1],
255 component.tableActions[2]
260 describe('with read and create', () => {
262 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0);
265 it(`always shows 'Add' as main action`, () => {
266 scenario.single = 'Add';
267 permissionHelper.testScenarios(scenario);
270 it(`shows 'Add' action`, () => {
271 expect(tableActions.tableActions.length).toBe(1);
272 expect(tableActions.tableActions).toEqual([component.tableActions[0]]);
276 describe('with read and edit', () => {
278 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
281 it(`shows always 'Edit' as main action`, () => {
282 scenario.empty = 'Edit';
283 permissionHelper.testScenarios(scenario);
286 it(`shows 'Edit' action`, () => {
287 expect(tableActions.tableActions.length).toBe(1);
288 expect(tableActions.tableActions).toEqual([component.tableActions[1]]);
292 describe('with read and delete', () => {
294 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1);
297 it(`shows always 'Delete' as main action`, () => {
298 scenario.single = 'Delete';
299 scenario.empty = 'Delete';
300 permissionHelper.testScenarios(scenario);
303 it(`shows 'Delete' action`, () => {
304 expect(tableActions.tableActions.length).toBe(1);
305 expect(tableActions.tableActions).toEqual([component.tableActions[2]]);
309 describe('with only read', () => {
311 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
314 it('shows no main action', () => {
315 permissionHelper.testScenarios({
316 fn: () => tableActions.getCurrentButton(),
322 it('shows no actions', () => {
323 expect(tableActions.tableActions.length).toBe(0);
324 expect(tableActions.tableActions).toEqual([]);