I'm attempting to unit test my setStep() method in angular using Jasmine but I keep getting Cannot read properties of undefined (reading 'checkBusiness') thrown
which prevents me from continuing the test.
This is the function in the component.ts:
get userBusiness(): string { return this.auth && this.auth.user && this.auth.user.Source; }
setSteps() {
if (this.userBusiness.checkBusiness('ABC')) {
this.items = steps['ABC'];
if (this.application.process.onboarding.status !== 'complete') { this.show = true; }
} else {
this.items = steps['DEF'];
}
}
checkBusiness() method is defined as a monkey patch to the String prototype class:
Interface String {
checkBusiness(...args: string[]): boolean;
}
String.prototype.checkBusiness = function(...args: string[]): boolean {
let result = false;
for (let arg of args) {
const currentBusinessArray = business[this];
if (!currentBusinessArray) {
result = false;
continue;
}
if (currentBusinessArray.includes(arg)) {
result = true;
}
}
return result;
};
Business is an array defined as:
const lob = Object.freeze( <any> {
ABC : ['ABC', 'ABC1', 'ABC2', 'ABC3'],
DEF : ['DEF', 'DEF1', 'DEF2', 'DEF3']
});
Testing/spec file:
it('should confirm that "show" is set to true in setSteps()', () => {
const testUserBusiness = {...testDataSvc.userABC}
const userBusinessSpy = spyOnProperty(component, 'userBusiness').and.returnValue(testUserBusiness);
console.log(component['userBusiness']);
component.setSteps();
})
Problem::
I keep getting Cannot read properties of undefined (reading 'checkBusiness') thrown
Any idea how to get past this? I've tried to search everywhere but there isn't something like this scenerio that I can follow.