Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check button is disable or not in angular?

I am trying to add unit test cases of template driven form. How to check submit button is disabled at initial stage and enable when when user enter all valid field .

Here is form

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

Unit test case

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


})

Any update ?

like image 911
user944513 Avatar asked May 27 '18 12:05

user944513


Video Answer


1 Answers

You can get the submit-button by using:

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

and to check if it is disabled you use the nativeElement of it:

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);
    })
  })
like image 172
Flyii Avatar answered Sep 17 '22 14:09

Flyii