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';
7 import _ = require('lodash');
8 import { of } from 'rxjs';
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';
15 describe('OsdSmartListComponent', () => {
16 let component: OsdSmartListComponent;
17 let fixture: ComponentFixture<OsdSmartListComponent>;
18 let osdService: OsdService;
20 const SMART_DATA_VERSION_1_0 = require('./fixtures/smart_data_version_1_0_response.json');
22 const spyOnGetSmartData = (fn: (id: number) => any) =>
23 spyOn(osdService, 'getSmartData').and.callFake(fn);
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.
30 const initializeComponentWithData = (data?: any, simpleChanges?: SimpleChanges) => {
31 spyOnGetSmartData(() => of(data || SMART_DATA_VERSION_1_0));
33 const changes: SimpleChanges = simpleChanges || {
34 osdId: new SimpleChange(null, 0, true)
36 component.ngOnChanges(changes);
40 * Sets attributes for _all_ returned devices according to the given path. The syntax is the same
41 * as used in lodash.set().
44 * patchData('json_format_version', [2, 0]) // sets the value of `json_format_version` to [2, 0]
47 * patchData('json_format_version[0]', 2) // same result
49 * @param path The path to the attribute
50 * @param newValue The new value
52 const patchData = (path: string, newValue: any): any => {
54 _.cloneDeep(SMART_DATA_VERSION_1_0),
55 (result, dataObj, deviceId) => {
56 result[deviceId] = _.set(dataObj, path, newValue);
64 declarations: [OsdSmartListComponent],
65 imports: [TabsModule, SharedModule, HttpClientTestingModule],
66 providers: [i18nProviders, TabsetComponent, TabsetConfig]
70 fixture = TestBed.createComponent(OsdSmartListComponent);
71 component = fixture.componentInstance;
72 fixture.detectChanges();
74 osdService = TestBed.get(OsdService);
77 it('should create', () => {
78 expect(component).toBeTruthy();
81 describe('tests version 1.x', () => {
82 beforeEach(() => initializeComponentWithData());
84 it('should return with proper keys', () => {
85 _.each(component.data, (smartData, _deviceId) => {
86 expect(_.keys(smartData)).toEqual(['info', 'smart', 'device', 'identifier']);
90 it('should not contain excluded keys in `info`', () => {
92 'ata_smart_attributes',
93 'ata_smart_selective_self_test_log',
96 _.each(component.data, (smartData, _deviceId) => {
97 _.each(excludes, (exclude) => expect(smartData.info[exclude]).toBeUndefined());
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();
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');
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');