简体   繁体   中英

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?

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM