1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { FormsModule } from '@angular/forms';
4 import { By } from '@angular/platform-browser';
5 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
6 import { RouterTestingModule } from '@angular/router/testing';
8 import { ToastrModule } from 'ngx-toastr';
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';
20 import { InventoryDevicesComponent } from './inventory-devices.component';
22 describe('InventoryDevicesComponent', () => {
23 let component: InventoryDevicesComponent;
24 let fixture: ComponentFixture<InventoryDevicesComponent>;
25 let orchService: OrchestratorService;
27 const fakeAuthStorageService = {
28 getPermissions: () => {
29 return new Permissions({ osd: ['read', 'update', 'create', 'delete'] });
33 const mockOrchStatus = (available: boolean, features?: OrchestratorFeature[]) => {
34 const orchStatus: OrchestratorStatus = { available: available, message: '', features: {} };
36 features.forEach((feature: OrchestratorFeature) => {
37 orchStatus.features[feature] = { available: true };
40 component.orchStatus = orchStatus;
45 BrowserAnimationsModule,
47 HttpClientTestingModule,
50 ToastrModule.forRoot()
53 { provide: AuthStorageService, useValue: fakeAuthStorageService },
56 declarations: [InventoryDevicesComponent]
60 fixture = TestBed.createComponent(InventoryDevicesComponent);
61 component = fixture.componentInstance;
62 orchService = TestBed.inject(OrchestratorService);
65 it('should create', () => {
66 expect(component).toBeTruthy();
69 it('should have columns that are sortable', () => {
70 expect(component.columns.every((column) => Boolean(column.prop))).toBeTruthy();
73 describe('table actions', () => {
74 const fakeDevices = require('./fixtures/inventory_list_response.json');
77 component.devices = fakeDevices;
78 component.selectionType = 'single';
79 fixture.detectChanges();
82 const verifyTableActions = async (
83 tableActions: CdTableAction[],
85 [action: string]: { disabled: boolean; disableDesc: string };
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
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
100 expect(actions).toEqual(expectResult);
103 const testTableActions = async (
105 features: OrchestratorFeature[],
106 tests: { selectRow?: number; expectResults: any }[]
108 mockOrchStatus(orch, features);
109 fixture.detectChanges();
110 await fixture.whenStable();
112 for (const test of tests) {
113 if (test.selectRow) {
114 component.selection = new CdTableSelection();
115 component.selection.selected = [test.selectRow];
117 await verifyTableActions(component.tableActions, test.expectResults);
121 it('should have correct states when Orchestrator is enabled', async () => {
125 Identify: { disabled: true, disableDesc: '' }
129 selectRow: fakeDevices[0],
131 Identify: { disabled: false, disableDesc: '' }
136 const features = [OrchestratorFeature.DEVICE_BLINK_LIGHT];
137 await testTableActions(true, features, tests);
140 it('should have correct states when Orchestrator is disabled', async () => {
141 const resultNoOrchestrator = {
143 disableDesc: orchService.disableMessages.noOrchestrator
148 Identify: { disabled: true, disableDesc: '' }
152 selectRow: fakeDevices[0],
154 Identify: resultNoOrchestrator
158 await testTableActions(false, [], tests);
161 it('should have correct states when Orchestrator features are missing', async () => {
162 const resultMissingFeatures = {
164 disableDesc: orchService.disableMessages.missingFeature
166 const expectResults = [
169 Identify: { disabled: true, disableDesc: '' }
173 selectRow: fakeDevices[0],
175 Identify: resultMissingFeatures
179 await testTableActions(true, [], expectResults);