如何检查按钮是否禁用?

用户名

我正在尝试添加的单元测试用例template driven form在初始阶段和当用户输入全部有效时,如何检查submit按钮被禁用enablefield

这是表格

https://stackblitz.com/edit/angular-a8q2zr?file=src%2Fapp%2Fapp.component.html

单元测试用例

https://stackblitz.com/edit/angular-testing-5m3qwm?file=app%2Fapp.component.spec.ts

import { ComponentFixture, TestBed,async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement }  from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';


//
describe('AppComponent', () => {
   let fixture ,component,debugElement; 
  beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports:      [ BrowserModule, FormsModule ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  beforeEach(()=>{
     fixture = TestBed.createComponent(AppComponent);
     component = fixture.componentInstance;
  })

  xdescribe('initial state of form',()=>{

    it('form is invalide button is disable',()=>{
    // fixture.detectChanges();
   //   debugElement = fixture.debugElement;
   //   expect(true).toBe(true)
    })
  })


})

任何更新 ?

Flyii

您可以使用以下方法获取提交按钮:

fixture.debugElement.query(By.css('input[type=submit]'))

并检查它是否被禁用,您可以使用它的nativeElement:

describe('initial state of form',()=>{

    it('form is invalide button is disable',()=>{
      let submitEL: DebugElement = fixture.debugElement.query(By.css('input[type=submit]'));
      expect(submitEL.nativeElement.disabled).toBe(true);
    })
  })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章