mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-20 23:57:42 +01:00
Add more UT
Signed-off-by: AllForNothing <sshijun@vmware.com>
This commit is contained in:
parent
e93bd75d38
commit
0c5d3cc899
@ -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 { TranslateModule, TranslateService } from '@ngx-translate/core';
|
||||||
import { GlobalSearchComponent } from './global-search.component';
|
import { GlobalSearchComponent } from './global-search.component';
|
||||||
import { SearchTriggerService } from './search-trigger.service';
|
import { SearchTriggerService } from './search-trigger.service';
|
||||||
@ -15,6 +15,9 @@ describe('GlobalSearchComponent', () => {
|
|||||||
searchClearChan$: {
|
searchClearChan$: {
|
||||||
subscribe: function () {
|
subscribe: function () {
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
triggerSearch() {
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let fakeAppConfigService = {
|
let fakeAppConfigService = {
|
||||||
@ -56,4 +59,16 @@ describe('GlobalSearchComponent', () => {
|
|||||||
it('should create', () => {
|
it('should create', () => {
|
||||||
expect(component).toBeTruthy();
|
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);
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
@ -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();
|
||||||
|
}));
|
||||||
|
});
|
@ -30,7 +30,6 @@ export class FilterLabelComponent implements OnInit, OnChanges {
|
|||||||
@Output() closePanelEvent = new EventEmitter();
|
@Output() closePanelEvent = new EventEmitter();
|
||||||
|
|
||||||
constructor(private labelService: LabelService,
|
constructor(private labelService: LabelService,
|
||||||
private ref: ChangeDetectorRef,
|
|
||||||
private errorHandler: ErrorHandler) { }
|
private errorHandler: ErrorHandler) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
@ -50,17 +49,9 @@ export class FilterLabelComponent implements OnInit, OnChanges {
|
|||||||
.pipe(distinctUntilChanged())
|
.pipe(distinctUntilChanged())
|
||||||
.subscribe((name: string) => {
|
.subscribe((name: string) => {
|
||||||
if (this.filterLabelName.length) {
|
if (this.filterLabelName.length) {
|
||||||
|
|
||||||
this.labelLists.forEach(data => {
|
this.labelLists.forEach(data => {
|
||||||
if (data.label.name.indexOf(this.filterLabelName) !== -1) {
|
data.show = data.label.name.indexOf(this.filterLabelName) !== -1;
|
||||||
data.show = true;
|
|
||||||
} else {
|
|
||||||
data.show = false;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
setTimeout(() => {
|
|
||||||
setInterval(() => this.ref.markForCheck(), 200);
|
|
||||||
}, 1000);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user