0

I'm working with Angular 13. I am trying to test a method assigned to variable that looks like this:

constructor(route: ActivatedRoute) {}

compFunction(id: number) {
  if (id) {
    let result = this.returnFunction();
  }
}

returnFunction() {
  return this.route.snapshot.paramMap.get('id')
}

First I have tried this:

it("should open foo page", inject((omitted)) => {
  component.compFunction('id')
  const data = component.returnFunction(); // not working this line
  data.toLowerCase();
})); 

But I am getting the following error

cannot read properties of undefined (reading: 'toLowerCase')

Secondly I tried to mock the function and assigned it but it didn't work.

Alexis
  • 1,685
  • 1
  • 12
  • 30
vinuta
  • 423
  • 9
  • 29

1 Answers1

0

You probably don't have an id param in your route when doing the tests, so your returnFunction function returns undefined. You'll want to mock route to give it a snapshot.paramMap and provide it in your test bed.

Angular Mock ActivatedRoute using Snapshot and ParamMap

Sleepwalker
  • 803
  • 3
  • 11