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';
6 import _ = require('lodash');
7 import { of } from 'rxjs';
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';
14 describe('OsdSmartListComponent', () => {
15 let component: OsdSmartListComponent;
16 let fixture: ComponentFixture<OsdSmartListComponent>;
17 let osdService: OsdService;
19 const SMART_DATA_VERSION_1_0 = require('./fixtures/smart_data_version_1_0_response.json');
21 const spyOnGetSmartData = (fn: (id: number) => any) =>
22 spyOn(osdService, 'getSmartData').and.callFake(fn);
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.
29 const initializeComponentWithData = (data?: any, simpleChanges?: SimpleChanges) => {
30 spyOnGetSmartData(() => of(data || SMART_DATA_VERSION_1_0));
32 const changes: SimpleChanges = simpleChanges || {
33 osdId: new SimpleChange(null, 0, true)
35 component.ngOnChanges(changes);
39 * Sets attributes for _all_ returned devices according to the given path. The syntax is the same
40 * as used in lodash.set().
43 * patchData('json_format_version', [2, 0]) // sets the value of `json_format_version` to [2, 0]
46 * patchData('json_format_version[0]', 2) // same result
48 * @param path The path to the attribute
49 * @param newValue The new value
51 const patchData = (path: string, newValue: any): any => {
53 _.cloneDeep(SMART_DATA_VERSION_1_0),
54 (result, dataObj, deviceId) => {
55 result[deviceId] = _.set(dataObj, path, newValue);
63 declarations: [OsdSmartListComponent],
64 imports: [TabsModule, SharedModule, HttpClientTestingModule],
65 providers: [i18nProviders]
69 fixture = TestBed.createComponent(OsdSmartListComponent);
70 component = fixture.componentInstance;
71 fixture.detectChanges();
73 osdService = TestBed.get(OsdService);
76 it('should create', () => {
77 expect(component).toBeTruthy();
80 describe('tests version 1.x', () => {
81 beforeEach(() => initializeComponentWithData());
83 it('should return with proper keys', () => {
84 _.each(component.data, (smartData, _deviceId) => {
85 expect(_.keys(smartData)).toEqual(['info', 'smart', 'device', 'identifier']);
89 it('should not contain excluded keys in `info`', () => {
91 'ata_smart_attributes',
92 'ata_smart_selective_self_test_log',
95 _.each(component.data, (smartData, _deviceId) => {
96 _.each(excludes, (exclude) => expect(smartData.info[exclude]).toBeUndefined());
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();