Add more ut for ui

Signed-off-by: sshijun <sshijun@vmware.com>
This commit is contained in:
sshijun 2019-12-02 16:58:24 +08:00
parent e5f8c2d779
commit 60dfb87caa
7 changed files with 129 additions and 52 deletions

View File

@ -44,7 +44,7 @@ describe('ConfigurationScannerComponent', () => {
return of(true); return of(true);
} }
}; };
beforeEach(async(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
SharedModule, SharedModule,
@ -66,9 +66,8 @@ describe('ConfigurationScannerComponent', () => {
// open auto detect // open auto detect
{ provide: ComponentFixtureAutoDetect, useValue: true } { provide: ComponentFixtureAutoDetect, useValue: true }
] ]
}) });
.compileComponents(); });
}));
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(ConfigurationScannerComponent); fixture = TestBed.createComponent(ConfigurationScannerComponent);
component = fixture.componentInstance; component = fixture.componentInstance;

View File

@ -66,12 +66,12 @@ describe('NewScannerFormComponent', () => {
nameInput.dispatchEvent(new Event('input')); nameInput.dispatchEvent(new Event('input'));
nameInput.blur(); nameInput.blur();
nameInput.dispatchEvent(new Event('blur')); nameInput.dispatchEvent(new Event('blur'));
let el = null;
setTimeout(() => {
el = fixture.nativeElement.querySelector('#name-error');
expect(el).toBeTruthy();
}, 20000);
tick(20000); tick(20000);
const el = fixture.nativeElement.querySelector('#name-error');
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(el).toBeTruthy();
});
})); }));
it('name should be valid', fakeAsync(() => { it('name should be valid', fakeAsync(() => {
let nameInput = fixture.nativeElement.querySelector('#scanner-name'); let nameInput = fixture.nativeElement.querySelector('#scanner-name');
@ -79,12 +79,12 @@ describe('NewScannerFormComponent', () => {
nameInput.dispatchEvent(new Event('input')); nameInput.dispatchEvent(new Event('input'));
nameInput.blur(); nameInput.blur();
nameInput.dispatchEvent(new Event('blur')); nameInput.dispatchEvent(new Event('blur'));
let el = null;
setTimeout(() => {
el = fixture.nativeElement.querySelector('#name-error');
expect(el).toBeFalsy();
}, 20000);
tick(20000); tick(20000);
const el = fixture.nativeElement.querySelector('#name-error');
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(el).toBeFalsy();
});
})); }));
it('endpoint url should be valid', fakeAsync(() => { it('endpoint url should be valid', fakeAsync(() => {
@ -95,12 +95,12 @@ describe('NewScannerFormComponent', () => {
urlInput.dispatchEvent(new Event('input')); urlInput.dispatchEvent(new Event('input'));
urlInput.blur(); urlInput.blur();
urlInput.dispatchEvent(new Event('blur')); urlInput.dispatchEvent(new Event('blur'));
let el = null;
setTimeout(() => {
el = fixture.nativeElement.querySelector('#endpoint-error');
}, 20000);
tick(20000); tick(20000);
expect(el).toBeFalsy(); const el = fixture.nativeElement.querySelector('#endpoint-error');
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(el).toBeFalsy();
});
})); }));
it('auth should be valid', () => { it('auth should be valid', () => {

View File

@ -35,7 +35,7 @@ describe('NewScannerModalComponent', () => {
return of(true).pipe(delay(200)); return of(true).pipe(delay(200));
} }
}; };
beforeEach(async(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
SharedModule, SharedModule,
@ -52,9 +52,8 @@ describe('NewScannerModalComponent', () => {
// open auto detect // open auto detect
{ provide: ComponentFixtureAutoDetect, useValue: true } { provide: ComponentFixtureAutoDetect, useValue: true }
] ]
}) });
.compileComponents(); });
}));
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(NewScannerModalComponent); fixture = TestBed.createComponent(NewScannerModalComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
@ -63,6 +62,16 @@ describe('NewScannerModalComponent', () => {
component.newScannerFormComponent.checkEndpointUrlSubscribe = new Subscription(); component.newScannerFormComponent.checkEndpointUrlSubscribe = new Subscription();
fixture.detectChanges(); fixture.detectChanges();
}); });
afterEach(() => {
if (component && component.newScannerFormComponent && component.newScannerFormComponent.checkNameSubscribe) {
component.newScannerFormComponent.checkNameSubscribe.unsubscribe();
component.newScannerFormComponent.checkNameSubscribe = null;
}
if (component && component.newScannerFormComponent && component.newScannerFormComponent.checkEndpointUrlSubscribe) {
component.newScannerFormComponent.checkEndpointUrlSubscribe.unsubscribe();
component.newScannerFormComponent.checkEndpointUrlSubscribe = null;
}
});
it('should creat', () => { it('should creat', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
@ -97,10 +106,11 @@ describe('NewScannerModalComponent', () => {
expect(component.validForSaving).toBeTruthy(); expect(component.validForSaving).toBeTruthy();
el.click(); el.click();
el.dispatchEvent(new Event('click')); el.dispatchEvent(new Event('click'));
setTimeout(() => {
expect(component.opened).toBeFalsy();
}, 10000);
tick(10000); tick(10000);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.opened).toBeFalsy();
});
})); }));
it('test connection button should not be disabled', fakeAsync(() => { it('test connection button should not be disabled', fakeAsync(() => {
let nameInput = fixture.nativeElement.querySelector('#scanner-name'); let nameInput = fixture.nativeElement.querySelector('#scanner-name');
@ -114,10 +124,11 @@ describe('NewScannerModalComponent', () => {
el.click(); el.click();
el.dispatchEvent(new Event('click')); el.dispatchEvent(new Event('click'));
expect(component.checkBtnState).toBe(ClrLoadingState.LOADING); expect(component.checkBtnState).toBe(ClrLoadingState.LOADING);
setTimeout(() => {
expect(component.checkBtnState).toBe(ClrLoadingState.SUCCESS);
}, 10000);
tick(10000); tick(10000);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.checkBtnState).toBe(ClrLoadingState.SUCCESS);
});
})); }));
it('add button should not be disabled', fakeAsync(() => { it('add button should not be disabled', fakeAsync(() => {
fixture.nativeElement.querySelector('#scanner-name').value = "test2"; fixture.nativeElement.querySelector('#scanner-name').value = "test2";
@ -137,10 +148,11 @@ describe('NewScannerModalComponent', () => {
expect(component.valid).toBeFalsy(); expect(component.valid).toBeFalsy();
el.click(); el.click();
el.dispatchEvent(new Event('click')); el.dispatchEvent(new Event('click'));
setTimeout(() => {
expect(component.opened).toBeFalsy();
}, 10000);
tick(10000); tick(10000);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.opened).toBeFalsy();
});
})); }));
}); });

View File

@ -26,7 +26,7 @@ describe('ScannerMetadataComponent', () => {
return of(mockScannerMetadata); return of(mockScannerMetadata);
} }
}; };
beforeEach(async(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
SharedModule, SharedModule,
@ -40,9 +40,8 @@ describe('ScannerMetadataComponent', () => {
ErrorHandler, ErrorHandler,
{ provide: ConfigScannerService, useValue: fakedConfigScannerService }, { provide: ConfigScannerService, useValue: fakedConfigScannerService },
] ]
}) });
.compileComponents(); });
}));
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(ScannerMetadataComponent); fixture = TestBed.createComponent(ScannerMetadataComponent);
component = fixture.componentInstance; component = fixture.componentInstance;

View File

@ -20,4 +20,4 @@
</clr-dg-cell> </clr-dg-cell>
</clr-dg-row> </clr-dg-row>
<clr-dg-footer>{{'GC.LATEST_JOBS' | translate :{param: jobs.length} }}</clr-dg-footer> <clr-dg-footer>{{'GC.LATEST_JOBS' | translate :{param: jobs.length} }}</clr-dg-footer>
</clr-datagrid> </clr-datagrid>

View File

@ -1,4 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, ComponentFixtureAutoDetect, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { SharedModule } from '../../../../utils/shared/shared.module'; import { SharedModule } from '../../../../utils/shared/shared.module';
import { GcRepoService } from "../gc.service"; import { GcRepoService } from "../gc.service";
import { of } from 'rxjs'; import { of } from 'rxjs';
@ -6,22 +6,52 @@ import { GcViewModelFactory } from "../gc.viewmodel.factory";
import { TranslateModule, TranslateService } from '@ngx-translate/core'; import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { ErrorHandler } from '../../../../utils/error-handler'; import { ErrorHandler } from '../../../../utils/error-handler';
import { GcHistoryComponent } from './gc-history.component'; import { GcHistoryComponent } from './gc-history.component';
import { GcJobData } from "../gcLog";
describe('GcHistoryComponent', () => { describe('GcHistoryComponent', () => {
let component: GcHistoryComponent; let component: GcHistoryComponent;
let fixture: ComponentFixture<GcHistoryComponent>; let fixture: ComponentFixture<GcHistoryComponent>;
const mockJobs: GcJobData[] = [
{
id: 1,
job_name: 'test',
job_kind: 'manual',
schedule: null,
job_status: 'pending',
job_uuid: 'abc',
creation_time: null,
update_time: null,
delete: false
},
{
id: 2,
job_name: 'test',
job_kind: 'manual',
schedule: null,
job_status: 'finished',
job_uuid: 'bcd',
creation_time: null,
update_time: null,
delete: false
}
];
let fakeGcRepoService = { let fakeGcRepoService = {
getJobs: function () { count: 0,
return of([]); getJobs() {
if (this.count === 0) {
this.count += 1;
return of([mockJobs[0]]);
} else {
this.count += 1;
return of([mockJobs[1]]);
}
},
getLogLink() {
return null;
} }
}; };
let fakeGcViewModelFactory = { const fakeGcViewModelFactory = new GcViewModelFactory();
createJobViewModel: function (data) { beforeEach(() => {
return data;
}
};
beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [GcHistoryComponent], declarations: [GcHistoryComponent],
imports: [ imports: [
@ -31,19 +61,36 @@ describe('GcHistoryComponent', () => {
providers: [ providers: [
ErrorHandler, ErrorHandler,
TranslateService, TranslateService,
GcViewModelFactory,
{ provide: GcRepoService, useValue: fakeGcRepoService }, { provide: GcRepoService, useValue: fakeGcRepoService },
{ provide: GcViewModelFactory, useValue: fakeGcViewModelFactory } { provide: GcViewModelFactory, useValue: fakeGcViewModelFactory },
// open auto detect
{ provide: ComponentFixtureAutoDetect, useValue: true }
] ]
}).compileComponents(); });
})); });
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(GcHistoryComponent); fixture = TestBed.createComponent(GcHistoryComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
fixture.detectChanges(); fixture.detectChanges();
}); });
afterEach(() => {
if (component && component.timerDelay) {
component.timerDelay.unsubscribe();
component.timerDelay = null;
}
});
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
it('should retry getting jobs', fakeAsync(() => {
const spy = spyOn(fakeGcRepoService, 'getJobs').and.callThrough();
tick(11000);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(spy.calls.count()).toEqual(2);
expect(component.jobs[1].status).toEqual('finished');
});
}));
}); });

View File

@ -32,8 +32,17 @@ describe('GcComponent', () => {
delete: false delete: false
} }
]; ];
const fakedErrorHandler = {
error(error) {
return error;
},
info(info) {
return info;
}
};
let spySchedule: jasmine.Spy; let spySchedule: jasmine.Spy;
let spyJobs: jasmine.Spy; let spyJobs: jasmine.Spy;
let spyGcNow: jasmine.Spy;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
@ -43,8 +52,8 @@ describe('GcComponent', () => {
providers: [ providers: [
{ provide: GcApiRepository, useClass: GcApiDefaultRepository }, { provide: GcApiRepository, useClass: GcApiDefaultRepository },
{ provide: SERVICE_CONFIG, useValue: config }, { provide: SERVICE_CONFIG, useValue: config },
{ provide: ErrorHandler, useValue: fakedErrorHandler },
GcRepoService, GcRepoService,
ErrorHandler,
GcViewModelFactory GcViewModelFactory
] ]
}) })
@ -58,9 +67,20 @@ describe('GcComponent', () => {
gcRepoService = fixture.debugElement.injector.get(GcRepoService); gcRepoService = fixture.debugElement.injector.get(GcRepoService);
spySchedule = spyOn(gcRepoService, "getSchedule").and.returnValues(of(mockSchedule)); spySchedule = spyOn(gcRepoService, "getSchedule").and.returnValues(of(mockSchedule));
spyJobs = spyOn(gcRepoService, "getJobs").and.returnValues(of(mockJobs)); spyJobs = spyOn(gcRepoService, "getJobs").and.returnValues(of(mockJobs));
spyGcNow = spyOn(gcRepoService, "manualGc").and.returnValues(of(true));
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should create', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
it('should get schedule and job', () => {
expect(spySchedule.calls.count()).toEqual(1);
expect(spyJobs.calls.count()).toEqual(1);
});
it('should trigger gcNow', () => {
const ele: HTMLButtonElement = fixture.nativeElement.querySelector('.gc-start-btn');
ele.click();
fixture.detectChanges();
expect(spyGcNow.calls.count()).toEqual(1);
});
}); });