]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
252bd67e2085a40563d4c2335356631fc2eb9a0e
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { TabsModule } from 'ngx-bootstrap/tabs';
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       RouterTestingModule,
33       SharedModule,
34       HttpClientTestingModule,
35       TabsModule.forRoot(),
36       ToastrModule.forRoot()
37     ],
38     providers: [MgrModuleService, NotificationService, i18nProviders]
39   });
40
41   beforeEach(() => {
42     fixture = TestBed.createComponent(MgrModuleListComponent);
43     component = fixture.componentInstance;
44     mgrModuleService = TestBed.get(MgrModuleService);
45     notificationService = TestBed.get(NotificationService);
46   });
47
48   it('should create', () => {
49     fixture.detectChanges();
50     expect(component).toBeTruthy();
51   });
52
53   describe('show action buttons and drop down actions depending on permissions', () => {
54     let tableActions: TableActionsComponent;
55     let scenario: { fn; empty; single };
56     let permissionHelper: PermissionHelper;
57
58     const getTableActionComponent = (): TableActionsComponent => {
59       fixture.detectChanges();
60       return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
61     };
62
63     beforeEach(() => {
64       permissionHelper = new PermissionHelper(component.permission, () =>
65         getTableActionComponent()
66       );
67       scenario = {
68         fn: () => tableActions.getCurrentButton().name,
69         single: 'Edit',
70         empty: 'Edit'
71       };
72     });
73
74     describe('with read and update', () => {
75       beforeEach(() => {
76         tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
77       });
78
79       it('shows action button', () => permissionHelper.testScenarios(scenario));
80
81       it('shows all actions', () => {
82         expect(tableActions.tableActions.length).toBe(3);
83         expect(tableActions.tableActions).toEqual(component.tableActions);
84       });
85     });
86
87     describe('with only read', () => {
88       beforeEach(() => {
89         tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
90       });
91
92       it('shows no main action', () => {
93         permissionHelper.testScenarios({
94           fn: () => tableActions.getCurrentButton(),
95           single: undefined,
96           empty: undefined
97         });
98       });
99
100       it('shows no actions', () => {
101         expect(tableActions.tableActions.length).toBe(0);
102         expect(tableActions.tableActions).toEqual([]);
103       });
104     });
105   });
106
107   describe('should update module state', () => {
108     beforeEach(() => {
109       component.selection = new CdTableSelection();
110       spyOn(notificationService, 'suspendToasties');
111       spyOn(component.blockUI, 'start');
112       spyOn(component.blockUI, 'stop');
113       spyOn(component.table, 'refreshBtn');
114     });
115
116     it('should enable module', fakeAsync(() => {
117       spyOn(mgrModuleService, 'enable').and.returnValue(observableThrowError('y'));
118       spyOn(mgrModuleService, 'list').and.returnValues(observableThrowError('z'), observableOf([]));
119       component.selection.selected.push({
120         name: 'foo',
121         enabled: false
122       });
123       component.selection.update();
124       component.updateModuleState();
125       tick(2000);
126       tick(2000);
127       expect(mgrModuleService.enable).toHaveBeenCalledWith('foo');
128       expect(mgrModuleService.list).toHaveBeenCalledTimes(2);
129       expect(notificationService.suspendToasties).toHaveBeenCalledTimes(2);
130       expect(component.blockUI.start).toHaveBeenCalled();
131       expect(component.blockUI.stop).toHaveBeenCalled();
132       expect(component.table.refreshBtn).toHaveBeenCalled();
133     }));
134
135     it('should disable module', fakeAsync(() => {
136       spyOn(mgrModuleService, 'disable').and.returnValue(observableThrowError('x'));
137       spyOn(mgrModuleService, 'list').and.returnValue(observableOf([]));
138       component.selection.selected.push({
139         name: 'bar',
140         enabled: true
141       });
142       component.selection.update();
143       component.updateModuleState();
144       tick(2000);
145       expect(mgrModuleService.disable).toHaveBeenCalledWith('bar');
146       expect(mgrModuleService.list).toHaveBeenCalledTimes(1);
147       expect(notificationService.suspendToasties).toHaveBeenCalledTimes(2);
148       expect(component.blockUI.start).toHaveBeenCalled();
149       expect(component.blockUI.stop).toHaveBeenCalled();
150       expect(component.table.refreshBtn).toHaveBeenCalled();
151     }));
152   });
153 });