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

View File

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

View File

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

View File

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

View File

@ -20,4 +20,4 @@
</clr-dg-cell>
</clr-dg-row>
<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 { GcRepoService } from "../gc.service";
import { of } from 'rxjs';
@ -6,22 +6,52 @@ import { GcViewModelFactory } from "../gc.viewmodel.factory";
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { ErrorHandler } from '../../../../utils/error-handler';
import { GcHistoryComponent } from './gc-history.component';
import { GcJobData } from "../gcLog";
describe('GcHistoryComponent', () => {
let component: 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 = {
getJobs: function () {
return of([]);
count: 0,
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 = {
createJobViewModel: function (data) {
return data;
}
};
beforeEach(async(() => {
const fakeGcViewModelFactory = new GcViewModelFactory();
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [GcHistoryComponent],
imports: [
@ -31,19 +61,36 @@ describe('GcHistoryComponent', () => {
providers: [
ErrorHandler,
TranslateService,
GcViewModelFactory,
{ provide: GcRepoService, useValue: fakeGcRepoService },
{ provide: GcViewModelFactory, useValue: fakeGcViewModelFactory }
{ provide: GcViewModelFactory, useValue: fakeGcViewModelFactory },
// open auto detect
{ provide: ComponentFixtureAutoDetect, useValue: true }
]
}).compileComponents();
}));
});
});
beforeEach(() => {
fixture = TestBed.createComponent(GcHistoryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
afterEach(() => {
if (component && component.timerDelay) {
component.timerDelay.unsubscribe();
component.timerDelay = null;
}
});
it('should create', () => {
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
}
];
const fakedErrorHandler = {
error(error) {
return error;
},
info(info) {
return info;
}
};
let spySchedule: jasmine.Spy;
let spyJobs: jasmine.Spy;
let spyGcNow: jasmine.Spy;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
@ -43,8 +52,8 @@ describe('GcComponent', () => {
providers: [
{ provide: GcApiRepository, useClass: GcApiDefaultRepository },
{ provide: SERVICE_CONFIG, useValue: config },
{ provide: ErrorHandler, useValue: fakedErrorHandler },
GcRepoService,
ErrorHandler,
GcViewModelFactory
]
})
@ -58,9 +67,20 @@ describe('GcComponent', () => {
gcRepoService = fixture.debugElement.injector.get(GcRepoService);
spySchedule = spyOn(gcRepoService, "getSchedule").and.returnValues(of(mockSchedule));
spyJobs = spyOn(gcRepoService, "getJobs").and.returnValues(of(mockJobs));
spyGcNow = spyOn(gcRepoService, "manualGc").and.returnValues(of(true));
fixture.detectChanges();
});
it('should create', () => {
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);
});
});