1

I have a function that update a variable, but in the coverage its show me that function as if I had not tried it. How can I test if a function was called ? This is my function

 changePage(event) {
    this.currentPages = event.currentPage - 1;
  }

And this is my test

it('Should call the function',()=>{
    const newPage = {
      currentPage:1
    }
    component.changePage(newPage);
    expect(component.currentPages).toEqual(0);
    expect(component.changePage).toHaveBeenCalled();
  })

enter image description here

SirRiT003
  • 173
  • 1
  • 2
  • 6

1 Answers1

0

You can spy the function to test whether it has been called or not. Over here you want the original function to be executed + it should get called. Thus you should call callThrough() method on it.

it('Should call the function',()=>{
    const newPage = {
      currentPage:1
    }
    // Spy + Call original method implemention (callThrough)
    const spyChangePage = spyOn(component, 'changePage').and.callThrough();
    component.changePage(newPage);
    expect(component.currentPages).toEqual(0);
    expect(spyChangePage).toHaveBeenCalled();
})
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299