[英]Angular - test reactive form control enable on value changes
I have to test if some controls are enabled.我必须测试是否启用了某些控件。
ngOnChanges(): void {
this.form.get('menuVisibility').valueChanges.subscribe((visible: boolean) => {
['userIdVisibility', 'userOptionsVisibility', 'languageVisibility'].forEach((controlName) => {
if (visible) {
this.form.controls[controlName].enable();
} else {
this.form.controls[controlName].disable();
}
});
});
}
I wrote this SPEC:我写了这个规范:
it('should dispatch action for valueChanges', fakeAsync(() => {
component.form.get('menuVisibility').setValue(true);
component.form.get('menuVisibility').updateValueAndValidity({ emitEvent: true });
component.ngOnChanges();
tick();
expect(component.form.get('userIdVisibility').enabled).toBeTruthy();
expect(component.form.get('userOptionsVisibility').enabled).toBeTruthy();
expect(component.form.get('languageVisibility').enabled).toBeTruthy();
}));
But the test never enters in the forEach block.但是测试永远不会进入 forEach 块。
I agree with @MoxxiManagarm where it is a memory leak, I would move it to the ngOnInit
.我同意@MoxxiManagarm 这是内存泄漏,我会将其移至
ngOnInit
。
For a quick unblock, try calling ngOnChanges()
first before setting the value so the observable stream is subscribed before we change the values:为了快速解除阻塞,在设置值之前先尝试调用
ngOnChanges()
以便在我们更改值之前订阅可观察的流:
it('should dispatch action for valueChanges', fakeAsync(() => {
component.ngOnChanges();
component.form.get('menuVisibility').setValue(true);
component.form.get('menuVisibility').updateValueAndValidity({ emitEvent: true });
tick();
expect(component.form.get('userIdVisibility').enabled).toBeTruthy();
expect(component.form.get('userOptionsVisibility').enabled).toBeTruthy();
expect(component.form.get('languageVisibility').enabled).toBeTruthy();
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.