1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { Component, Input } from '@angular/core';
3 import { ComponentFixture, TestBed } from '@angular/core/testing';
5 import { TreeModule } from 'angular-tree-component';
6 import * as _ from 'lodash';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of } from 'rxjs';
11 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { CephfsService } from '../../../shared/api/cephfs.service';
13 import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
14 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
15 import { SharedModule } from '../../../shared/shared.module';
16 import { CephfsClientsComponent } from '../cephfs-clients/cephfs-clients.component';
17 import { CephfsDetailComponent } from '../cephfs-detail/cephfs-detail.component';
18 import { CephfsDirectoriesComponent } from '../cephfs-directories/cephfs-directories.component';
19 import { CephfsTabsComponent } from './cephfs-tabs.component';
21 describe('CephfsTabsComponent', () => {
22 let component: CephfsTabsComponent;
23 let fixture: ComponentFixture<CephfsTabsComponent>;
24 let service: CephfsService;
31 clients: { status: ViewCacheStatus; data: any[] };
35 const getReload: any = () => component['reloadSubscriber'];
36 const setReload = (sth?: any) => (component['reloadSubscriber'] = sth);
37 const mockRunOutside = () => {
38 component['subscribeInterval'] = () => {
39 // It's mocked because the rxjs timer subscription ins't called through the use of 'tick'.
44 getReload().unsubscribed = true;
52 const setSelection = (selection: object[]) => {
53 component.selection.selected = selection;
54 component.ngOnChanges();
57 const selectFs = (id: number, name: string) => {
72 const updateData = () => {
73 component['data'] = _.cloneDeep(data);
74 component.softRefresh();
77 @Component({ selector: 'cd-cephfs-chart', template: '' })
78 class CephfsChartStubComponent {
87 HttpClientTestingModule,
89 ToastrModule.forRoot()
93 CephfsChartStubComponent,
94 CephfsDetailComponent,
95 CephfsDirectoriesComponent,
96 CephfsClientsComponent
98 providers: [i18nProviders]
102 fixture = TestBed.createComponent(CephfsTabsComponent);
103 component = fixture.componentInstance;
104 component.selection = new CdTableSelection();
109 mdsCounters: { a: { name: 'a', x: [], y: [] } },
112 status: ViewCacheStatus.ValueOk,
113 data: [{}, {}, {}, {}]
116 service = TestBed.get(CephfsService);
117 spyOn(service, 'getTabs').and.callFake(() => of(data));
119 fixture.detectChanges();
121 setReload(); // Clears rxjs timer subscription
124 it('should create', () => {
125 expect(component).toBeTruthy();
128 it('should resist invalid mds info', () => {
137 expect(component.grafanaId).toBe(undefined);
140 it('should find out the grafana id', () => {
141 selectFs(2, 'otherMds');
142 expect(component.grafanaId).toBe('otherMds');
145 it('should set default values on id change before api request', () => {
146 const defaultDetails: Record<string, any> = {
153 const defaultClients: Record<string, any> = {
155 status: ViewCacheStatus.ValueNone
157 component['subscribeInterval'] = () => {};
159 expect(component.clients).not.toEqual(defaultClients);
160 expect(component.details).not.toEqual(defaultDetails);
161 selectFs(2, 'otherMds');
162 expect(component.clients).toEqual(defaultClients);
163 expect(component.details).toEqual(defaultDetails);
166 it('should force data updates on tab change without api requests', () => {
167 const oldClients = component.clients;
168 const oldDetails = component.details;
170 expect(service.getTabs).toHaveBeenCalledTimes(0);
171 expect(component.details).not.toBe(oldDetails);
172 expect(component.clients).not.toBe(oldClients);
175 describe('handling of id change', () => {
177 setReload(); // Clears rxjs timer subscription
178 selectFs(2, 'otherMds');
179 old = getReload(); // Gets current subscription
182 it('should have called getDetails once', () => {
183 expect(component.details.pools.length).toBe(2);
184 expect(service.getTabs).toHaveBeenCalledTimes(1);
187 it('should not subscribe to an new interval for the same selection', () => {
188 expect(component.id).toBe(2);
189 expect(component.grafanaId).toBe('otherMds');
190 selectFs(2, 'otherMds');
191 expect(component.id).toBe(2);
192 expect(component.grafanaId).toBe('otherMds');
193 expect(getReload()).toBe(old);
196 it('should subscribe to an new interval', () => {
197 selectFs(3, 'anotherMds');
198 expect(getReload()).not.toBe(old); // Holds an new object
201 it('should unsubscribe the old interval if it exists', () => {
202 selectFs(3, 'anotherMds');
203 expect(old.unsubscribed).toBe(true);
206 it('should not unsubscribe if no interval exists', () => {
207 expect(() => component.ngOnDestroy()).not.toThrow();
210 it('should request the details of the new id', () => {
211 expect(service.getTabs).toHaveBeenCalledWith(2);
214 it('should should unsubscribe on deselect', () => {
216 expect(old.unsubscribed).toBe(true);
217 expect(getReload()).toBe(undefined); // Cleared timer subscription