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 { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of as observableOf, throwError as observableThrowError } from 'rxjs';
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';
19 describe('MgrModuleListComponent', () => {
20 let component: MgrModuleListComponent;
21 let fixture: ComponentFixture<MgrModuleListComponent>;
22 let mgrModuleService: MgrModuleService;
23 let notificationService: NotificationService;
26 declarations: [MgrModuleListComponent, MgrModuleDetailsComponent],
28 BrowserAnimationsModule,
31 HttpClientTestingModule,
33 ToastrModule.forRoot()
35 providers: [MgrModuleService, NotificationService]
39 fixture = TestBed.createComponent(MgrModuleListComponent);
40 component = fixture.componentInstance;
41 mgrModuleService = TestBed.inject(MgrModuleService);
42 notificationService = TestBed.inject(NotificationService);
45 it('should create', () => {
46 fixture.detectChanges();
47 expect(component).toBeTruthy();
50 it('should test all TableActions combinations', () => {
51 const permissionHelper: PermissionHelper = new PermissionHelper(component.permission);
52 const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions(
53 component.tableActions
56 expect(tableActions).toEqual({
57 'create,update,delete': {
58 actions: ['Edit', 'Enable', 'Disable'],
59 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
62 actions: ['Edit', 'Enable', 'Disable'],
63 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
67 primary: { multiple: '', executing: '', single: '', no: '' }
69 create: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } },
71 actions: ['Edit', 'Enable', 'Disable'],
72 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
75 actions: ['Edit', 'Enable', 'Disable'],
76 primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' }
78 delete: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } },
81 primary: { multiple: '', executing: '', single: '', no: '' }
86 describe('should update module state', () => {
88 component.selection = new CdTableSelection();
89 spyOn(notificationService, 'suspendToasties');
90 spyOn(component.blockUI, 'start');
91 spyOn(component.blockUI, 'stop');
92 spyOn(component.table, 'refreshBtn');
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({
103 component.updateModuleState();
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();
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({
122 component.updateModuleState();
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();
132 it.only('should not disable module without selecting one', () => {
133 expect(component.getTableActionDisabledDesc()).toBeTruthy();
136 it('should not disable dashboard module', () => {
137 component.selection.selected = [
142 expect(component.getTableActionDisabledDesc()).toBeTruthy();
145 it('should not disable an always-on module', () => {
146 component.selection.selected = [
152 expect(component.getTableActionDisabledDesc()).toBe('This Manager module is always on.');