how to mock new promise blocks in jasmine

80 Views Asked by At

I have following method in my service

initialStateData() {
    if (!this.hasLoaded()) { 
        console.log('do you see me?? 1');
        // next line erroring out here on..
        console.log('do you see me?? 2');
        this._dataLoadPromise = new Promise((resolve, _reject) => {
                this.actionManager.initLoad();
                const promises = [];
                promises.push(this.getFacs());
                promises.push(this.getRoles());
                promises.push(this.getParams());
                promises.push(this.getSecurityRights());

                Promise.all(promises).then(() => {
                    this.actionManager.completeInitialLoad(this.initialState, this.initialStaticData);

                    this.fac$.subscribe((fac) => this.loadFacBasedData(fac, false));
                    this.fg$.subscribe((fg) => this.loadFacBasedData(fg, true));
                    this.rightSets$.subscribe((rightSets) => this.getUsersForSecRights(rightSets));
                    this._hasLoaded = true;
                    resolve();
                })
                .catch((err) => {
                    this.actionManager.dispatchAction('ERROR_OCCURRED', 'in initialStateData()');
                    this.HandleError(err);
                });
            });
    }
}

My test case as below:

describe('initialStateData', () => {
    it('makes expected calls', () => {
      const actionManagerStub: ActionManager = TestBed.get(ActionManager);
      service.initialStaticData = { rights: [{ GroupKey: 33, GroupName: 'Manager', Description: null, GroupIsShown: true }], roles: [{ roleId: 'a0986679', roleName: 'admin' } ] } ;
      spyOn(service, 'getSecurityRights').and.callThrough();
      spyOn(actionManagerStub, 'initLoad');
      spyOn(actionManagerStub, 'completeInitialLoad').and.callThrough();
      spyOn(actionManagerStub, 'dispatchAction').and.callThrough();
      (<jasmine.Spy>service.initialStateData).and.callThrough();
      service.initialStateData();
      expect(service.getRightSetsForRoles).toHaveBeenCalled();
      expect(service.getLastCoders).toHaveBeenCalled();
      expect(service.getDischarges).toHaveBeenCalled();
      expect(service.getGrouperDescription).toHaveBeenCalled();
      expect(service.getUsersForRightSets).toHaveBeenCalled();
      expect(actionManagerStub.initLoad).toHaveBeenCalled();
      expect(actionManagerStub.completeInitialLoad).toHaveBeenCalled();
      expect(actionManagerStub.dispatchAction).toHaveBeenCalled();
    });
  });

I see that do you see me?? 1 in console but do you see me?? 2 I see following errors as well in console

context.js:232 Unhandled Promise rejection: Dispatch failed: did you forget to configure your store?

how do I get the jasmine execute into new Promise((resolve, _reject)) block from test case?

any help appreciated..

1

There are 1 best solutions below

0
On

This can be resolved by using done parameter in your test case and call it once you think the promise is resolved. ex:

it('makes expected calls', (done) => {
...settingup spys etc
service.initialStateData();
...assertions
servie._dataLoadPromise.then(() => {
...moreassertions
done();
});
};