I am facing an issue where the 'offsetWidth' value is undefined and I need to spyOn it. The function getCurrentPage retrieves an element based on the id currentpage.
Although spying on getCurrentPage works, I have been unable to declare the offsetWidth successfully.
Functions:
getCurrentPage() {
return this.$document[0].getElementById('currentPage');
}
calculateStep() {
this.stepWidth = this.getCurrentPage().offsetWidth / (this.totalSteps - 1);
return this.stepWidth;
}
Unit test:
let stepWidth = new StepController();
stepWidth.totalSteps = 5;
const dummyCurrentPage = '<div id="currentPage" style="width: 20%;"></div>';
spyOn(stepWidth, 'getCurrentPage').and.returnValue(dummyCurrentPage);
const expectedResult = '200 / 4';
const result = stepWidth.calculateStep();
expect(result).toBe(expectedResult);
});
I am trying to figure out how to spyOn the offsetWidth of this.getCurrentPage().offsetWidth?