I have a filter predicate function which I use to filter a mat-table in angular
this.dataSource.filterPredicate = this.myService.tableFilter();
tableFilter(): (data: any, filter: string) => boolean {
let filterFunction = function (data, filter): boolean {
let filterValues = JSON.parse(filter);
... my logic ...
};
return filterFunction;
}
}
I want to write a unit test to test this filter predicate function
it('filter table', inject(
[MyService],
(service: MyService) => {
// how to do this?
let messageObject = service.tableFilter(dummyData,
getDummyFilterValues());
}
));
How do I call the service.tableFilter function in the unit test?
Replace the logic where I am returning an object in the below code with a boolean return in your case. I think the question is regarding how to call the returned function
typescript code
tableFilter(): (data: any, filter: string) => any {
const filterFunction = (data, filter) => {
const filterValues = JSON.parse(filter);
return data.find(d => {
return d.num === filterValues.num;
});
};
return filterFunction;
}
spec file/ unit test block
it('test filterfx', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
const filterfx = app.tableFilter();
expect(filterfx(app.data, JSON.stringify({ num: 'one' })).num).toEqual('one');
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.