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 { ClarityModule } from "@clr/angular";
import { of } from "rxjs";
@ -39,6 +39,9 @@ describe('ConfigurationScannerComponent', () => {
},
getScanners() {
return of([mockScanner1]);
},
updateScanner() {
return of(true);
}
};
beforeEach(async(() => {
@ -60,6 +63,8 @@ describe('ConfigurationScannerComponent', () => {
ConfirmationDialogService,
TranslateService,
{ provide: ConfigScannerService, useValue: fakedConfigScannerService },
// open auto detect
{ provide: ComponentFixtureAutoDetect, useValue: true }
]
})
.compileComponents();
@ -81,4 +86,14 @@ describe('ConfigurationScannerComponent', () => {
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>
</div>
<div class="modal-footer">
<button 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="cancel" type="button" class="btn btn-outline" (click)="close()">{{'BUTTON.CANCEL' | translate}}</button>
<button id="save-scanner" type="button" [clrLoading]="saveBtnState" class="btn btn-primary" [disabled]="!valid" (click)="save()">{{'BUTTON.OK' | translate}}</button>
</div>
</clr-modal>

View File

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