diff --git a/src/portal/src/app/base/global-search/global-search.component.spec.ts b/src/portal/src/app/base/global-search/global-search.component.spec.ts index d00ecf689..c64eaf941 100644 --- a/src/portal/src/app/base/global-search/global-search.component.spec.ts +++ b/src/portal/src/app/base/global-search/global-search.component.spec.ts @@ -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); + })); }); diff --git a/src/portal/src/lib/components/artifact/artifact-summary.component.ts b/src/portal/src/lib/components/artifact/artifact-summary.component.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/portal/src/lib/components/create-edit-rule/filter-label.component.spec.ts b/src/portal/src/lib/components/create-edit-rule/filter-label.component.spec.ts new file mode 100644 index 000000000..3f06d609f --- /dev/null +++ b/src/portal/src/lib/components/create-edit-rule/filter-label.component.spec.ts @@ -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; + 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(); + })); +}); diff --git a/src/portal/src/lib/components/create-edit-rule/filter-label.component.ts b/src/portal/src/lib/components/create-edit-rule/filter-label.component.ts index f088e99ac..fecff2108 100644 --- a/src/portal/src/lib/components/create-edit-rule/filter-label.component.ts +++ b/src/portal/src/lib/components/create-edit-rule/filter-label.component.ts @@ -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); } }); }