]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
826728477c473a4e0bf7cf1135ec5ef962128690
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { SimpleChange, SimpleChanges } from '@angular/core';
3 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import { By } from '@angular/platform-browser';
5 import { TabsetComponent, TabsetConfig, TabsModule } from 'ngx-bootstrap/tabs';
6
7 import _ = require('lodash');
8 import { of } from 'rxjs';
9
10 import { configureTestBed, i18nProviders } from '../../../../../testing/unit-test-helper';
11 import { OsdService } from '../../../../shared/api/osd.service';
12 import { SharedModule } from '../../../../shared/shared.module';
13 import { OsdSmartListComponent } from './osd-smart-list.component';
14
15 describe('OsdSmartListComponent', () => {
16   let component: OsdSmartListComponent;
17   let fixture: ComponentFixture<OsdSmartListComponent>;
18   let osdService: OsdService;
19
20   const SMART_DATA_VERSION_1_0 = require('./fixtures/smart_data_version_1_0_response.json');
21
22   const spyOnGetSmartData = (fn: (id: number) => any) =>
23     spyOn(osdService, 'getSmartData').and.callFake(fn);
24
25   /**
26    * Initializes the component after it spied upon the `getSmartData()` method of `OsdService`.
27    * @param data The data to be used to return when `getSmartData()` is called.
28    * @param simpleChanges (optional) The changes to be used for `ngOnChanges()` method.
29    */
30   const initializeComponentWithData = (data?: any, simpleChanges?: SimpleChanges) => {
31     spyOnGetSmartData(() => of(data || SMART_DATA_VERSION_1_0));
32     component.ngOnInit();
33     const changes: SimpleChanges = simpleChanges || {
34       osdId: new SimpleChange(null, 0, true)
35     };
36     component.ngOnChanges(changes);
37   };
38
39   /**
40    * Sets attributes for _all_ returned devices according to the given path. The syntax is the same
41    * as used in lodash.set().
42    *
43    * @example
44    * patchData('json_format_version', [2, 0])  // sets the value of `json_format_version` to [2, 0]
45    *                                           // for all devices
46    *
47    * patchData('json_format_version[0]', 2)    // same result
48    *
49    * @param path The path to the attribute
50    * @param newValue The new value
51    */
52   const patchData = (path: string, newValue: any): any => {
53     return _.reduce(
54       _.cloneDeep(SMART_DATA_VERSION_1_0),
55       (result, dataObj, deviceId) => {
56         result[deviceId] = _.set(dataObj, path, newValue);
57         return result;
58       },
59       {}
60     );
61   };
62
63   configureTestBed({
64     declarations: [OsdSmartListComponent],
65     imports: [TabsModule, SharedModule, HttpClientTestingModule],
66     providers: [i18nProviders, TabsetComponent, TabsetConfig]
67   });
68
69   beforeEach(() => {
70     fixture = TestBed.createComponent(OsdSmartListComponent);
71     component = fixture.componentInstance;
72     fixture.detectChanges();
73
74     osdService = TestBed.get(OsdService);
75   });
76
77   it('should create', () => {
78     expect(component).toBeTruthy();
79   });
80
81   describe('tests version 1.x', () => {
82     beforeEach(() => initializeComponentWithData());
83
84     it('should return with proper keys', () => {
85       _.each(component.data, (smartData, _deviceId) => {
86         expect(_.keys(smartData)).toEqual(['info', 'smart', 'device', 'identifier']);
87       });
88     });
89
90     it('should not contain excluded keys in `info`', () => {
91       const excludes = [
92         'ata_smart_attributes',
93         'ata_smart_selective_self_test_log',
94         'ata_smart_data'
95       ];
96       _.each(component.data, (smartData, _deviceId) => {
97         _.each(excludes, (exclude) => expect(smartData.info[exclude]).toBeUndefined());
98       });
99     });
100   });
101
102   it('should not work for version 2.x', () => {
103     initializeComponentWithData(patchData('json_format_version', [2, 0]));
104     expect(component.data).toEqual({});
105     expect(component.incompatible).toBeTruthy();
106   });
107
108   it('should display info panel for passed self test', () => {
109     initializeComponentWithData();
110     fixture.detectChanges();
111     const alertPanel = fixture.debugElement.query(By.css('cd-alert-panel'));
112     expect(component.incompatible).toBe(false);
113     expect(component.loading).toBe(false);
114     expect(alertPanel.attributes.size).toBe('slim');
115     expect(alertPanel.attributes.title).toBe('SMART overall-health self-assessment test result');
116     expect(alertPanel.attributes.type).toBe('info');
117   });
118
119   it('should display warning panel for failed self test', () => {
120     initializeComponentWithData(patchData('ata_smart_data.self_test.status.passed', false));
121     fixture.detectChanges();
122     const alertPanel = fixture.debugElement.query(By.css('cd-alert-panel'));
123     expect(component.incompatible).toBe(false);
124     expect(component.loading).toBe(false);
125     expect(alertPanel.attributes.size).toBe('slim');
126     expect(alertPanel.attributes.title).toBe('SMART overall-health self-assessment test result');
127     expect(alertPanel.attributes.type).toBe('warning');
128   });
129 });