]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts
import 15.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / nfs / nfs-list / nfs-list.component.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
9
10 import {
11 configureTestBed,
12 expectItemTasks,
13 i18nProviders,
14 PermissionHelper
15 } from '../../../../testing/unit-test-helper';
16 import { NfsService } from '../../../shared/api/nfs.service';
17 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
18 import { ExecutingTask } from '../../../shared/models/executing-task';
19 import { SummaryService } from '../../../shared/services/summary.service';
20 import { TaskListService } from '../../../shared/services/task-list.service';
21 import { SharedModule } from '../../../shared/shared.module';
22 import { NfsDetailsComponent } from '../nfs-details/nfs-details.component';
23 import { NfsListComponent } from './nfs-list.component';
24
25 describe('NfsListComponent', () => {
26 let component: NfsListComponent;
27 let fixture: ComponentFixture<NfsListComponent>;
28 let summaryService: SummaryService;
29 let nfsService: NfsService;
30 let httpTesting: HttpTestingController;
31
32 const refresh = (data: object) => {
33 summaryService['summaryDataSource'].next(data);
34 };
35
36 configureTestBed(
37 {
38 declarations: [NfsListComponent, NfsDetailsComponent],
39 imports: [
40 BrowserAnimationsModule,
41 HttpClientTestingModule,
42 RouterTestingModule,
43 SharedModule,
44 ToastrModule.forRoot(),
45 TabsModule.forRoot()
46 ],
47 providers: [TaskListService, i18nProviders]
48 },
49 true
50 );
51
52 beforeEach(() => {
53 fixture = TestBed.createComponent(NfsListComponent);
54 component = fixture.componentInstance;
55 summaryService = TestBed.get(SummaryService);
56 nfsService = TestBed.get(NfsService);
57 httpTesting = TestBed.get(HttpTestingController);
58 });
59
60 it('should create', () => {
61 expect(component).toBeTruthy();
62 });
63
64 describe('after ngOnInit', () => {
65 beforeEach(() => {
66 fixture.detectChanges();
67 spyOn(nfsService, 'list').and.callThrough();
68 httpTesting.expectOne('api/nfs-ganesha/daemon').flush([]);
69 });
70
71 afterEach(() => {
72 httpTesting.verify();
73 });
74
75 it('should load exports on init', () => {
76 refresh({});
77 httpTesting.expectOne('api/nfs-ganesha/export');
78 expect(nfsService.list).toHaveBeenCalled();
79 });
80
81 it('should not load images on init because no data', () => {
82 refresh(undefined);
83 expect(nfsService.list).not.toHaveBeenCalled();
84 });
85
86 it('should call error function on init when summary service fails', () => {
87 spyOn(component.table, 'reset');
88 summaryService['summaryDataSource'].error(undefined);
89 expect(component.table.reset).toHaveBeenCalled();
90 });
91 });
92
93 describe('handling of executing tasks', () => {
94 let exports: any[];
95
96 const addExport = (export_id: string) => {
97 const model = {
98 export_id: export_id,
99 path: 'path_' + export_id,
100 fsal: 'fsal_' + export_id,
101 cluster_id: 'cluster_' + export_id
102 };
103 exports.push(model);
104 };
105
106 const addTask = (name: string, export_id: string) => {
107 const task = new ExecutingTask();
108 task.name = name;
109 switch (task.name) {
110 case 'nfs/create':
111 task.metadata = {
112 path: 'path_' + export_id,
113 fsal: 'fsal_' + export_id,
114 cluster_id: 'cluster_' + export_id
115 };
116 break;
117 default:
118 task.metadata = {
119 cluster_id: 'cluster_' + export_id,
120 export_id: export_id
121 };
122 break;
123 }
124 summaryService.addRunningTask(task);
125 };
126
127 beforeEach(() => {
128 exports = [];
129 addExport('a');
130 addExport('b');
131 addExport('c');
132 component.exports = exports;
133 refresh({ executing_tasks: [], finished_tasks: [] });
134 spyOn(nfsService, 'list').and.callFake(() => of(exports));
135 fixture.detectChanges();
136
137 const req = httpTesting.expectOne('api/nfs-ganesha/daemon');
138 req.flush([]);
139 });
140
141 it('should gets all exports without tasks', () => {
142 expect(component.exports.length).toBe(3);
143 expect(component.exports.every((expo) => !expo.cdExecuting)).toBeTruthy();
144 });
145
146 it('should add a new export from a task', fakeAsync(() => {
147 addTask('nfs/create', 'd');
148 tick();
149 expect(component.exports.length).toBe(4);
150 expectItemTasks(component.exports[0], undefined);
151 expectItemTasks(component.exports[1], undefined);
152 expectItemTasks(component.exports[2], undefined);
153 expectItemTasks(component.exports[3], 'Creating');
154 }));
155
156 it('should show when an existing export is being modified', () => {
157 addTask('nfs/edit', 'a');
158 addTask('nfs/delete', 'b');
159 expect(component.exports.length).toBe(3);
160 expectItemTasks(component.exports[0], 'Updating');
161 expectItemTasks(component.exports[1], 'Deleting');
162 });
163 });
164
165 it('should test all TableActions combinations', () => {
166 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
167 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
168 component.tableActions
169 );
170
171 expect(tableActions).toEqual({
172 'create,update,delete': {
173 actions: ['Create', 'Edit', 'Delete'],
174 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
175 },
176 'create,update': {
177 actions: ['Create', 'Edit'],
178 primary: { multiple: 'Create', executing: 'Edit', single: 'Edit', no: 'Create' }
179 },
180 'create,delete': {
181 actions: ['Create', 'Delete'],
182 primary: { multiple: 'Create', executing: 'Delete', single: 'Delete', no: 'Create' }
183 },
184 create: {
185 actions: ['Create'],
186 primary: { multiple: 'Create', executing: 'Create', single: 'Create', no: 'Create' }
187 },
188 'update,delete': {
189 actions: ['Edit', 'Delete'],
190 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
191 },
192 update: {
193 actions: ['Edit'],
194 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
195 },
196 delete: {
197 actions: ['Delete'],
198 primary: { multiple: 'Delete', executing: 'Delete', single: 'Delete', no: 'Delete' }
199 },
200 'no-permissions': {
201 actions: [],
202 primary: { multiple: '', executing: '', single: '', no: '' }
203 }
204 });
205 });
206 });