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