I have this HTML in my component.html:
<input type="radio" [checked]="selected" (change)="select()" />
How can I make a Spectator query and expect to test if this input element is checked or not?
I have tried with:
expect(spectator.query('input')).toHaveAttribute('checked');
But I get the error:
Error: Expected element to have attribute 'checked', but had 'undefined'
And I have tried with:
expect(spectator.query('input')).toBeChecked();
But then I get the error:
Error: Expected element to be checked
How can I test this simple HTML input element?
Thank you
Søren
expect(spectator.query('input')).toBeChecked();
is the correct usage.It looks like
selected
property is false due to which radio button is not selected and you are getting this error. Simple fix you binding in test (by settingselected
to true) or update assertion to check if radio button is not selected:Take a look at this stackblitz code sample where I have 2 bound radio buttons one selected and another not selected and I have tests for it.
Also, take a look at this guide to see available custom matchers.