]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
7c10a1700ed889785038ecbd8c9681b74d2a40c9
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { TabsModule } from 'ngx-bootstrap/tabs';
6 import { ToastrModule } from 'ngx-toastr';
7 import { of as observableOf, throwError as observableThrowError } from 'rxjs';
8
9 import {
10   configureTestBed,
11   i18nProviders,
12   PermissionHelper
13 } from '../../../../../testing/unit-test-helper';
14 import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
15 import { TableActionsComponent } from '../../../../shared/datatable/table-actions/table-actions.component';
16 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
17 import { NotificationService } from '../../../../shared/services/notification.service';
18 import { SharedModule } from '../../../../shared/shared.module';
19 import { MgrModuleDetailsComponent } from '../mgr-module-details/mgr-module-details.component';
20 import { MgrModuleListComponent } from './mgr-module-list.component';
21
22 describe('MgrModuleListComponent', () => {
23   let component: MgrModuleListComponent;
24   let fixture: ComponentFixture<MgrModuleListComponent>;
25   let mgrModuleService: MgrModuleService;
26   let notificationService: NotificationService;
27
28   configureTestBed({
29     declarations: [MgrModuleListComponent, MgrModuleDetailsComponent],
30     imports: [
31       RouterTestingModule,
32       SharedModule,
33       HttpClientTestingModule,
34       TabsModule.forRoot(),
35       ToastrModule.forRoot()
36     ],
37     providers: [MgrModuleService, NotificationService, i18nProviders]
38   });
39
40   beforeEach(() => {
41     fixture = TestBed.createComponent(MgrModuleListComponent);
42     component = fixture.componentInstance;
43     mgrModuleService = TestBed.get(MgrModuleService);
44     notificationService = TestBed.get(NotificationService);
45   });
46
47   it('should create', () => {
48     fixture.detectChanges();
49     expect(component).toBeTruthy();
50   });
51
52   it('should test all TableActions combinations', () => {
53     const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
54     const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
55       component.tableActions
56     );
57
58     expect(tableActions).toEqual({
59       'create,update,delete': {
60         actions: ['Edit', 'Enable', 'Disable'],
61         primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
62       },
63       'create,update': {
64         actions: ['Edit', 'Enable', 'Disable'],
65         primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
66       },
67       'create,delete': {
68         actions: [],
69         primary: { multiple: '', executing: '', single: '', no: '' }
70       },
71       create: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } },
72       'update,delete': {
73         actions: ['Edit', 'Enable', 'Disable'],
74         primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
75       },
76       update: {
77         actions: ['Edit', 'Enable', 'Disable'],
78         primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
79       },
80       delete: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } },
81       'no-permissions': {
82         actions: [],
83         primary: { multiple: '', executing: '', single: '', no: '' }
84       }
85     });
86   });
87
88   describe('should update module state', () => {
89     beforeEach(() => {
90       component.selection = new CdTableSelection();
91       spyOn(notificationService, 'suspendToasties');
92       spyOn(component.blockUI, 'start');
93       spyOn(component.blockUI, 'stop');
94       spyOn(component.table, 'refreshBtn');
95     });
96
97     it('should enable module', fakeAsync(() => {
98       spyOn(mgrModuleService, 'enable').and.returnValue(observableThrowError('y'));
99       spyOn(mgrModuleService, 'list').and.returnValues(observableThrowError('z'), observableOf([]));
100       component.selection.selected.push({
101         name: 'foo',
102         enabled: false
103       });
104       component.selection.update();
105       component.updateModuleState();
106       tick(2000);
107       tick(2000);
108       expect(mgrModuleService.enable).toHaveBeenCalledWith('foo');
109       expect(mgrModuleService.list).toHaveBeenCalledTimes(2);
110       expect(notificationService.suspendToasties).toHaveBeenCalledTimes(2);
111       expect(component.blockUI.start).toHaveBeenCalled();
112       expect(component.blockUI.stop).toHaveBeenCalled();
113       expect(component.table.refreshBtn).toHaveBeenCalled();
114     }));
115
116     it('should disable module', fakeAsync(() => {
117       spyOn(mgrModuleService, 'disable').and.returnValue(observableThrowError('x'));
118       spyOn(mgrModuleService, 'list').and.returnValue(observableOf([]));
119       component.selection.selected.push({
120         name: 'bar',
121         enabled: true
122       });
123       component.selection.update();
124       component.updateModuleState();
125       tick(2000);
126       expect(mgrModuleService.disable).toHaveBeenCalledWith('bar');
127       expect(mgrModuleService.list).toHaveBeenCalledTimes(1);
128       expect(notificationService.suspendToasties).toHaveBeenCalledTimes(2);
129       expect(component.blockUI.start).toHaveBeenCalled();
130       expect(component.blockUI.stop).toHaveBeenCalled();
131       expect(component.table.refreshBtn).toHaveBeenCalled();
132     }));
133   });
134 });