summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/inventory/inventory-devices/inventory-devices.component.spec.ts
blob: 29a3ece96d8ae6c695283f5ea3610ef9d368ede9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';

import { ToastrModule } from 'ngx-toastr';

import { HostService } from '~/app/shared/api/host.service';
import { OrchestratorService } from '~/app/shared/api/orchestrator.service';
import { TableActionsComponent } from '~/app/shared/datatable/table-actions/table-actions.component';
import { CdTableAction } from '~/app/shared/models/cd-table-action';
import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
import { OrchestratorFeature } from '~/app/shared/models/orchestrator.enum';
import { OrchestratorStatus } from '~/app/shared/models/orchestrator.interface';
import { Permissions } from '~/app/shared/models/permissions';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed } from '~/testing/unit-test-helper';
import { InventoryDevicesComponent } from './inventory-devices.component';

describe('InventoryDevicesComponent', () => {
  let component: InventoryDevicesComponent;
  let fixture: ComponentFixture<InventoryDevicesComponent>;
  let orchService: OrchestratorService;
  let hostService: HostService;

  const fakeAuthStorageService = {
    getPermissions: () => {
      return new Permissions({ osd: ['read', 'update', 'create', 'delete'] });
    }
  };

  const mockOrchStatus = (available: boolean, features?: OrchestratorFeature[]) => {
    const orchStatus: OrchestratorStatus = { available: available, message: '', features: {} };
    if (features) {
      features.forEach((feature: OrchestratorFeature) => {
        orchStatus.features[feature] = { available: true };
      });
    }
    component.orchStatus = orchStatus;
  };

  configureTestBed({
    imports: [
      BrowserAnimationsModule,
      FormsModule,
      HttpClientTestingModule,
      SharedModule,
      RouterTestingModule,
      ToastrModule.forRoot()
    ],
    providers: [
      { provide: AuthStorageService, useValue: fakeAuthStorageService },
      TableActionsComponent
    ],
    declarations: [InventoryDevicesComponent]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(InventoryDevicesComponent);
    component = fixture.componentInstance;
    hostService = TestBed.inject(HostService);
    orchService = TestBed.inject(OrchestratorService);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should have columns that are sortable', () => {
    expect(component.columns.every((column) => Boolean(column.prop))).toBeTruthy();
  });

  it('should call inventoryDataList only when showOnlyAvailableData is true', () => {
    const hostServiceSpy = spyOn(hostService, 'inventoryDeviceList').and.callThrough();
    component.getDevices();
    expect(hostServiceSpy).toBeCalledTimes(0);
    component.showAvailDeviceOnly = true;
    component.getDevices();
    expect(hostServiceSpy).toBeCalledTimes(1);
  });

  describe('table actions', () => {
    const fakeDevices = require('./fixtures/inventory_list_response.json');

    beforeEach(() => {
      component.devices = fakeDevices;
      component.selectionType = 'single';
      fixture.detectChanges();
    });

    const verifyTableActions = async (
      tableActions: CdTableAction[],
      expectResult: {
        [action: string]: { disabled: boolean; disableDesc: string };
      }
    ) => {
      fixture.detectChanges();
      await fixture.whenStable();
      const tableActionElement = fixture.debugElement.query(By.directive(TableActionsComponent));
      // There is actually only one action for now
      const actions = {};
      tableActions.forEach((action) => {
        const actionElement = tableActionElement.query(By.css('button'));
        actions[action.name] = {
          disabled: actionElement.classes.disabled,
          disableDesc: actionElement.properties.title
        };
      });
      expect(actions).toEqual(expectResult);
    };

    const testTableActions = async (
      orch: boolean,
      features: OrchestratorFeature[],
      tests: { selectRow?: number; expectResults: any }[]
    ) => {
      mockOrchStatus(orch, features);
      fixture.detectChanges();
      await fixture.whenStable();

      for (const test of tests) {
        if (test.selectRow) {
          component.selection = new CdTableSelection();
          component.selection.selected = [test.selectRow];
        }
        await verifyTableActions(component.tableActions, test.expectResults);
      }
    };

    it('should have correct states when Orchestrator is enabled', async () => {
      const tests = [
        {
          expectResults: {
            Identify: { disabled: true, disableDesc: '' }
          }
        },
        {
          selectRow: fakeDevices[0],
          expectResults: {
            Identify: { disabled: false, disableDesc: '' }
          }
        }
      ];

      const features = [OrchestratorFeature.DEVICE_BLINK_LIGHT];
      await testTableActions(true, features, tests);
    });

    it('should have correct states when Orchestrator is disabled', async () => {
      const resultNoOrchestrator = {
        disabled: true,
        disableDesc: orchService.disableMessages.noOrchestrator
      };
      const tests = [
        {
          expectResults: {
            Identify: { disabled: true, disableDesc: '' }
          }
        },
        {
          selectRow: fakeDevices[0],
          expectResults: {
            Identify: resultNoOrchestrator
          }
        }
      ];
      await testTableActions(false, [], tests);
    });

    it('should have correct states when Orchestrator features are missing', async () => {
      const resultMissingFeatures = {
        disabled: true,
        disableDesc: orchService.disableMessages.missingFeature
      };
      const expectResults = [
        {
          expectResults: {
            Identify: { disabled: true, disableDesc: '' }
          }
        },
        {
          selectRow: fakeDevices[0],
          expectResults: {
            Identify: resultMissingFeatures
          }
        }
      ];
      await testTableActions(true, [], expectResults);
    });
  });
});