Unit test to test default value for mat select in angular

68 views Asked by At

I am trying to write a unit test to test default value should be 'abc' when the mat select dropdown has value 'abc' and should disable the dropdown when it has only 1 value that is 'abc'

I have the code of .ts as below:

this.example = [{name:'abc'},{name:'xyz'}];
const default= this.example.filter((x) => x.name === 'abc')
 
if (default.length > 0) {
const name= default.find((x) -> x.name).name;
this.myTestForm.controls['test'].setValue(name);
const defaultCount = 2;
if(this.example.length < defaultCount && default.some((x)-> x.name = "abc")){
this.myTestForm.controls['test"].disable();
} else {
this.myTestForm.controls['test'].enable();
}

My .html as follows:

<form [formGroup]="myTestForm">
<div>
<mat-form-field>
<mat-select formControlName='test' data-aid='default'>
<mat-option *ngFor='let t of example' [value]="t.name">
{{t.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>

I know about unit testing the dropdown values, but need in unit testing default value when the array has only 1 element

0

There are 0 answers