Commit 0bbc1088 authored by Vitali Stupin's avatar Vitali Stupin
Browse files

adding tests

parent b6905226
sonar.projectKey=xtss-catalogue sonar.projectKey=xtss-catalogue
sonar.sources=src sonar.sources=src
sonar.sourceEncoding=UTF-8 sonar.sourceEncoding=UTF-8
sonar.exclusions=**/node_modules/**,**/*.spec.ts sonar.exclusions=**/node_modules/**,**/*.spec.ts,src/environments/**,src/karma.conf.js,src/main.ts
sonar.tests=src sonar.tests=src
sonar.test.inclusions=**/*.spec.ts sonar.test.inclusions=**/*.spec.ts
sonar.typescript.lcov.reportPaths=coverage/methods/lcov.info sonar.typescript.lcov.reportPaths=coverage/methods/lcov.info
......
...@@ -2,6 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; ...@@ -2,6 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { HeaderComponent } from './header.component'; import { HeaderComponent } from './header.component';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
import { LanguagesService } from '../languages.service';
describe('HeaderComponent', () => { describe('HeaderComponent', () => {
let component: HeaderComponent; let component: HeaderComponent;
...@@ -27,4 +28,11 @@ describe('HeaderComponent', () => { ...@@ -27,4 +28,11 @@ describe('HeaderComponent', () => {
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
it('should set language', () => {
const languagesService: LanguagesService = TestBed.get(LanguagesService);
spyOn(languagesService, 'setLang').and.returnValue(null);
component.setLang('xxx');
expect(languagesService.setLang).toHaveBeenCalledWith('xxx');
});
}); });
import { TestBed } from '@angular/core/testing'; import { TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { LanguagesService } from './languages.service'; import { LanguagesService } from './languages.service';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
describe('LanguagesService', () => { describe('LanguagesService', () => {
beforeEach(() => TestBed.configureTestingModule({ beforeEach(() => TestBed.configureTestingModule({
...@@ -13,4 +13,28 @@ describe('LanguagesService', () => { ...@@ -13,4 +13,28 @@ describe('LanguagesService', () => {
const service: LanguagesService = TestBed.get(LanguagesService); const service: LanguagesService = TestBed.get(LanguagesService);
expect(service).toBeTruthy(); expect(service).toBeTruthy();
}); });
it('should set default lang with empty localStorage', () => {
const translateService: TranslateService = TestBed.get(TranslateService);
spyOn(translateService, 'setDefaultLang');
spyOn(window.localStorage, 'getItem').and.returnValue(undefined);
TestBed.get(LanguagesService);
expect(translateService.setDefaultLang).toHaveBeenCalledWith('est');
});
it('should set default lang from localStorage', () => {
const translateService: TranslateService = TestBed.get(TranslateService);
spyOn(translateService, 'setDefaultLang');
spyOn(window.localStorage, 'getItem').and.returnValue('ENG');
TestBed.get(LanguagesService);
expect(translateService.setDefaultLang).toHaveBeenCalledWith('eng');
});
it('should set language', () => {
const translateService: TranslateService = TestBed.get(TranslateService);
spyOn(translateService, 'use');
const service = TestBed.get(LanguagesService);
service.setLang('ENG');
expect(translateService.use).toHaveBeenCalledWith('eng');
});
}); });
...@@ -3,10 +3,12 @@ import { TranslateModule } from '@ngx-translate/core'; ...@@ -3,10 +3,12 @@ import { TranslateModule } from '@ngx-translate/core';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { SearchComponent } from './search.component'; import { SearchComponent } from './search.component';
import { SubsystemsService } from 'src/app/subsystems.service';
describe('SearchComponent', () => { describe('SearchComponent', () => {
let component: SearchComponent; let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>; let fixture: ComponentFixture<SearchComponent>;
let subsystemsService: SubsystemsService;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
...@@ -21,6 +23,10 @@ describe('SearchComponent', () => { ...@@ -21,6 +23,10 @@ describe('SearchComponent', () => {
})); }));
beforeEach(() => { beforeEach(() => {
subsystemsService = TestBed.get(SubsystemsService);
spyOn(subsystemsService, 'setNonEmpty').and.returnValue(null);
spyOn(subsystemsService, 'setLimit').and.returnValue(null);
spyOn(subsystemsService, 'setFilter').and.returnValue(null);
fixture = TestBed.createComponent(SearchComponent); fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
fixture.detectChanges(); fixture.detectChanges();
...@@ -29,4 +35,19 @@ describe('SearchComponent', () => { ...@@ -29,4 +35,19 @@ describe('SearchComponent', () => {
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
it('setNonEmpty should work', () => {
component.setNonEmpty(true);
expect(subsystemsService.setNonEmpty).toHaveBeenCalledWith(true);
});
it('setLimit should work', () => {
component.setLimit('50');
expect(subsystemsService.setLimit).toHaveBeenCalledWith('50');
});
it('setFilter should work', () => {
component.setFilter('test');
expect(subsystemsService.setFilter).toHaveBeenCalledWith('test');
});
}); });
...@@ -3,10 +3,15 @@ import { TranslateModule } from '@ngx-translate/core'; ...@@ -3,10 +3,15 @@ import { TranslateModule } from '@ngx-translate/core';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
import { SubsystemItemComponent } from './subsystem-item.component'; import { SubsystemItemComponent } from './subsystem-item.component';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { SubsystemsService } from 'src/app/subsystems.service';
import { Router } from '@angular/router';
import { PREVIEW_SIZE } from '../../config';
import { Method } from 'src/app/method';
describe('SubsystemItemComponent', () => { describe('SubsystemItemComponent', () => {
let component: SubsystemItemComponent; let component: SubsystemItemComponent;
let fixture: ComponentFixture<SubsystemItemComponent>; let fixture: ComponentFixture<SubsystemItemComponent>;
let subsystemsService: SubsystemsService;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
...@@ -17,29 +22,62 @@ describe('SubsystemItemComponent', () => { ...@@ -17,29 +22,62 @@ describe('SubsystemItemComponent', () => {
TranslateModule.forRoot(), TranslateModule.forRoot(),
HttpClientModule, HttpClientModule,
RouterTestingModule RouterTestingModule
],
providers: [
{ provide: Router, useValue: {
navigateByUrl: jasmine.createSpy('navigateByUrl')
}}
] ]
}) })
.compileComponents(); .compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
subsystemsService = TestBed.get(SubsystemsService);
spyOn(subsystemsService, 'getApiUrlBase').and.returnValue(null);
fixture = TestBed.createComponent(SubsystemItemComponent); fixture = TestBed.createComponent(SubsystemItemComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
component.subsystem = { component.subsystem = {
xRoadInstance: 'XRD', xRoadInstance: 'INST',
memberClass: 'CLASS', memberClass: 'CLASS',
memberCode: 'CODE', memberCode: 'CODE',
subsystemCode: 'SUB', subsystemCode: 'SUB',
subsystemStatus: 'OK', subsystemStatus: 'OK',
fullSubsystemName: 'XRD/CLASS/CODE/SUB', fullSubsystemName: 'INST/CLASS/CODE/SUB',
methods: [] methods: []
}; };
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
it('getApiUrlBase should work', () => {
component.getApiUrlBase();
expect(subsystemsService.getApiUrlBase).toHaveBeenCalled();
});
it('should preview methods', () => {
expect(component.getMethodsPreview().length).toBe(0);
for (let i = 0; i < PREVIEW_SIZE + 10; i++) {
component.subsystem.methods.push(new Method());
}
expect(component.getMethodsPreview().length).toBe(PREVIEW_SIZE);
});
it('should calculate methods not in preview', () => {
expect(component.getNotInPreview()).toBe(0);
for (let i = 0; i < PREVIEW_SIZE + 10; i++) {
component.subsystem.methods.push(new Method());
}
expect(component.getNotInPreview()).toBe(10);
});
it('should go to detail view', () => {
component.showDetail();
expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/INST/CLASS/CODE/SUB');
});
}); });
...@@ -5,8 +5,10 @@ import { RouterTestingModule } from '@angular/router/testing'; ...@@ -5,8 +5,10 @@ import { RouterTestingModule } from '@angular/router/testing';
import { SubsystemListComponent } from './subsystem-list.component'; import { SubsystemListComponent } from './subsystem-list.component';
import { Component, Input } from '@angular/core'; import { Component, Input } from '@angular/core';
import { Subsystem } from '../subsystem'; import { Subsystem } from '../subsystem';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute, Router, Scroll } from '@angular/router';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { SubsystemsService } from '../subsystems.service';
import { ViewportScroller } from '@angular/common';
@Component({selector: 'app-header', template: ''}) @Component({selector: 'app-header', template: ''})
class HeaderStubComponent {} class HeaderStubComponent {}
...@@ -20,6 +22,9 @@ class SubsystemItemStubComponent { ...@@ -20,6 +22,9 @@ class SubsystemItemStubComponent {
describe('SubsystemListComponent', () => { describe('SubsystemListComponent', () => {
let component: SubsystemListComponent; let component: SubsystemListComponent;
let fixture: ComponentFixture<SubsystemListComponent>; let fixture: ComponentFixture<SubsystemListComponent>;
let getInstanceSpy;
let getInstancesSpy;
let subsystemsService: SubsystemsService;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
...@@ -31,24 +36,85 @@ describe('SubsystemListComponent', () => { ...@@ -31,24 +36,85 @@ describe('SubsystemListComponent', () => {
], ],
imports: [ imports: [
TranslateModule.forRoot(), TranslateModule.forRoot(),
HttpClientModule, HttpClientModule
RouterTestingModule ],
providers: [
{ provide: ActivatedRoute, useValue: {
params: of({
instance: 'INST'
})
}},
{ provide: Router, useValue: {
events: of(new Scroll(null, [11, 12], null)),
navigateByUrl: jasmine.createSpy('navigateByUrl')
}}
] ]
}) })
.compileComponents(); .compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
// Mocks and spies subsystemsService = TestBed.get(SubsystemsService);
TestBed.get(ActivatedRoute).params = of({ getInstanceSpy = spyOn(subsystemsService, 'getInstance').and.returnValue('INST');
instance: 'EE' getInstancesSpy = spyOn(subsystemsService, 'getInstances').and.returnValue(['INST']);
}); spyOn(TestBed.get(ViewportScroller), 'scrollToPosition');
spyOn(subsystemsService, 'setInstance').and.returnValue(null);
spyOn(subsystemsService, 'getDefaultInstance').and.returnValue('DEFINST');
spyOn(TestBed.get(SubsystemsService), 'getApiUrlBase').and.returnValue('base');
});
it('should create', () => {
fixture = TestBed.createComponent(SubsystemListComponent); fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
fixture.detectChanges(); fixture.detectChanges();
expect(component).toBeTruthy();
}); });
it('should create', () => { it('should redirect on incorrect instance', () => {
expect(component).toBeTruthy(); getInstancesSpy.and.returnValue(['XXX']);
fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/DEFINST');
});
it('should detect when instance is not selected', () => {
getInstanceSpy.and.returnValue('');
fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(subsystemsService.setInstance).toHaveBeenCalledWith('INST');
});
it('should detect change instance', () => {
getInstanceSpy.and.returnValue('INST2');
getInstancesSpy.and.returnValue(['INST', 'INST2']);
fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(subsystemsService.setInstance).toHaveBeenCalledWith('INST');
});
it('should scroll to position', () => {
fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(TestBed.get(ViewportScroller).scrollToPosition).toHaveBeenCalledWith([11, 12]);
});
it('switchInstance should work', () => {
fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.switchInstance('NEWINST');
expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/NEWINST');
});
it('should receive service warnings', () => {
fixture = TestBed.createComponent(SubsystemListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
subsystemsService.warnings.emit('WARN');
expect(component.message).toBe('WARN');
}); });
}); });
...@@ -73,7 +73,7 @@ export class SubsystemListComponent implements OnInit, AfterViewInit, OnDestroy ...@@ -73,7 +73,7 @@ export class SubsystemListComponent implements OnInit, AfterViewInit, OnDestroy
} }
// Only reload on switching of instance or when no instance is selected yet on service side // Only reload on switching of instance or when no instance is selected yet on service side
if (this.getInstance() === '' || this.getInstance() !== params.instance) { if (this.getInstance() === '' || this.getInstance() !== params.instance) {
this.subsystemsService.setInstance(params.instance ? params.instance : this.subsystemsService.getDefaultInstance()); this.subsystemsService.setInstance(params.instance);
} }
}); });
} }
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { Component } from '@angular/core'; import { Component, EventEmitter } from '@angular/core';
import { SubsystemComponent } from './subsystem.component'; import { SubsystemComponent } from './subsystem.component';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
// import { ViewportScroller } from '@angular/common'; import { Router, ActivatedRoute, Scroll } from '@angular/router';
// import { SubsystemsService } from '../methods.service'; import { of, BehaviorSubject } from 'rxjs';
import { ActivatedRoute } from '@angular/router'; import { SubsystemsService } from '../subsystems.service';
import { of } from 'rxjs'; import { ViewportScroller } from '@angular/common';
@Component({selector: 'app-header', template: ''}) @Component({selector: 'app-header', template: ''})
class HeaderStubComponent {} class HeaderStubComponent {}
...@@ -15,6 +14,9 @@ class HeaderStubComponent {} ...@@ -15,6 +14,9 @@ class HeaderStubComponent {}
describe('SubsystemComponent', () => { describe('SubsystemComponent', () => {
let component: SubsystemComponent; let component: SubsystemComponent;
let fixture: ComponentFixture<SubsystemComponent>; let fixture: ComponentFixture<SubsystemComponent>;
let getInstanceSpy;
let getInstancesSpy;
let subsystemsService: SubsystemsService;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
...@@ -24,40 +26,114 @@ describe('SubsystemComponent', () => { ...@@ -24,40 +26,114 @@ describe('SubsystemComponent', () => {
], ],
imports: [ imports: [
TranslateModule.forRoot(), TranslateModule.forRoot(),
HttpClientModule, HttpClientModule
RouterTestingModule ],
]/*,
providers: [ providers: [
SubsystemsService { provide: ActivatedRoute, useValue: {
]*/ params: of({
instance: 'INST',
class: 'CLASS',
member: 'MEMBER',
subsystem: 'SYSTEM'
})
}},
{ provide: Router, useValue: {
events: of(new Scroll(null, [11, 12], null)),
navigateByUrl: jasmine.createSpy('navigateByUrl')
}}
]
}) })
.compileComponents(); .compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
// Mocks and spies subsystemsService = TestBed.get(SubsystemsService);
TestBed.get(ActivatedRoute).params = of({ getInstanceSpy = spyOn(subsystemsService, 'getInstance').and.returnValue('INST');
instance: 'EE', getInstancesSpy = spyOn(subsystemsService, 'getInstances').and.returnValue(['INST']);
class: 'CLASS', spyOn(TestBed.get(ViewportScroller), 'scrollToPosition');
member: 'MEMBER', spyOn(subsystemsService, 'setInstance').and.returnValue(null);
subsystem: 'SYSTEM' spyOn(TestBed.get(SubsystemsService), 'getApiUrlBase').and.returnValue('base');
}); subsystemsService.subsystemsSubject = new BehaviorSubject([
// spyOn(TestBed.get(ViewportScroller), "scrollToPosition").and.callFake(() => {}); {
memberClass: '',
subsystemCode: '',
xRoadInstance: '',
subsystemStatus: '',
memberCode: '',
fullSubsystemName: 'INST/CLASS/MEMBER/SYSTEM',
methods: []
}
]);
});
it('should create', () => {
fixture = TestBed.createComponent(SubsystemComponent); fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
fixture.detectChanges(); fixture.detectChanges();
expect(component).toBeTruthy();
}); });
/*afterEach(() => { it('should detect incorrect instance', () => {
TestBed.resetTestEnvironment() getInstancesSpy.and.returnValue(['XXX']);
});*/ fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(component.message).toBe('Incorrect instance!');
});
it('should create', () => { it('should detect when instance is not selected', () => {
expect(component).toBeTruthy(); getInstanceSpy.and.returnValue('');
fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(subsystemsService.setInstance).toHaveBeenCalledWith('INST');
});
it('should detect change instance', () => {
getInstanceSpy.and.returnValue('INST2');
getInstancesSpy.and.returnValue(['INST', 'INST2']);
fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(subsystemsService.setInstance).toHaveBeenCalledWith('INST');
});
it('should detect incorrect subsystem', () => {
subsystemsService.subsystemsSubject = new BehaviorSubject([]);
fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
expect(component.message).toBe('Subsystem "INST/CLASS/MEMBER/SYSTEM" cannot be found!');
}); });
/*it('scrollToPosition was called', async(() => { it('should scroll to position', () => {
expect(TestBed.get(ViewportScroller).scrollToPosition).toHaveBeenCalledWith([0, 0]) fixture = TestBed.createComponent(SubsystemComponent);
}));*/ component = fixture.componentInstance;
fixture.detectChanges();
expect(TestBed.get(ViewportScroller).scrollToPosition).toHaveBeenCalledWith([11, 12]);
});
it('getApiUrlBase should work', () => {
fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.getApiUrlBase();
expect(subsystemsService.getApiUrlBase).toHaveBeenCalled();
});
it('goToList should work', () => {
fixture = TestBed.createComponent(SubsystemComponent);
component = fixture.componentInstance;