2017-09-06 5 views
0

私は剣道オートコンプリートコントロールを持つコンポーネントをテストしようとしています。テストが破損しているときには、結果がポップアップに表示されません。 何をする必要がありますか?角2テストkendo-autocomplete

他の情報が必要な場合は、以下のコードをお持ちでください。

コンポーネント

import { Component, OnInit, Input, Output, Inject } from '@angular/core'; 
 
import { IFieldLookUpService } from 'app/services/ifield-look-up.service'; 
 
import { FieldLookUpValueResults } from 'app/models/field-look-up-result'; 
 

 
@Component({ 
 
    selector: 'field-lookup', 
 
    templateUrl: './field-lookup.component.html', 
 
    styleUrls: ['./field-lookup.component.css'] 
 
}) 
 
export class FieldLookupComponent implements OnInit { 
 
    @Input() fieldId: number; 
 
    @Input() fieldName: string; 
 
    @Output() selectedValue: string; 
 
    private source: FieldLookUpValueResults; 
 
    public fieldLookUpValues: FieldLookUpValueResults; 
 

 
    constructor(@Inject('IFieldLookUpService') private fieldLookUpService: IFieldLookUpService) { } 
 

 
    ngOnInit() { 
 
    this.loadData(); 
 
    } 
 

 
    handleFilter(value) { 
 
    this.fieldLookUpValues.results = this.source.results.filter((s) => s.text.toLowerCase().indexOf(value.toLowerCase()) !== -1); 
 
    } 
 
    private loadData() { 
 
    this.fieldLookUpService.getLookUpValues(this.fieldId, this.fieldName) 
 
    .subscribe(data => { this.source = data; 
 
     this.fieldLookUpValues = new FieldLookUpValueResults(this.source.header, null); 
 
    }) 
 
    } 
 
}

Component.html

<div *ngIf="fieldLookUpValues"> 
 
    <kendo-autocomplete [data]="fieldLookUpValues.results" [valueField]="'text'" [suggest]="true" [value]="selectedValue" [filterable]="true" (filterChange)="handleFilter($event)"> 
 
     <ng-template kendoAutoCompleteHeaderTemplate> 
 
      <strong>{{fieldLookUpValues.header}}</strong> 
 
     </ng-template> 
 
    </kendo-autocomplete> 
 
</div>

スペック

import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 
 
import { DebugElement } from '@angular/core'; 
 
import { By } from '@angular/platform-browser'; 
 

 
import { FieldLookupComponent } from './field-lookup.component'; 
 
import { FieldLookUpValueResults, FieldLookUpValue } from 'app/models/field-look-up-result'; 
 

 
import { IFieldLookUpService } from 'app/services/ifield-look-up.service'; 
 

 
import { Observable } from 'rxjs/Observable'; 
 

 
import { DropDownsModule } from '@progress/kendo-angular-dropdowns'; 
 

 
fdescribe('FieldLookupComponent',() => { 
 
    let component: FieldLookupComponent; 
 
    let fixture: ComponentFixture<FieldLookupComponent>; 
 
    let debugEl: DebugElement; 
 
    let mockFieldLookUpService; 
 
    let inputElement; 
 

 
    beforeEach(async(() => { 
 
    mockFieldLookUpService = jasmine.createSpyObj('mockFieldLookUpService', ['getLookUpValues']); 
 
    let mockData = new FieldLookUpValueResults('LookUp Values Result Header', 
 
    [ 
 
     new FieldLookUpValue('LookUp Value 1', '1'), 
 
     new FieldLookUpValue('LookUp Value 2', '2'), 
 
    ]); 
 

 
    mockFieldLookUpService.getLookUpValues.and.returnValue(Observable.of(mockData)); 
 

 
    TestBed.configureTestingModule({ 
 
     declarations: [ FieldLookupComponent ], 
 
     imports: [ 
 
     DropDownsModule 
 
     ], 
 
     providers: [ 
 
     { provide: 'IFieldLookUpService', useFactory:() => mockFieldLookUpService }, 
 
     ] 
 
    }) 
 
    .compileComponents(); 
 
    })); 
 

 
    beforeEach(() => { 
 
    fixture = TestBed.createComponent(FieldLookupComponent); 
 
    component = fixture.componentInstance; 
 
    debugEl = fixture.debugElement; 
 
    fixture.detectChanges(); 
 
    inputElement = debugEl.query(By.css('input')).nativeElement; 
 
    console.log(component); 
 
    }); 
 

 
    fit('should be created',() => { 
 
    expect(component).toBeTruthy(); 
 
    }); 
 

 
    fit('should have the autocomplete input',() => { 
 
    expect(inputElement).toBeTruthy(); 
 
    }); 
 

 
    fdescribe('when character L is set in autocompelte box',() => { 
 
    let list: DebugElement; 
 
    let listItems: DebugElement[]; 
 

 
    beforeEach(() => { 
 
     inputElement.value = 'L'; 
 
     fixture.detectChanges(); 
 
     list = debugEl.query(By.css('ul')).nativeElement; 
 
     listItems = list.queryAll(By.css('li')); 
 
    }) 
 

 
    fit('should have the kend pop-up shown',() => { 
 
     expect(list).toBeTruthy(); 
 
    }); 
 

 
    }); 
 

 
});
私はオートコンプリート入力に値 'L' を設定し、その後、私は、ポップアップが表示されるはずですが、彼らは(リストおよびリストアイテム)nullで

inputElement.value = 'L'; 
fixture.detectChanges(); 
list = debugEl.query(By.css('ul')).nativeElement; 
listItems = list.queryAll(By.css('li')); 

答えて

0

オートコンプリートで使用されるポップアップコンポーネント(ポップアップを持つ他の剣道コンポーネントに適用)は、デフォルトでルートコンポーネントに追加されたです。つまり、ポップアップはコンポーネントツリーの一部ではありません。なぜそうなのかに興味がある方のため は、心の中でそれらの詳細では、このGithub issue

を読んで、あなたはオートコンプリートインスタンスを使用し、そのpopupRefプロパティからポップアップ要素を取得する必要があります。ここで

{{ autocomplete?.popupRef?.popupElement.nodeName }} 

は、このアプローチを示していplunkerです:

http://plnkr.co/edit/bQTmfBUT7r5z6wjt5MtL?p=preview

あなたは正しくpopupRefを取得するために、テストでダニを待つ必要がありますのでご注意ください。

P. IMHO、レンダリングされたULリストのテストは不要です。オートコンプリートコンポーネントを提供するベンダーは、渡された[data]の値に基づいて出力項目をテストしました。この事実を考慮すると、私はちょうどautocomplete.dataプロパティをテストするだけで十分です。

機能テストを追加して、構築しているアプリケーションが全体として機能していることを確認することができます。

関連する問題