如何检查按钮是否禁用角度?

时间:2018-05-27 12:09:13

标签: javascript angular jasmine karma-jasmine

我正在尝试添加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)
    })
  })


})

任何更新?

1 个答案:

答案 0 :(得分:4)

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

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);
    })
  })