1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { TabsModule } from 'ngx-bootstrap/tabs';
6 import { ToastrModule } from 'ngx-toastr';
7 import { 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: object) => {
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);
58 it('should create', () => {
59 expect(component).toBeTruthy();
62 describe('after ngOnInit', () => {
64 fixture.detectChanges();
65 spyOn(nfsService, 'list').and.callThrough();
66 httpTesting.expectOne('api/nfs-ganesha/daemon').flush([]);
73 it('should load exports on init', () => {
75 httpTesting.expectOne('api/nfs-ganesha/export');
76 expect(nfsService.list).toHaveBeenCalled();
79 it('should not load images on init because no data', () => {
81 expect(nfsService.list).not.toHaveBeenCalled();
84 it('should call error function on init when summary service fails', () => {
85 spyOn(component.table, 'reset');
86 summaryService['summaryDataSource'].error(undefined);
87 expect(component.table.reset).toHaveBeenCalled();
91 describe('handling of executing tasks', () => {
94 const addExport = (export_id: string) => {
97 path: 'path_' + export_id,
98 fsal: 'fsal_' + export_id,
99 cluster_id: 'cluster_' + export_id
104 const addTask = (name: string, export_id: string) => {
105 const task = new ExecutingTask();
110 path: 'path_' + export_id,
111 fsal: 'fsal_' + export_id,
112 cluster_id: 'cluster_' + export_id
117 cluster_id: 'cluster_' + export_id,
122 summaryService.addRunningTask(task);
130 component.exports = exports;
131 refresh({ executing_tasks: [], finished_tasks: [] });
132 spyOn(nfsService, 'list').and.callFake(() => of(exports));
133 fixture.detectChanges();
135 const req = httpTesting.expectOne('api/nfs-ganesha/daemon');
139 it('should gets all exports without tasks', () => {
140 expect(component.exports.length).toBe(3);
141 expect(component.exports.every((expo) => !expo.cdExecuting)).toBeTruthy();
144 it('should add a new export from a task', fakeAsync(() => {
145 addTask('nfs/create', 'd');
147 expect(component.exports.length).toBe(4);
148 expectItemTasks(component.exports[0], undefined);
149 expectItemTasks(component.exports[1], undefined);
150 expectItemTasks(component.exports[2], undefined);
151 expectItemTasks(component.exports[3], 'Creating');
154 it('should show when an existing export is being modified', () => {
155 addTask('nfs/edit', 'a');
156 addTask('nfs/delete', 'b');
157 expect(component.exports.length).toBe(3);
158 expectItemTasks(component.exports[0], 'Updating');
159 expectItemTasks(component.exports[1], 'Deleting');
163 it('should test all TableActions combinations', () => {
164 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
165 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
166 component.tableActions
169 expect(tableActions).toEqual({
170 'create,update,delete': {
171 actions: ['Create', 'Edit', 'Delete'],
172 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
175 actions: ['Create', 'Edit'],
176 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
179 actions: ['Create', 'Delete'],
180 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
184 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
187 actions: ['Edit', 'Delete'],
188 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
192 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
196 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
200 primary: { multiple: '', executing: '', single: '', no: '' }