Sachithra Wishwamal
Sachithra Wishwamal

Reputation: 127

Unit Test Angular @Input set function not triggered

In my Component

@Input('price')
set setPrice(price) {
    this.price = price;
    this.modifyTotalAmount();
}

test (component.spec.ts)

it('should call function ', () => {
    spyOn(fixture.componentInstance, 'modifyTotalAmount');
    fixture.componentInstance.price = 4500;
    fixture.detectChanges();
    const divActualPrice = fixture.debugElement.query(By.css('#actualPrice'));
    expect(divActualPrice.componentInstance.modifyTotalAmount).toHaveBeenCalled();
});

Normally when parent compoent value get changed this setPrice(price) hits and modifyTotalAmount() function called. But when unit test runs that modifyTotalAmount() not called. this test case get faild. I think what I have done in test case could be wrong. can anyone please clarify whats wrong with this.

Upvotes: 2

Views: 5900

Answers (1)

AlexG
AlexG

Reputation: 76

As Aniket Kadam already pointed out, when you set fixture.componentInstance.price = 4500 you are not using the setter setPrice that you would need to call in order to trigger this.modifyTotalAmount()

So do fixture.componentInstance.setPrice = 4500 instead.

Upvotes: 3

Related Questions