1
votes

I have this error:

'Unhandled Promise rejection:', 'Cannot read properties of undefined (reading 'then')', '; Zone:', 'ProxyZone', '; Task:', 'jasmine.onComplete', '; Value:', TypeError: Cannot read properties of undefined (reading 'then')
TypeError: Cannot read properties of undefined (reading 'then')

This is my .ts file:

getPillars = async (): Promise<void> => {

        if (localStorage.getItem('pillars')) {
            this.pillars = JSON.parse(localStorage.getItem('pillars'));
            return;
        }

        let items = await this.api.send('Categories', 'get', filter ? { filter: filter } : {}).then((res: { data: any[], count: number }) => {
            return res.data.map(el => {
                return { id: el.id, name: el.name, type: 'Categories' }
            });
        });
        localStorage.setItem('pillars', JSON.stringify(items));

        this.pillars = items;
    }

And my test file:

describe('getPillars()', () => {
    it('Should validate the session success', () => {
        let spy1 = spyOn(apiService, 'send').and.returnValue(Promise.resolve(of('pillars')));

        component.getPillars();

        expect(spy1).toHaveBeenCalled();
      });
  });
1

1 Answers

1
votes
let items = await this.api.send('Categories', 'get', filter ? { filter: filter } : {}).then((res: { data: any[], count: number }) => {
            return res.data.map(el => {
                return { id: el.id, name: el.name, type: 'Categories' }
            });
        });

Here you wait a promise to be returned from send

But later in test you return a promise that resolves into another promise.

You must make it just resolve into some actual value

let spy1 = spyOn(apiService, 'send').and.returnValue(Promise.resolve('pillars'));