简体   繁体   English

Angular - 测试响应式表单控件启用值更改

[英]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.

相关问题 如何在角度反应形式控件中添加小数,其值从 1 和更大开始 - how to add decimal in angular reactive form control with value starting with 1 and greater Angular 反应式表单 - 禁用所需的表单控件使表单有效,即使没有给出值 - Angular reactive form - Disabling a required form control makes the form valid even if no value is given 角反应形式克隆值 - Angular Reactive Form clone value 角度-反应形式,如何正确存储初始值,并检查形式是否发生变化? - Angular - Reactive form, how to correctly store the initial value, and check for changes in the form? 如何使用从回调函数获得的值以角度设置反应形式控件的值? - How to set value in a reactive form control in angular, using the value obtained from the call back function? Angular Reactive Form 单元测试:HTML 值和控制值不同步 - Angular Reactive Form Unit Testing: HTML value and control value out of sync 以 angular 反应形式绑定表格数组值 - Bind form array value in angular reactive form Angular 5反应性表单-通过html更新formControl不会更新值,只有控件 - Angular 5 reactive form- updating formControl through the html does not update the value, only the control 刷新页面后Angular 4 Reactive Form启用字段 - Angular 4 Reactive Form Enable Field After refresh page Angular 7:在角度反应形式中找不到名称为 formControlName 的控件 - Angular 7: Cannot find control with name: formControlName in angular reactive form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM