Add more UT

Signed-off-by: AllForNothing <sshijun@vmware.com>
This commit is contained in:
AllForNothing 2020-04-21 09:54:32 +08:00
parent e93bd75d38
commit 0c5d3cc899
4 changed files with 126 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, getTestBed, TestBed, tick } from '@angular/core/testing';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { GlobalSearchComponent } from './global-search.component';
import { SearchTriggerService } from './search-trigger.service';
@ -15,6 +15,9 @@ describe('GlobalSearchComponent', () => {
searchClearChan$: {
subscribe: function () {
}
},
triggerSearch() {
return undefined;
}
};
let fakeAppConfigService = {
@ -56,4 +59,16 @@ describe('GlobalSearchComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should trigger search', fakeAsync(async () => {
const service: SearchTriggerService = TestBed.get(SearchTriggerService);
const spy: jasmine.Spy = spyOn(service, 'triggerSearch').and.callThrough();
const input: HTMLInputElement = fixture.nativeElement.querySelector('#search_input');
expect(input).toBeTruthy();
input.value = 'test';
input.dispatchEvent(new Event('keyup'));
tick(500);
fixture.detectChanges();
await fixture.whenStable();
expect(spy.calls.count()).toEqual(1);
}));
});

View File

@ -0,0 +1,109 @@
import { ComponentFixture, TestBed, async, fakeAsync, tick } from '@angular/core/testing';
import { NoopAnimationsModule } from "@angular/platform-browser/animations";
import { SharedModule } from "../../utils/shared/shared.module";
import { ErrorHandler } from "../../utils/error-handler/error-handler";
import { IServiceConfig, SERVICE_CONFIG } from '../../entities/service.config';
import {FilterLabelComponent} from "./filter-label.component";
import {LabelService} from "../../services/label.service";
import { RouterTestingModule } from '@angular/router/testing';
import { CURRENT_BASE_HREF } from '../../utils/utils';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Label } from '../../services';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';
describe("FilterLabelComponent", () => {
let fixture: ComponentFixture<FilterLabelComponent>;
let comp: FilterLabelComponent;
const fakedLabel1: Label = {
id: 1,
name: "dd",
description: "fff",
color: "#CD3517",
scope: "g",
project_id: 0,
creation_time: "2020-04-20T08:08:39.540765Z",
update_time: "2020-04-20T08:08:39.540765Z",
deleted: false
};
const fakedLabel2: Label = {
id: 2,
name: "ff",
description: "fff",
color: "#CD3518",
scope: "p",
project_id: 1,
creation_time: "2020-04-20T08:08:39.540765Z",
update_time: "2020-04-20T08:08:39.540765Z",
deleted: false
};
const config: IServiceConfig = {
replicationBaseEndpoint: CURRENT_BASE_HREF + "/replication/testing",
targetBaseEndpoint: CURRENT_BASE_HREF + "/registries/testing"
};
const fakedLabelService = {
getGLabels() {
return of([fakedLabel1]).pipe(delay(0));
},
getPLabels() {
return of([fakedLabel2]).pipe(delay(0));
}
};
const fakedErrorHandler = {
error() {
return null;
}
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [SharedModule, NoopAnimationsModule, RouterTestingModule],
declarations: [
FilterLabelComponent,
],
providers: [
{ provide: ErrorHandler, useValue: fakedErrorHandler },
{ provide: SERVICE_CONFIG, useValue: config },
{ provide: LabelService, useValue: fakedLabelService}
],
schemas: [
NO_ERRORS_SCHEMA
]
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(FilterLabelComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
comp.openFilterLabelPanel = true;
});
it("Should create", () => {
expect(comp).toBeTruthy();
});
it("Should render and filter", fakeAsync( async () => {
comp.labelLists = [{
iconsShow: true,
label: fakedLabel1,
show: true,
},
{
iconsShow: true,
label: fakedLabel2,
show: true,
}];
fixture.detectChanges();
let buttons: HTMLCollection = fixture.nativeElement.getElementsByClassName('labelBtn');
expect(buttons.length).toEqual(2);
const input: HTMLInputElement = fixture.nativeElement.querySelector('.filterInput');
input.value = 'dd';
input.dispatchEvent(new Event('input'));
input.dispatchEvent(new Event('keyup'));
tick(600);
fixture.detectChanges();
await fixture.whenStable();
expect(comp.labelLists[1].show).toBeFalsy();
}));
});

View File

@ -30,7 +30,6 @@ export class FilterLabelComponent implements OnInit, OnChanges {
@Output() closePanelEvent = new EventEmitter();
constructor(private labelService: LabelService,
private ref: ChangeDetectorRef,
private errorHandler: ErrorHandler) { }
ngOnInit(): void {
@ -50,17 +49,9 @@ export class FilterLabelComponent implements OnInit, OnChanges {
.pipe(distinctUntilChanged())
.subscribe((name: string) => {
if (this.filterLabelName.length) {
this.labelLists.forEach(data => {
if (data.label.name.indexOf(this.filterLabelName) !== -1) {
data.show = true;
} else {
data.show = false;
}
data.show = data.label.name.indexOf(this.filterLabelName) !== -1;
});
setTimeout(() => {
setInterval(() => this.ref.markForCheck(), 200);
}, 1000);
}
});
}