I have a scenario where my function calls the api.send service twice, however when I run a test expecting it to resolve both promises, only res1 is returned and not res2. How can I ensure that both promises are resolved successfully?
Here is my function:
handleItems = () => {
this.api.send('filter', 'get', { lang: 'ES', filter: { id: this.item['id'] } }).then(res1 => {
this.item = res1['data'][0];
this.api.send('filter', 'get', { lang: 'EN', filter: { id: this.item['id'] } }).then(res2 => {
let itemEng = res2['data'][0];
});
});
};
This is my test case:
it('should invoke api.send with English result', () => {
component.formDef = [{ field_id: 57, field_key: "name1", field_name: 'test1' }];
let res1 = { data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test1' }] };
let res2 = { data: [{ id: 1, name: 'test2' }, { id: 2, name: 'test' }] };
component.item = let item = {id:1, count: 1, data:[{id: 1, tacticas: {95: [135]}}]};
let spy1 = spyOn(api, 'send').and.returnValues(Promise.resolve(res1), Promise.resolve(res2));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
expect(spy1).toHaveBeenCalledTimes(2);