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