Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

130
Views
Unit testing monkey patch in Angular (Cannot read properties of undefined (reading 'checkABC'))

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.

7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.