summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/pool/pool-details/pool-details.component.spec.ts
blob: f30f954b5b65210c36f34b8718be135012d38792 (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
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ChangeDetectorRef } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';

import { RbdConfigurationListComponent } from '~/app/ceph/block/rbd-configuration-list/rbd-configuration-list.component';
import { Permissions } from '~/app/shared/models/permissions';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed, Mocks, TabHelper } from '~/testing/unit-test-helper';
import { PoolDetailsComponent } from './pool-details.component';

describe('PoolDetailsComponent', () => {
  let poolDetailsComponent: PoolDetailsComponent;
  let fixture: ComponentFixture<PoolDetailsComponent>;

  // Needed because of ChangeDetectionStrategy.OnPush
  // https://github.com/angular/angular/issues/12313#issuecomment-444623173
  let changeDetector: ChangeDetectorRef;
  const detectChanges = () => {
    poolDetailsComponent.ngOnChanges();
    changeDetector.detectChanges(); // won't call ngOnChanges on it's own but updates fixture
  };

  const updatePoolSelection = (selection: any) => {
    poolDetailsComponent.selection = selection;
    detectChanges();
  };

  const currentPoolUpdate = () => {
    updatePoolSelection(poolDetailsComponent.selection);
  };

  configureTestBed({
    imports: [
      BrowserAnimationsModule,
      NgbNavModule,
      SharedModule,
      HttpClientTestingModule,
      RouterTestingModule
    ],
    declarations: [PoolDetailsComponent, RbdConfigurationListComponent]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(PoolDetailsComponent);
    // Needed because of ChangeDetectionStrategy.OnPush
    // https://github.com/angular/angular/issues/12313#issuecomment-444623173
    changeDetector = fixture.componentRef.injector.get(ChangeDetectorRef);
    poolDetailsComponent = fixture.componentInstance;
    poolDetailsComponent.selection = undefined;
    poolDetailsComponent.permissions = new Permissions({
      grafana: ['read']
    });
    updatePoolSelection({ tiers: [0], pool: 0, pool_name: 'micro_pool' });
  });

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

  describe('Pool details tabset', () => {
    it('should recognize a tabset child', () => {
      detectChanges();
      const ngbNav = TabHelper.getNgbNav(fixture);
      expect(ngbNav).toBeDefined();
    });

    it('should not change the tabs active status when selection is the same as before', () => {
      const tabs = TabHelper.getNgbNavItems(fixture);
      expect(tabs[0].active).toBeTruthy();
      currentPoolUpdate();
      expect(tabs[0].active).toBeTruthy();

      const ngbNav = TabHelper.getNgbNav(fixture);
      ngbNav.select(tabs[1].id);
      expect(tabs[1].active).toBeTruthy();
      currentPoolUpdate();
      expect(tabs[1].active).toBeTruthy();
    });

    it('should filter out cdExecuting, cdIsBinary and all stats', () => {
      updatePoolSelection({
        prop1: 1,
        cdIsBinary: true,
        prop2: 2,
        cdExecuting: true,
        prop3: 3,
        stats: { anyStat: 3, otherStat: [1, 2, 3] }
      });
      const expectedPool = { prop1: 1, prop2: 2, prop3: 3 };
      expect(poolDetailsComponent.poolDetails).toEqual(expectedPool);
    });

    describe('Updates of shown data', () => {
      const expectedChange = (
        expected: {
          selectedPoolConfiguration?: object;
          poolDetails?: object;
        },
        newSelection: object,
        doesNotEqualOld = true
      ) => {
        const getData = () => {
          const data = {};
          Object.keys(expected).forEach((key) => (data[key] = poolDetailsComponent[key]));
          return data;
        };
        const oldData = getData();
        updatePoolSelection(newSelection);
        const newData = getData();
        if (doesNotEqualOld) {
          expect(expected).not.toEqual(oldData);
        } else {
          expect(expected).toEqual(oldData);
        }
        expect(expected).toEqual(newData);
      };

      it('should update shown data on change', () => {
        expectedChange(
          {
            poolDetails: {
              pg_num: 256,
              pg_num_target: 256,
              pg_placement_num: 256,
              pg_placement_num_target: 256,
              pool: 2,
              pool_name: 'somePool',
              type: 'replicated',
              size: 3
            }
          },
          Mocks.getPool('somePool', 2)
        );
      });

      it('should not update shown data if no detail has changed on pool refresh', () => {
        expectedChange(
          {
            poolDetails: {
              pool: 0,
              pool_name: 'micro_pool',
              tiers: [0]
            }
          },
          poolDetailsComponent.selection,
          false
        );
      });

      it('should show "Cache Tiers Details" tab if selected pool has "tiers"', () => {
        const tabsItem = TabHelper.getNgbNavItems(fixture);
        const tabsText = TabHelper.getTextContents(fixture);
        expect(poolDetailsComponent.selection['tiers'].length).toBe(1);
        expect(tabsItem.length).toBe(3);
        expect(tabsText[2]).toBe('Cache Tiers Details');
        expect(tabsItem[0].active).toBeTruthy();
      });

      it('should not show "Cache Tiers Details" tab if selected pool has no "tiers"', () => {
        updatePoolSelection({ tiers: [] });
        const tabs = TabHelper.getNgbNavItems(fixture);
        expect(tabs.length).toEqual(2);
        expect(tabs[0].active).toBeTruthy();
      });
    });
  });
});