2

I am running unit test for angular app, I want to set the text content of this input element via unit test.I am using jasmine

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

I tried this but it is not working

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 Answer 1

6

You must dispatch an input event after setting the value of it:

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

expect(formData.nativeElement.value).toBe(22);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Chrillewoodz, but this didn't set the text content of input element, tried fixture.detectchanges after every statement, didn't work
@karansys I don't think you should be testing for textContent, but rather the value.
Thanks :) that works , can you edit your answer, so I will mark it as correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.