大佬教程收集整理的这篇文章主要介绍了Angular 5组件测试选择和触发事件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的代码如下:
public onProductChanged(event): void { this.SELEctedProduct = this.products.find((product: Products) => product.id == event.target.value); }
我的选择下拉菜单:
<SELEct id="product" (changE)="onProductChanged($event)"> <option>--- Please SELEct ---</option> <option *ngFor="let product of products" [value]="product.id"> {{ product.displayName }} </option> </SELEct>
产品对象是一个对象:
{ "id": 1,"name": "name_here","displayName": "Name Here" }
这一切都有效,但我想在我的组件测试中测试,更改选择值会触发事件并检索正确的值.
我的测试代码如下:
describe('Product SELEction',() => { it('should SELEct product',() => { expect(component.SELEctedProduct).toBeUndefined(); productSELEctElement.nativeElement.value = 'Product Name'; productSELEctElement.nativeElement.dispatchEvent(new Event('change')); fixture.detectChanges(); expect(component.onProductChanged).toHaveBeenCalled(); expect(component.SELEctedProduct).toEqual({ "id": 1,"name": "product_name","displayName": "Product Name" }); }); });
已调用productChanged事件并且该测试通过.但是,我的SELEctedProduct始终为null.如何使用下拉列表中更改的值触发事件?
beforeEach(() => { fixture = TESTBed.createComponent(SELEctProductsComponent); component = fixture.componenTinstance; component.products = products; fixture.detectChanges(); productSELEctElement = fixture.debugElement.query(By.css('#products')); spyOn(component,'onProductChanged').and.callThrough(); expect(component.products).toEqual(products); expect(component.SELEctedProduct).toBeUndefined(); }); describe('Product SELEction',() => { productSELEctElement.nativeElement.value = 1; productSELEctElement.nativeElement.dispatchEvent(new Event('change')); fixture.detectChanges(); expect(component.onProductChanged).toHaveBeenCalled(); expect(component.SELEctedProduct).toEqual({ "id": 1,"displayName": "Product Name" }); }); });
以上是大佬教程为你收集整理的Angular 5组件测试选择和触发事件全部内容,希望文章能够帮你解决Angular 5组件测试选择和触发事件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。