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 ?
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);
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With