]>
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 | ||
11fdf7f2 | 6 | import { TabsModule } from 'ngx-bootstrap/tabs'; |
494da23a | 7 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
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: [ | |
e306af50 | 32 | BrowserAnimationsModule, |
11fdf7f2 TL |
33 | RouterTestingModule, |
34 | SharedModule, | |
35 | HttpClientTestingModule, | |
36 | TabsModule.forRoot(), | |
494da23a | 37 | ToastrModule.forRoot() |
11fdf7f2 TL |
38 | ], |
39 | providers: [MgrModuleService, NotificationService, i18nProviders] | |
40 | }); | |
41 | ||
42 | beforeEach(() => { | |
43 | fixture = TestBed.createComponent(MgrModuleListComponent); | |
44 | component = fixture.componentInstance; | |
45 | mgrModuleService = TestBed.get(MgrModuleService); | |
46 | notificationService = TestBed.get(NotificationService); | |
47 | }); | |
48 | ||
49 | it('should create', () => { | |
50 | fixture.detectChanges(); | |
51 | expect(component).toBeTruthy(); | |
52 | }); | |
53 | ||
9f95a23c TL |
54 | it('should test all TableActions combinations', () => { |
55 | const permissionHelper: PermissionHelper = new PermissionHelper(component.permission); | |
56 | const tableActions: TableActionsComponent = permissionHelper.setPermissionsAndGetActions( | |
57 | component.tableActions | |
58 | ); | |
59 | ||
60 | expect(tableActions).toEqual({ | |
61 | 'create,update,delete': { | |
62 | actions: ['Edit', 'Enable', 'Disable'], | |
63 | primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } | |
64 | }, | |
65 | 'create,update': { | |
66 | actions: ['Edit', 'Enable', 'Disable'], | |
67 | primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } | |
68 | }, | |
69 | 'create,delete': { | |
70 | actions: [], | |
71 | primary: { multiple: '', executing: '', single: '', no: '' } | |
72 | }, | |
73 | create: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, | |
74 | 'update,delete': { | |
75 | actions: ['Edit', 'Enable', 'Disable'], | |
76 | primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } | |
77 | }, | |
78 | update: { | |
79 | actions: ['Edit', 'Enable', 'Disable'], | |
80 | primary: { multiple: 'Edit', executing: 'Edit', single: 'Edit', no: 'Edit' } | |
81 | }, | |
82 | delete: { actions: [], primary: { multiple: '', executing: '', single: '', no: '' } }, | |
83 | 'no-permissions': { | |
84 | actions: [], | |
85 | primary: { multiple: '', executing: '', single: '', no: '' } | |
86 | } | |
11fdf7f2 TL |
87 | }); |
88 | }); | |
89 | ||
90 | describe('should update module state', () => { | |
91 | beforeEach(() => { | |
92 | component.selection = new CdTableSelection(); | |
93 | spyOn(notificationService, 'suspendToasties'); | |
94 | spyOn(component.blockUI, 'start'); | |
95 | spyOn(component.blockUI, 'stop'); | |
96 | spyOn(component.table, 'refreshBtn'); | |
97 | }); | |
98 | ||
99 | it('should enable module', fakeAsync(() => { | |
100 | spyOn(mgrModuleService, 'enable').and.returnValue(observableThrowError('y')); | |
101 | spyOn(mgrModuleService, 'list').and.returnValues(observableThrowError('z'), observableOf([])); | |
9f95a23c | 102 | component.selection.add({ |
11fdf7f2 | 103 | name: 'foo', |
92f5a8d4 TL |
104 | enabled: false, |
105 | always_on: false | |
11fdf7f2 | 106 | }); |
11fdf7f2 TL |
107 | component.updateModuleState(); |
108 | tick(2000); | |
109 | tick(2000); | |
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(); | |
116 | })); | |
117 | ||
118 | it('should disable module', fakeAsync(() => { | |
119 | spyOn(mgrModuleService, 'disable').and.returnValue(observableThrowError('x')); | |
120 | spyOn(mgrModuleService, 'list').and.returnValue(observableOf([])); | |
9f95a23c | 121 | component.selection.add({ |
11fdf7f2 | 122 | name: 'bar', |
92f5a8d4 TL |
123 | enabled: true, |
124 | always_on: false | |
11fdf7f2 | 125 | }); |
11fdf7f2 TL |
126 | component.updateModuleState(); |
127 | tick(2000); | |
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(); | |
134 | })); | |
92f5a8d4 TL |
135 | |
136 | it('should not disable module (1)', () => { | |
137 | component.selection.selected = [ | |
138 | { | |
139 | name: 'dashboard' | |
140 | } | |
141 | ]; | |
92f5a8d4 TL |
142 | expect(component.isTableActionDisabled('enabled')).toBeTruthy(); |
143 | }); | |
144 | ||
145 | it('should not disable module (2)', () => { | |
146 | component.selection.selected = [ | |
147 | { | |
148 | name: 'bar', | |
149 | always_on: true | |
150 | } | |
151 | ]; | |
92f5a8d4 TL |
152 | expect(component.isTableActionDisabled('enabled')).toBeTruthy(); |
153 | }); | |
11fdf7f2 TL |
154 | }); |
155 | }); |