Reputation: 910
I am creating a test for clear Item, but when I run the test I get the error of "TypeError: Cannot read properties of undefined (reading 'filter') at "
How can I arrange to run filter in the test?
My function:
clearItem = (item, key) => {
this.form[key] = this.form[key].filter(el => el !== item);
this.chipsOptions = this.chipsOptions.filter(el => el !== item);
}
My test:
describe('clearItem()', () => {
it('should clear item', () => {
let item = {id:1, name:'test'};
component.form = { key: {id:1}, id: 1, name: 'testForm' };
component.addItem(item);
component.clearItem(item, {key: { id: 1 }});
expect(component.form['key']).not.toEqual(item);
expect(component.chipsOptions['key']).not.toEqual(item);
});
});
Upvotes: 0
Views: 28482
Reputation: 121
The filter
method is defined for array objects. Before using filter
method, try to check the types of objects that you use this method.
console.log(Array.isArray(this.form[key]));
console.log(Array.isArray(this.chipsOptions));
// When both of them are true, then you can use filter.
Upvotes: 2