Add more UT for scanner

Signed-off-by: sshijun <sshijun@vmware.com>
This commit is contained in:
sshijun 2019-11-11 13:56:24 +08:00
parent 06e4e124d8
commit 22a1a7bb52
3 changed files with 54 additions and 15 deletions

View File

@ -1,4 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, ComponentFixtureAutoDetect, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { ClarityModule } from "@clr/angular"; import { ClarityModule } from "@clr/angular";
import { of } from "rxjs"; import { of } from "rxjs";
@ -39,6 +39,9 @@ describe('ConfigurationScannerComponent', () => {
}, },
getScanners() { getScanners() {
return of([mockScanner1]); return of([mockScanner1]);
},
updateScanner() {
return of(true);
} }
}; };
beforeEach(async(() => { beforeEach(async(() => {
@ -60,6 +63,8 @@ describe('ConfigurationScannerComponent', () => {
ConfirmationDialogService, ConfirmationDialogService,
TranslateService, TranslateService,
{ provide: ConfigScannerService, useValue: fakedConfigScannerService }, { provide: ConfigScannerService, useValue: fakedConfigScannerService },
// open auto detect
{ provide: ComponentFixtureAutoDetect, useValue: true }
] ]
}) })
.compileComponents(); .compileComponents();
@ -81,4 +86,14 @@ describe('ConfigurationScannerComponent', () => {
expect(el.getAttribute('disable')).toBeFalsy(); expect(el.getAttribute('disable')).toBeFalsy();
}); });
}); });
it('edit a scanner', () => {
component.selectedRow = mockScanner1;
component.editScanner();
expect(component.newScannerDialog.opened).toBeTruthy();
fixture.detectChanges();
fixture.nativeElement.querySelector('#scanner-name').value = 'test456';
fixture.nativeElement.querySelector('#button-save').click();
fixture.detectChanges();
expect(component.newScannerDialog.opened).toBeFalsy();
});
}); });

View File

@ -87,7 +87,7 @@
</clr-datagrid> </clr-datagrid>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-outline" (click)="close()">{{'BUTTON.CANCEL' | translate}}</button> <button id="cancel" type="button" class="btn btn-outline" (click)="close()">{{'BUTTON.CANCEL' | translate}}</button>
<button type="button" [clrLoading]="saveBtnState" class="btn btn-primary" [disabled]="!valid" (click)="save()">{{'BUTTON.OK' | translate}}</button> <button id="save-scanner" type="button" [clrLoading]="saveBtnState" class="btn btn-primary" [disabled]="!valid" (click)="save()">{{'BUTTON.OK' | translate}}</button>
</div> </div>
</clr-modal> </clr-modal>

View File

@ -9,14 +9,23 @@ import { ScannerComponent } from "./scanner.component";
import { ConfigScannerService } from "../../config/scanner/config-scanner.service"; import { ConfigScannerService } from "../../config/scanner/config-scanner.service";
import { SharedModule } from "../../shared/shared.module"; import { SharedModule } from "../../shared/shared.module";
import { ActivatedRoute } from "@angular/router"; import { ActivatedRoute } from "@angular/router";
import { Scanner } from "../../config/scanner/scanner";
xdescribe('ScannerComponent', () => { describe('ScannerComponent', () => {
let mockScanner1 = { const mockScanner1: Scanner = {
uuid: 'abc',
name: 'test1', name: 'test1',
description: 'just a sample', description: 'just a sample',
version: '1.0.0', version: '1.0.0',
url: 'http://168.0.0.1' url: 'http://168.0.0.1'
}; };
const mockScanner2: Scanner = {
uuid: 'def',
name: 'test2',
description: 'just a sample',
version: '2.0.0',
url: 'http://168.0.0.2'
};
let component: ScannerComponent; let component: ScannerComponent;
let fixture: ComponentFixture<ScannerComponent>; let fixture: ComponentFixture<ScannerComponent>;
let fakedConfigScannerService = { let fakedConfigScannerService = {
@ -24,7 +33,13 @@ xdescribe('ScannerComponent', () => {
return of(mockScanner1); return of(mockScanner1);
}, },
getScanners() { getScanners() {
return of([mockScanner1]); return of([mockScanner1, mockScanner2]);
},
getProjectScanners() {
return of([mockScanner1, mockScanner2]);
},
updateProjectScanner() {
return of(true);
} }
}; };
let fakedRoute = { let fakedRoute = {
@ -57,21 +72,30 @@ xdescribe('ScannerComponent', () => {
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(ScannerComponent); fixture = TestBed.createComponent(ScannerComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
spyOn(component, 'getPermission').and.returnValue(undefined);
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should creat', () => { it('should creat', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
it('should get scanner and render', () => { it('should get scanner and render', () => {
fixture.whenStable().then(() => { component.hasCreatePermission = true;
let el: HTMLElement = fixture.nativeElement.querySelector('#scanner-name'); let el: HTMLElement = fixture.nativeElement.querySelector('#scanner-name');
expect(el.textContent.trim).toEqual('test1'); expect(el.textContent.trim()).toEqual('test1');
});
}); });
it('should get scanners and edit button is available', () => { it('select another scanner', () => {
fixture.whenStable().then(() => { component.hasCreatePermission = true;
let el: HTMLElement = fixture.nativeElement.querySelector('#edit-scanner'); component.getScanners();
expect(el).toBeTruthy(); fixture.detectChanges();
}); const editButton = fixture.nativeElement.querySelector('#edit-scanner');
expect(editButton).toBeTruthy();
editButton.click();
fixture.detectChanges();
component.selectedScanner = mockScanner2;
fixture.detectChanges();
const saveButton = fixture.nativeElement.querySelector('#save-scanner');
saveButton.click();
fixture.detectChanges();
expect(component.opened).toBeFalsy();
}); });
}); });