Search code examples
javascriptjasmine

jasmine test that function does return a resolved promise


Code:

var cartModule = (function() {  
  checkOverbook: function(skip) {
    return new Promise(function(resolve, reject) {
      if (skip) {
        addItemPromiseResolver = resolve;
      } else {
        resolve({"continue_add":true})
      }
    })
  },
})();

I'd like to test that when cartModule.checkOverbook is called with skip = true, that the promise is resolved, but that when it's called with skip = false it is not resolved. Is this possible/recommended, or should I just test this in the context of the consuming function?


Solution

  • I think it is possible, you could possibly do something like so:

    it('resolves if skip is false', async () => {
      const skip = false;
      await expectAsync(cartModule.checkOverbook(skip)).toBeResolvedTo({ 'continue_add': true });
    });
    
    it('changes addItemPromiseResolver to resolve if skip is true', () => {
      expect(addItemPromiseResolver).toBeUndefined();
      const skip = true;
      // just call the function
      cartModule.checkOverbook(skip);
      expect(addItemPromiseResolver).not.toBeUndefined();
    });