I encountered an issue in my terminal saying:
TypeError: Cannot read properties of undefined (reading 'id')
While attempting to test the API call, this error popped up.
This is the function I am working with:
itemToForm = () => {
this.api.send(this.component, 'get',
{ lang: 'ES', filter: { id: this.item['id'] } }
).then(resEsp => {
this.item = resEsp['data'][0];
this.api.send(this.component, 'get',
{ lang: 'EN', filter: { id: this.item['id'] } }
).then(res => {
let itemEng = res['data'][0];
let fields = this.formDef.map(register => register.filter(
field => field['register_table'].indexOf('traduction') !== -1
).map(
field => field['field_name'])
).filter(register => register.length);
fields = fields.length ? fields[0] : [];
if (itemEng) {
this.item = Object.keys(itemEng).reduce((obj, key) => {
obj[key] = this.item[key];
if (fields.indexOf(key) !== -1) {
obj[key + '_eng'] = itemEng[key];
}
return obj;
}, {});
}
if (this.item) {
this.setForm();
}
})
})
}
Here is my specification file:
it('should call api.send', () => {
let spy1 = spyOn(api, 'send');
let item = {
id: 1,
name: 'test',
}
component.addItem(item);
component.itemToForm();
expect(spy1).toHaveBeenCalled();
});