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';
6 import { ToastModule } from 'ng2-toastr';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
8 import { of as observableOf, throwError as observableThrowError } from 'rxjs';
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';
23 describe('MgrModuleListComponent', () => {
24 let component: MgrModuleListComponent;
25 let fixture: ComponentFixture<MgrModuleListComponent>;
26 let mgrModuleService: MgrModuleService;
27 let notificationService: NotificationService;
30 declarations: [MgrModuleListComponent, MgrModuleDetailsComponent],
34 HttpClientTestingModule,
38 providers: [MgrModuleService, NotificationService, i18nProviders]
42 fixture = TestBed.createComponent(MgrModuleListComponent);
43 component = fixture.componentInstance;
44 mgrModuleService = TestBed.get(MgrModuleService);
45 notificationService = TestBed.get(NotificationService);
48 it('should create', () => {
49 fixture.detectChanges();
50 expect(component).toBeTruthy();
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;
58 const getTableActionComponent = (): TableActionsComponent => {
59 fixture.detectChanges();
60 return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
64 permissionHelper = new PermissionHelper(component.permission, () =>
65 getTableActionComponent()
68 fn: () => tableActions.getCurrentButton().name,
74 describe('with read and update', () => {
76 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
79 it('shows action button', () => permissionHelper.testScenarios(scenario));
81 it('shows all actions', () => {
82 expect(tableActions.tableActions.length).toBe(3);
83 expect(tableActions.tableActions).toEqual(component.tableActions);
87 describe('with only read', () => {
89 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
92 it('shows no main action', () => {
93 permissionHelper.testScenarios({
94 fn: () => tableActions.getCurrentButton(),
100 it('shows no actions', () => {
101 expect(tableActions.tableActions.length).toBe(0);
102 expect(tableActions.tableActions).toEqual([]);
107 describe('should update module state', () => {
109 component.selection = new CdTableSelection();
110 spyOn(notificationService, 'suspendToasties');
111 spyOn(component.blockUI, 'start');
112 spyOn(component.blockUI, 'stop');
113 spyOn(component.table, 'refreshBtn');
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({
123 component.selection.update();
124 component.updateModuleState();
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();
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({
142 component.selection.update();
143 component.updateModuleState();
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();