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