]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
e306af50 | 3 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
11fdf7f2 TL |
4 | import { RouterTestingModule } from '@angular/router/testing'; |
5 | ||
f67539c2 | 6 | import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; |
494da23a | 7 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
8 | import { of as observableOf, throwError as observableThrowError } from 'rxjs'; |
9 | ||
f67539c2 TL |
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'; | |
11fdf7f2 TL |
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: [ | |
e306af50 | 28 | BrowserAnimationsModule, |
11fdf7f2 TL |
29 | RouterTestingModule, |
30 | SharedModule, | |
31 | HttpClientTestingModule, | |
f67539c2 | 32 | NgbNavModule, |
494da23a | 33 | ToastrModule.forRoot() |
11fdf7f2 | 34 | ], |
f67539c2 | 35 | providers: [MgrModuleService, NotificationService] |
11fdf7f2 TL |
36 | }); |
37 | ||
38 | beforeEach(() => { | |
39 | fixture = TestBed.createComponent(MgrModuleListComponent); | |
40 | component = fixture.componentInstance; | |
f67539c2 TL |
41 | mgrModuleService = TestBed.inject(MgrModuleService); |
42 | notificationService = TestBed.inject(NotificationService); | |
11fdf7f2 TL |
43 | }); |
44 | ||
45 | it('should create', () => { | |
46 | fixture.detectChanges(); | |
47 | expect(component).toBeTruthy(); | |
48 | }); | |
49 | ||
9f95a23c TL |
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 | } | |
11fdf7f2 TL |
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([])); | |
9f95a23c | 98 | component.selection.add({ |
11fdf7f2 | 99 | name: 'foo', |
92f5a8d4 TL |
100 | enabled: false, |
101 | always_on: false | |
11fdf7f2 | 102 | }); |
11fdf7f2 TL |
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([])); | |
9f95a23c | 117 | component.selection.add({ |
11fdf7f2 | 118 | name: 'bar', |
92f5a8d4 TL |
119 | enabled: true, |
120 | always_on: false | |
11fdf7f2 | 121 | }); |
11fdf7f2 TL |
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 | })); | |
92f5a8d4 | 131 | |
f67539c2 TL |
132 | it.only('should not disable module without selecting one', () => { |
133 | expect(component.getTableActionDisabledDesc()).toBeTruthy(); | |
134 | }); | |
135 | ||
136 | it('should not disable dashboard module', () => { | |
92f5a8d4 TL |
137 | component.selection.selected = [ |
138 | { | |
139 | name: 'dashboard' | |
140 | } | |
141 | ]; | |
f67539c2 | 142 | expect(component.getTableActionDisabledDesc()).toBeTruthy(); |
92f5a8d4 TL |
143 | }); |
144 | ||
f67539c2 | 145 | it('should not disable an always-on module', () => { |
92f5a8d4 TL |
146 | component.selection.selected = [ |
147 | { | |
148 | name: 'bar', | |
149 | always_on: true | |
150 | } | |
151 | ]; | |
f67539c2 | 152 | expect(component.getTableActionDisabledDesc()).toBe('This Manager module is always on.'); |
92f5a8d4 | 153 | }); |
11fdf7f2 TL |
154 | }); |
155 | }); |