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';
6 import { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
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],
32 BrowserAnimationsModule,
35 HttpClientTestingModule,
37 ToastrModule.forRoot()
39 providers: [MgrModuleService, NotificationService, i18nProviders]
43 fixture = TestBed.createComponent(MgrModuleListComponent);
44 component = fixture.componentInstance;
45 mgrModuleService = TestBed.get(MgrModuleService);
46 notificationService = TestBed.get(NotificationService);
49 it('should create', () => {
50 fixture.detectChanges();
51 expect(component).toBeTruthy();
54 it('should test all TableActions combinations', () => {
55 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
56 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
57 component.tableActions
60 expect(tableActions).toEqual({
61 'create,update,delete': {
62 actions: ['Edit', 'Enable', 'Disable'],
63 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
66 actions: ['Edit', 'Enable', 'Disable'],
67 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
71 primary: { multiple: '', executing: '', single: '', no: '' }
73 create: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } },
75 actions: ['Edit', 'Enable', 'Disable'],
76 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
79 actions: ['Edit', 'Enable', 'Disable'],
80 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
82 delete: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } },
85 primary: { multiple: '', executing: '', single: '', no: '' }
90 describe('should update module state', () => {
92 component.selection = new CdTableSelection();
93 spyOn(notificationService, 'suspendToasties');
94 spyOn(component.blockUI, 'start');
95 spyOn(component.blockUI, 'stop');
96 spyOn(component.table, 'refreshBtn');
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({
107 component.updateModuleState();
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();
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({
126 component.updateModuleState();
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();
136 it('should not disable module (1)', () => {
137 component.selection.selected = [
142 expect(component.isTableActionDisabled('enabled')).toBeTruthy();
145 it('should not disable module (2)', () => {
146 component.selection.selected = [
152 expect(component.isTableActionDisabled('enabled')).toBeTruthy();