]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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'; | |
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: [ | |
32 | RouterTestingModule, | |
33 | SharedModule, | |
34 | HttpClientTestingModule, | |
35 | TabsModule.forRoot(), | |
494da23a | 36 | ToastrModule.forRoot() |
11fdf7f2 TL |
37 | ], |
38 | providers: [MgrModuleService, NotificationService, i18nProviders] | |
39 | }); | |
40 | ||
41 | beforeEach(() => { | |
42 | fixture = TestBed.createComponent(MgrModuleListComponent); | |
43 | component = fixture.componentInstance; | |
44 | mgrModuleService = TestBed.get(MgrModuleService); | |
45 | notificationService = TestBed.get(NotificationService); | |
46 | }); | |
47 | ||
48 | it('should create', () => { | |
49 | fixture.detectChanges(); | |
50 | expect(component).toBeTruthy(); | |
51 | }); | |
52 | ||
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; | |
57 | ||
58 | const getTableActionComponent = (): TableActionsComponent => { | |
59 | fixture.detectChanges(); | |
60 | return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance; | |
61 | }; | |
62 | ||
63 | beforeEach(() => { | |
64 | permissionHelper = new PermissionHelper(component.permission, () => | |
65 | getTableActionComponent() | |
66 | ); | |
67 | scenario = { | |
68 | fn: () => tableActions.getCurrentButton().name, | |
69 | single: 'Edit', | |
70 | empty: 'Edit' | |
71 | }; | |
72 | }); | |
73 | ||
74 | describe('with read and update', () => { | |
75 | beforeEach(() => { | |
76 | tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0); | |
77 | }); | |
78 | ||
79 | it('shows action button', () => permissionHelper.testScenarios(scenario)); | |
80 | ||
81 | it('shows all actions', () => { | |
82 | expect(tableActions.tableActions.length).toBe(3); | |
83 | expect(tableActions.tableActions).toEqual(component.tableActions); | |
84 | }); | |
85 | }); | |
86 | ||
87 | describe('with only read', () => { | |
88 | beforeEach(() => { | |
89 | tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0); | |
90 | }); | |
91 | ||
92 | it('shows no main action', () => { | |
93 | permissionHelper.testScenarios({ | |
94 | fn: () => tableActions.getCurrentButton(), | |
95 | single: undefined, | |
96 | empty: undefined | |
97 | }); | |
98 | }); | |
99 | ||
100 | it('shows no actions', () => { | |
101 | expect(tableActions.tableActions.length).toBe(0); | |
102 | expect(tableActions.tableActions).toEqual([]); | |
103 | }); | |
104 | }); | |
105 | }); | |
106 | ||
107 | describe('should update module state', () => { | |
108 | beforeEach(() => { | |
109 | component.selection = new CdTableSelection(); | |
110 | spyOn(notificationService, 'suspendToasties'); | |
111 | spyOn(component.blockUI, 'start'); | |
112 | spyOn(component.blockUI, 'stop'); | |
113 | spyOn(component.table, 'refreshBtn'); | |
114 | }); | |
115 | ||
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({ | |
120 | name: 'foo', | |
92f5a8d4 TL |
121 | enabled: false, |
122 | always_on: false | |
11fdf7f2 TL |
123 | }); |
124 | component.selection.update(); | |
125 | component.updateModuleState(); | |
126 | tick(2000); | |
127 | tick(2000); | |
128 | expect(mgrModuleService.enable).toHaveBeenCalledWith('foo'); | |
129 | expect(mgrModuleService.list).toHaveBeenCalledTimes(2); | |
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 | })); | |
135 | ||
136 | it('should disable module', fakeAsync(() => { | |
137 | spyOn(mgrModuleService, 'disable').and.returnValue(observableThrowError('x')); | |
138 | spyOn(mgrModuleService, 'list').and.returnValue(observableOf([])); | |
139 | component.selection.selected.push({ | |
140 | name: 'bar', | |
92f5a8d4 TL |
141 | enabled: true, |
142 | always_on: false | |
11fdf7f2 TL |
143 | }); |
144 | component.selection.update(); | |
145 | component.updateModuleState(); | |
146 | tick(2000); | |
147 | expect(mgrModuleService.disable).toHaveBeenCalledWith('bar'); | |
148 | expect(mgrModuleService.list).toHaveBeenCalledTimes(1); | |
149 | expect(notificationService.suspendToasties).toHaveBeenCalledTimes(2); | |
150 | expect(component.blockUI.start).toHaveBeenCalled(); | |
151 | expect(component.blockUI.stop).toHaveBeenCalled(); | |
152 | expect(component.table.refreshBtn).toHaveBeenCalled(); | |
153 | })); | |
92f5a8d4 TL |
154 | |
155 | it('should not disable module (1)', () => { | |
156 | component.selection.selected = [ | |
157 | { | |
158 | name: 'dashboard' | |
159 | } | |
160 | ]; | |
161 | component.selection.update(); | |
162 | expect(component.isTableActionDisabled('enabled')).toBeTruthy(); | |
163 | }); | |
164 | ||
165 | it('should not disable module (2)', () => { | |
166 | component.selection.selected = [ | |
167 | { | |
168 | name: 'bar', | |
169 | always_on: true | |
170 | } | |
171 | ]; | |
172 | component.selection.update(); | |
173 | expect(component.isTableActionDisabled('enabled')).toBeTruthy(); | |
174 | }); | |
11fdf7f2 TL |
175 | }); |
176 | }); |