I'm learning Unit testing with Jasmine and Karma. My test case is passing but I do not understand one thing. Here's my typescript:
// array list of objects where each month will have one object i.e. 12 objects (Jan to Dec)
monthsData: Array<{
monthName: string;
monthYear: number;
isInRange: boolean;
isLowerEdge: boolean;
isUpperEdge: boolean;
}>;
rangeState: { edge1Exists: boolean; edge2Exists: boolean; edgeIndexes: Array<number> };
initRangeState() {} <---- method which should be called; Not important for this question
And here's my test case in spec file:
it('should re-initialize range state when reflection is triggered', () => {
fixture.detectChanges(); <--- why this is required ?
const rangeState = { edge1Exists: true, edge2Exists: true, edgeIndexes: [] };
const monthsData = {
monthName: 'Jan',
monthYear: 2020,
isInRange: true,
isLowerEdge: true,
isUpperEdge: false
};
fixture.componentInstance.rangeState = rangeState;
fixture.componentInstance.monthsData[0] = monthsData;
...
expect(fixture.componentInstance.initRangeState).toHaveBeenCalled();
});
I do not understand that when I passed dummy rangeState and monthsData already. Then why I need to run fixture.detectChanges() in first place. My test case is failing without calling that. Please tell me the reason behind it. I'm sure there is something that I don't know.
Here's the screenshot when fixture.detectChanges() is removed:
