Search code examples
angularjasmineangular-materialangular-test

Unit Test for function with mat paginator method firstPage()


I have a function I am trying to test. I am using the matPaginator.firstPage() method inside of it. However any test I write for this function I can an error of

"Cannot read property 'firstPage' of undefined"

I cannot find anything on how to mock paginator in unit tests or provided it in my spec file that will allow this test to complete.

Any help/suggestions/tips would be much appreciated.


Solution

  • it('area of business selected should be in active Filter Chip Object', () => {
     component.selectedBusinessArea = 'Fake Business';
     component.activeFilterChips = [];
     component.activeFilterObj = {};
     fixture.detectChanges();
     fixture.whenStable().then(() => {
      spyOn(component.paginator, 'firstPage').and.returnValue('');
      component.startFilter();
      fixture.detectChanges();
      expect(component.activeFilterChips).toBe([{
        option: "businessArea",
        value: "Fake Business"
      }]);
     })
    });
    

    Creating a spyOn for the firstPage() for paginator and wrapping that inside of the whenStable() has my test passing. Not sure if that is the correct way of handling it. Also, I am not trying to test the firstPage() so I gave a returnValue of ''.