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