How to write unit test case for Angular 2 Reactive forms by using Karma/Jasmine?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to write unit test case for Angular 2 Reactive forms by using Karma/Jasmine?



I have develop a simple reactive form in angular 2, Which includes username, password and login button. Here button default disable up to getting email and password values. Here i am trying to write a test case for button enable by using karma/Jasmine, But i am new to this testing framework, I have written normal valid and invalid things,but not getting clarity to write this particular thing . If any one aware of this, could you please help me to quick start.



This is my reactive form:




<nb-card *ngIf="!loading" >
<nb-card-header>
<i class="fa fa-lock"></i>Sign In Form </nb-card-header>
<nb-card-body>
<form [formGroup]="authForm" (ngSubmit)="onLogin()" >
<input id="userid" type="text" class="bx--text-input" placeholder="EOSE Userid"
formControlName="userid" [attr.data-invalid]="useridMessage ? '' : null"/>

<div class="bx--form-requirement" *ngIf="useridMessage"> useridMessage </div>
<br>
<input id="password" type="password" class="bx--text-input" placeholder="EOSE Password"
formControlName="password" [attr.data-invalid]="passwordMessage ? '' : null"/>
<div class="bx--form-requirement" *ngIf="passwordMessage" > passwordMessage </div>

<br>
<carbon-button type="primary" [disabled]="!authForm.valid">Sign In</carbon-button>
</form>
</nb-card-body>
</nb-card>



This is my .spec file.




beforeEach(() =>
fixture = TestBed.createComponent(LoginFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
);

it('should create the app', async(() =>
const fixture = TestBed.createComponent(LoginFormComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
));
it('form should be invalid', async(() =>
component.authForm.controls['userid'].setValue('');
component.authForm.controls['password'].setValue('');
expect(component.authForm.valid).toBeFalsy();
));





Do you want to write a test case to check if button is disabled or not?
– Amit Chigadani
Aug 8 at 18:02






@AmitChigadani Yes, exactltly. If we give input data to the both fields button should enable otherwise button will disable.
– Sp Raju
Aug 9 at 4:02




1 Answer
1



Test case to check if button is disabled or not


it('form should be invalid', async(() =>
component.authForm.controls['userid'].setValue('');
component.authForm.controls['password'].setValue('');
expect(component.authForm.valid).toBeFalsy();

// update view, once the values are entered
fixture.detectChanges();
let btn = fixture.debugElement.query(By.css('carbon-button')); // fetch button element
expect(btn.nativeElement.disabled).toBeTruthy(); // check if it is disabled
));





Thanks for your answer, Which you have written this test case should be pass right because button will be disable state. But this is failing with the error is"Expected undefined to be truthy."
– Sp Raju
Aug 9 at 4:00






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard