3

How do we write a unit test against primeng filter function in angular using jasmine

home.component.html

 <input type="text" pInputText size="50" placeholder="Global Filter" (input)="filterGlobal(dt, $event)" >

home.component.ts

  filterGlobal(dt: any, event){
    dt.filterGlobal(event.target.value, 'contains')
  }

home.component.spec.ts

itgeek
  • 549
  • 1
  • 15
  • 33
  • Any chance you were able to find a solution to this? I've been looking for a while for something. – edjm Sep 23 '22 at 21:30

1 Answers1

0

Answering this as it took me a while to figure it out.

You need to use fakeAsync() and tick() on your unit test to allow time for everything to update. Use TestBed.configureTestingModule() and TestBed.createComponent() to create your fixture. Then

it('name of test', fakeAsync(() => {

  //Arrange
  const mockInputEvent = { target: { value: 'Mock Value' } };

  //Act
  fixture.componentInstance.filterGlobal(mockInputEvent);
  tick(3000);
  fixture.detectChanges();
  
  //Assert
  //Write your assertions here

}));