Reputation: 179
How to get coverage for array with .find method in angular. I'm getting trouble in the below piece of code.
sample.component.ts
public permissions = [{id: 0, value: 'fruit' }, {id: 1, value: 'vegetable'}];
this.filteredList = this.permissions.slice();
public isFiltered(permission) {
return this.filteredList.find(item => item.id === permission.id);
}
sample.spec.ts
it('should call myMethod ', () => {
expect(component.isFiltered(1)).toEqual(true);
});
I'm getting the following error ,
TypeError: Cannot read property 'find' of undefined
Any help would be appreciated
Upvotes: 1
Views: 3622
Reputation: 2377
write it like this
it('should call myMethod ', () => {
let permissions = [{id: 0, value: 'fruit' },{id: 1, value: 'vegetable'} ];
component.filteredList = permissions.slice();
expect(component.isFiltered({id:1}).value).toEqual('vegetable');
});
You need to give some value to component.filteredList
UPDATE
as you are using persmissions.id
in your ts to filter you need to pass an object with property id
(to filter by) to isFiltered()
. Also isFiltered
does not returns a boolean but the object (i.e. search result itself) so we check its value to be the required one
Upvotes: 1