angular - כיצד להגדיר תוכן טקסט של אלמנט קלט ב-html בבדיקת יחידה


אני מריץ בדיקת יחידה עבור אפליקציית אנגולר, אני רוצה להגדיר את תוכן הטקסט של אלמנט הקלט הזה באמצעות בדיקת יחידה. אני משתמש בג'סמין

<input type="text" id="accountid" class="form-control col-sm-3" [(ngModel)]="record.accountid" name="accountid" required>

ניסיתי את זה אבל זה לא עובד

let formData = fixture.debugElement.query(By.css('#accountid'));
formData.nativeElement.value = 22;
fixture.detectChanges();
console.log(formData.nativeElement.textContent);//expected to print 22
expect(formData.nativeElement.textContent).toBe(22);//fails

תשובה #1

עליך לשלוח אירוע input לאחר הגדרת הערך שלו:

let formData = fixture.debugElement.query(By.css('#accountid'));
    formData.nativeElement.value = 22;
    formData.nativeElement.dispatchEvent(new Event('input'));

expect(formData.nativeElement.value).toBe(22);