I am running a a Jasmine test on my Angular application. I have created a test that passes and I assumed it would cover my change event method. The input is a set of radio buttons. and all I am testing is that the value changed when the event was triggered.
.ts file
onBehalfOfChangeEvent(x) {
const value = x.value; // this line not covered on report
this.params.onBehalfOf = value; // this line not covered on report
}
.html file
<div formGroupName="onBehalfOfGroup" class="form-group">
<div *ngFor="let radio of onBehalfOfArray; let i = index">
<label class="col-12 customradio"
><span>{{ radio.value }}</span>
<input
[ngClass]="{ checked: registerForm.get('onBehalfOfGroup.onBehalfOf').value == params.onBehalfOf }"
class="radioBtn"
id="radio-{{ i }}"
type="radio"
[value]="radio.value"
name="onBehalfOf"
formControlName="onBehalfOf"
(change)="onBehalfOfChangeEvent(radio)"
/>
<span class="checkmark"></span>
</label>
</div>
</div>
.spec file
it('should trigger onBehalfOfChangeEvent method on change', () => {
const testee = component;
expect(testee).toBeTruthy();
let de = fixture.debugElement.query(By.css('.radioBtn'));
spyOn(testee, 'onBehalfOfChangeEvent');
de.triggerEventHandler('change', {});
const x = { value: 'Self' };
const value = x.value;
component.onBehalfOfChangeEvent(value);
expect(testee.onBehalfOfChangeEvent).toHaveBeenCalled();
expect(component.params.onBehalfOf).toEqual(value);
});
Can anybody see why those two lines are not covered? Here is a stackBlitz - https://stackblitz.com/edit/angular-queryparam-gbb23f?file=src/app/app.component.spec.ts
onBehalfOfChangeEventbut never actually call its implementation. Try to addand.callThrough()to your spy ononBehalfOfChangeEvent. Does the test even pass where you check thatparams.onBehalfOfwas correctly set?de.triggerEventHandler('change', x)in your test now.