]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
3 | import { FormsModule } from '@angular/forms'; | |
f67539c2 | 4 | import { By } from '@angular/platform-browser'; |
e306af50 | 5 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
f67539c2 | 6 | import { RouterTestingModule } from '@angular/router/testing'; |
9f95a23c | 7 | |
9f95a23c TL |
8 | import { ToastrModule } from 'ngx-toastr'; |
9 | ||
f67539c2 TL |
10 | import { OrchestratorService } from '~/app/shared/api/orchestrator.service'; |
11 | import { TableActionsComponent } from '~/app/shared/datatable/table-actions/table-actions.component'; | |
12 | import { CdTableAction } from '~/app/shared/models/cd-table-action'; | |
13 | import { CdTableSelection } from '~/app/shared/models/cd-table-selection'; | |
14 | import { OrchestratorFeature } from '~/app/shared/models/orchestrator.enum'; | |
15 | import { OrchestratorStatus } from '~/app/shared/models/orchestrator.interface'; | |
16 | import { Permissions } from '~/app/shared/models/permissions'; | |
17 | import { AuthStorageService } from '~/app/shared/services/auth-storage.service'; | |
18 | import { SharedModule } from '~/app/shared/shared.module'; | |
19 | import { configureTestBed } from '~/testing/unit-test-helper'; | |
9f95a23c TL |
20 | import { InventoryDevicesComponent } from './inventory-devices.component'; |
21 | ||
22 | describe('InventoryDevicesComponent', () => { | |
23 | let component: InventoryDevicesComponent; | |
24 | let fixture: ComponentFixture<InventoryDevicesComponent>; | |
f67539c2 TL |
25 | let orchService: OrchestratorService; |
26 | ||
27 | const fakeAuthStorageService = { | |
28 | getPermissions: () => { | |
29 | return new Permissions({ osd: ['read', 'update', 'create', 'delete'] }); | |
30 | } | |
31 | }; | |
32 | ||
33 | const mockOrchStatus = (available: boolean, features?: OrchestratorFeature[]) => { | |
34 | const orchStatus: OrchestratorStatus = { available: available, message: '', features: {} }; | |
35 | if (features) { | |
36 | features.forEach((feature: OrchestratorFeature) => { | |
37 | orchStatus.features[feature] = { available: true }; | |
38 | }); | |
39 | } | |
40 | component.orchStatus = orchStatus; | |
41 | }; | |
9f95a23c TL |
42 | |
43 | configureTestBed({ | |
e306af50 TL |
44 | imports: [ |
45 | BrowserAnimationsModule, | |
46 | FormsModule, | |
47 | HttpClientTestingModule, | |
48 | SharedModule, | |
f67539c2 | 49 | RouterTestingModule, |
e306af50 TL |
50 | ToastrModule.forRoot() |
51 | ], | |
f67539c2 TL |
52 | providers: [ |
53 | { provide: AuthStorageService, useValue: fakeAuthStorageService }, | |
54 | TableActionsComponent | |
55 | ], | |
9f95a23c TL |
56 | declarations: [InventoryDevicesComponent] |
57 | }); | |
58 | ||
59 | beforeEach(() => { | |
60 | fixture = TestBed.createComponent(InventoryDevicesComponent); | |
61 | component = fixture.componentInstance; | |
f67539c2 | 62 | orchService = TestBed.inject(OrchestratorService); |
9f95a23c TL |
63 | }); |
64 | ||
65 | it('should create', () => { | |
66 | expect(component).toBeTruthy(); | |
67 | }); | |
68 | ||
69 | it('should have columns that are sortable', () => { | |
70 | expect(component.columns.every((column) => Boolean(column.prop))).toBeTruthy(); | |
71 | }); | |
f67539c2 TL |
72 | |
73 | describe('table actions', () => { | |
74 | const fakeDevices = require('./fixtures/inventory_list_response.json'); | |
75 | ||
76 | beforeEach(() => { | |
77 | component.devices = fakeDevices; | |
78 | component.selectionType = 'single'; | |
79 | fixture.detectChanges(); | |
80 | }); | |
81 | ||
82 | const verifyTableActions = async ( | |
83 | tableActions: CdTableAction[], | |
84 | expectResult: { | |
85 | [action: string]: { disabled: boolean; disableDesc: string }; | |
86 | } | |
87 | ) => { | |
88 | fixture.detectChanges(); | |
89 | await fixture.whenStable(); | |
90 | const tableActionElement = fixture.debugElement.query(By.directive(TableActionsComponent)); | |
91 | // There is actually only one action for now | |
92 | const actions = {}; | |
93 | tableActions.forEach((action) => { | |
94 | const actionElement = tableActionElement.query(By.css('button')); | |
95 | actions[action.name] = { | |
96 | disabled: actionElement.classes.disabled, | |
97 | disableDesc: actionElement.properties.title | |
98 | }; | |
99 | }); | |
100 | expect(actions).toEqual(expectResult); | |
101 | }; | |
102 | ||
103 | const testTableActions = async ( | |
104 | orch: boolean, | |
105 | features: OrchestratorFeature[], | |
106 | tests: { selectRow?: number; expectResults: any }[] | |
107 | ) => { | |
108 | mockOrchStatus(orch, features); | |
109 | fixture.detectChanges(); | |
110 | await fixture.whenStable(); | |
111 | ||
112 | for (const test of tests) { | |
113 | if (test.selectRow) { | |
114 | component.selection = new CdTableSelection(); | |
115 | component.selection.selected = [test.selectRow]; | |
116 | } | |
117 | await verifyTableActions(component.tableActions, test.expectResults); | |
118 | } | |
119 | }; | |
120 | ||
121 | it('should have correct states when Orchestrator is enabled', async () => { | |
122 | const tests = [ | |
123 | { | |
124 | expectResults: { | |
125 | Identify: { disabled: true, disableDesc: '' } | |
126 | } | |
127 | }, | |
128 | { | |
129 | selectRow: fakeDevices[0], | |
130 | expectResults: { | |
131 | Identify: { disabled: false, disableDesc: '' } | |
132 | } | |
133 | } | |
134 | ]; | |
135 | ||
136 | const features = [OrchestratorFeature.DEVICE_BLINK_LIGHT]; | |
137 | await testTableActions(true, features, tests); | |
138 | }); | |
139 | ||
140 | it('should have correct states when Orchestrator is disabled', async () => { | |
141 | const resultNoOrchestrator = { | |
142 | disabled: true, | |
143 | disableDesc: orchService.disableMessages.noOrchestrator | |
144 | }; | |
145 | const tests = [ | |
146 | { | |
147 | expectResults: { | |
148 | Identify: { disabled: true, disableDesc: '' } | |
149 | } | |
150 | }, | |
151 | { | |
152 | selectRow: fakeDevices[0], | |
153 | expectResults: { | |
154 | Identify: resultNoOrchestrator | |
155 | } | |
156 | } | |
157 | ]; | |
158 | await testTableActions(false, [], tests); | |
159 | }); | |
160 | ||
161 | it('should have correct states when Orchestrator features are missing', async () => { | |
162 | const resultMissingFeatures = { | |
163 | disabled: true, | |
164 | disableDesc: orchService.disableMessages.missingFeature | |
165 | }; | |
166 | const expectResults = [ | |
167 | { | |
168 | expectResults: { | |
169 | Identify: { disabled: true, disableDesc: '' } | |
170 | } | |
171 | }, | |
172 | { | |
173 | selectRow: fakeDevices[0], | |
174 | expectResults: { | |
175 | Identify: resultMissingFeatures | |
176 | } | |
177 | } | |
178 | ]; | |
179 | await testTableActions(true, [], expectResults); | |
180 | }); | |
181 | }); | |
9f95a23c | 182 | }); |