summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-devices-selection-modal/osd-devices-selection-modal.component.spec.ts
blob: 60ef65d0517c1a9b120af626d2d4f70a785272b8 (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
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';

import { InventoryDevice } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-device.model';
import { InventoryDevicesComponent } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-devices.component';
import { CdTableColumnFiltersChange } from '~/app/shared/models/cd-table-column-filters-change';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed, Mocks } from '~/testing/unit-test-helper';
import { OsdDevicesSelectionModalComponent } from './osd-devices-selection-modal.component';

describe('OsdDevicesSelectionModalComponent', () => {
  let component: OsdDevicesSelectionModalComponent;
  let fixture: ComponentFixture<OsdDevicesSelectionModalComponent>;
  let timeoutFn: Function;

  const devices: InventoryDevice[] = [Mocks.getInventoryDevice('node0', '1')];

  const expectSubmitButton = (enabled: boolean) => {
    const nativeElement = fixture.debugElement.nativeElement;
    const button = nativeElement.querySelector('.modal-footer .tc_submitButton');
    expect(button.disabled).toBe(!enabled);
  };

  configureTestBed({
    imports: [
      BrowserAnimationsModule,
      FormsModule,
      HttpClientTestingModule,
      SharedModule,
      ReactiveFormsModule,
      RouterTestingModule,
      ToastrModule.forRoot()
    ],
    providers: [NgbActiveModal],
    declarations: [OsdDevicesSelectionModalComponent, InventoryDevicesComponent]
  });

  beforeEach(() => {
    spyOn(window, 'setTimeout').and.callFake((fn) => (timeoutFn = fn));

    fixture = TestBed.createComponent(OsdDevicesSelectionModalComponent);
    component = fixture.componentInstance;
    component.devices = devices;

    // Mocks InventoryDeviceComponent
    component.inventoryDevices = {
      columns: [
        { name: 'Device path', prop: 'path' },
        {
          name: 'Type',
          prop: 'human_readable_type'
        },
        {
          name: 'Available',
          prop: 'available'
        }
      ]
    } as InventoryDevicesComponent;
    // Mocks the update from the above component
    component.filterColumns = ['path', 'human_readable_type'];
    fixture.detectChanges();
  });

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

  it('should disable submit button initially', () => {
    expectSubmitButton(false);
  });

  it(
    'should update requiredFilters after ngAfterViewInit is called to prevent ' +
      'ExpressionChangedAfterItHasBeenCheckedError',
    () => {
      expect(component.requiredFilters).toEqual([]);
      timeoutFn();
      expect(component.requiredFilters).toEqual(['Device path', 'Type']);
    }
  );

  it('should enable submit button after filtering some devices', () => {
    const event: CdTableColumnFiltersChange = {
      filters: [
        {
          name: 'hostname',
          prop: 'hostname',
          value: { raw: 'node0', formatted: 'node0' }
        },
        {
          name: 'size',
          prop: 'size',
          value: { raw: '1024', formatted: '1KiB' }
        }
      ],
      data: devices,
      dataOut: []
    };
    component.onFilterChange(event);
    fixture.detectChanges();
    expectSubmitButton(true);
  });
});