1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

this is not working for me.Running into previous error itself.Could you please post a working code.
Happy to help :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.